Created
October 7, 2016 22:37
-
-
Save rtyley/70cba919a3aa07545b1c46f999aebaa0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Analog Input | |
Demonstrates analog input by reading an analog sensor on analog pin 0 and | |
turning on and off a light emitting diode(LED) connected to digital pin 13. | |
The amount of time the LED will be on and off depends on | |
the value obtained by analogRead(). | |
The circuit: | |
* Potentiometer attached to analog input 0 | |
* center pin of the potentiometer to the analog pin | |
* one side pin (either one) to ground | |
* the other side pin to +5V | |
* LED anode (long leg) attached to digital output 13 | |
* LED cathode (short leg) attached to ground | |
* Note: because most Arduinos have a built-in LED attached | |
to pin 13 on the board, the LED is optional. | |
Created by David Cuartielles | |
modified 30 Aug 2011 | |
By Tom Igoe | |
This example code is in the public domain. | |
http://www.arduino.cc/en/Tutorial/AnalogInput | |
*/ | |
int sensorPin = A0; // select the input pin for the potentiometer | |
int ledPin = 13; // select the pin for the LED | |
int16_t sensorValue = 0; // variable to store the value coming from the sensor | |
int count = 0; | |
const int Setter = 5; | |
const int Unsetter = 6; | |
// the setup function runs once when you press reset or power the board | |
void setup() { | |
pinMode(Setter, OUTPUT); | |
pinMode(Unsetter, OUTPUT); | |
// declare the ledPin as an OUTPUT: | |
pinMode(ledPin, OUTPUT); | |
Serial.println("Well, setup completed..."); | |
} | |
void loop() { | |
Serial.print("About to read_differential... "); | |
Serial.println(count); | |
// analogReference(EXTERNAL); | |
sensorValue = analogRead(A0); // adc_read(0x30); // | |
Serial.print("A0="); | |
Serial.println(sensorValue); | |
sensorValue = analogRead(A4); // adc_read(0x30); // | |
Serial.print("A4="); | |
Serial.println(sensorValue); | |
if (sensorValue > 0) { | |
digitalWrite(ledPin, HIGH); | |
pulse(Setter); | |
} else { | |
digitalWrite(ledPin, LOW); | |
pulse(Unsetter); | |
} | |
delay(500); | |
count++; | |
} | |
void pulse(int pin) { | |
digitalWrite(pin, HIGH); | |
delay(15); | |
digitalWrite(pin, LOW); | |
} | |
int16_t adc_read(byte mux) | |
{ | |
// _BV(REFS1) | _BV(REFS0) | | |
ADMUX = _BV(REFS1) | _BV(REFS0) | (mux & 0x1f); | |
ADCSRB = _BV(ADHSM); // high speed mode | |
if(mux & 0x20) ADCSRB |= _BV(MUX5); | |
delayMicroseconds(125); // required for amplifier to settle | |
// Start the conversion | |
bitWrite(ADCSRA, ADSC, 1); | |
delayMicroseconds(125); // required for amplifier to settle | |
while (bit_is_set(ADCSRA, ADSC)); | |
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH | |
uint8_t high = ADCH; // unlocks both | |
Serial.print("Read values...\nlow="); | |
Serial.print(low); | |
Serial.print(" high="); | |
Serial.println(high); | |
int smallInt = (uint16_t)high << 8 | (uint16_t)low; | |
const int negative = (smallInt & (1 << 9)) != 0; | |
int nativeInt; | |
if (negative) | |
nativeInt = smallInt | ~((1 << 10) - 1); | |
else | |
nativeInt = smallInt; | |
return nativeInt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment