Last active
July 6, 2020 17:28
-
-
Save apotox/68f0e797aa01e8a92ea12c1586201330 to your computer and use it in GitHub Desktop.
AnalogRead and the interrupt pins
This file contains 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
/* | |
------------------- | |
read more about this gist here: https://medium.com/@saphidev/analogread-and-the-interrupt-pins-f9c986d3fc0e | |
------------------- | |
*/ | |
#define LED 13 //optionel | |
#define BTN A5 //ADC5 | |
#define INTRPT 2 //PD2 refer to the INT0 vector | |
void setup(){ | |
Serial.begin(9600); | |
pinMode(LED,OUTPUT); | |
pinMode(INTRPT,INPUT_PULLUP); | |
pinMode(BTN,INPUT_PULLUP); | |
//digitalPinToInterrupt(INTRPT) or INT0 | |
//toggle is our ISR function | |
//FALLING: when the pin goes from HIGH to LOW | |
attachInterrupt(digitalPinToInterrupt(INTRPT),toggle,FALLING); | |
} | |
void toggle(){ | |
//delay to give the ADC a time to correct its value | |
delay(10); | |
Serial.print("value: "); | |
short v = analogRead(BTN); | |
Serial.println(v); | |
if(v > V1-10 && v < V1 + 10){ | |
//... button 1 pushed | |
}else if(v > V2-10 && v < V2 + 10){ | |
//... button 2 pushed | |
}else if(v > V3-10 && v < V3 + 10){ | |
//... button 3 pushed | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment