Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Handradencounter (Empfänger)
- // https://www.youtube.com/watch?reload=9&v=HQuLZHsGZdI
- // http://www.kevindarrah.com/?page_id=1348
- // https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
- // Hardware: Arduino UNO/micro
- // Pinout: http://pinoutguide.com/Electronics/arduino_micro_pinout.shtml
- int pulses, A_SIG=0, B_SIG=1;
- int resetPin = 8;
- void setup(){
- pinMode(resetPin, INPUT_PULLUP); // set pin to input with pullup
- attachInterrupt(1, A_RISE, RISING); // INT.1 = Pin2
- attachInterrupt(0, B_RISE, RISING); // INT.0 = Pin3
- Serial.begin(115200);
- }//setup
- void loop(){
- if (digitalRead(resetPin) == LOW) {
- pulses = 0;
- Serial.println(pulses);
- }
- }
- void A_RISE(){
- detachInterrupt(1);
- A_SIG=1;
- if(B_SIG==0)
- pulses++;//moving forward
- if(B_SIG==1)
- pulses--;//moving reverse
- Serial.println(pulses >> 2);
- attachInterrupt(1, A_FALL, FALLING);
- }
- void A_FALL(){
- detachInterrupt(1);
- A_SIG=0;
- if(B_SIG==1)
- pulses++;//moving forward
- if(B_SIG==0)
- pulses--;//moving reverse
- Serial.println(pulses >> 2);
- attachInterrupt(1, A_RISE, RISING);
- }
- void B_RISE(){
- detachInterrupt(0);
- B_SIG=1;
- if(A_SIG==1)
- pulses++;//moving forward
- if(A_SIG==0)
- pulses--;//moving reverse
- Serial.println(pulses >> 2);
- attachInterrupt(0, B_FALL, FALLING);
- }
- void B_FALL(){
- detachInterrupt(0);
- B_SIG=0;
- if(A_SIG==0)
- pulses++;//moving forward
- if(A_SIG==1)
- pulses--;//moving reverse
- Serial.println(pulses >> 2);
- attachInterrupt(0, B_RISE, RISING);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement