Advertisement
Guest User

Handrad Encounter mit Interrupts

a guest
Jan 24th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | None | 0 0
  1. // Handradencounter (Empfänger)
  2. // https://www.youtube.com/watch?reload=9&v=HQuLZHsGZdI
  3. // http://www.kevindarrah.com/?page_id=1348
  4. // https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
  5. // Hardware: Arduino UNO/micro
  6. // Pinout: http://pinoutguide.com/Electronics/arduino_micro_pinout.shtml
  7.  
  8. int pulses, A_SIG=0, B_SIG=1;
  9. int resetPin = 8;
  10.  
  11. void setup(){
  12.   pinMode(resetPin, INPUT_PULLUP);           // set pin to input with pullup
  13.   attachInterrupt(1, A_RISE, RISING);        // INT.1 = Pin2
  14.   attachInterrupt(0, B_RISE, RISING);        // INT.0 = Pin3
  15.   Serial.begin(115200);
  16. }//setup
  17.  
  18.  
  19. void loop(){
  20.     if (digitalRead(resetPin) == LOW) {
  21.       pulses = 0;
  22.       Serial.println(pulses);
  23.     }
  24. }
  25.  
  26. void A_RISE(){
  27.  detachInterrupt(1);
  28.  A_SIG=1;
  29.  
  30.  if(B_SIG==0)
  31.  pulses++;//moving forward
  32.  if(B_SIG==1)
  33.  pulses--;//moving reverse
  34.  Serial.println(pulses >> 2);
  35.  attachInterrupt(1, A_FALL, FALLING);
  36. }
  37.  
  38. void A_FALL(){
  39.   detachInterrupt(1);
  40.  A_SIG=0;
  41.  
  42.  if(B_SIG==1)
  43.  pulses++;//moving forward
  44.  if(B_SIG==0)
  45.  pulses--;//moving reverse
  46.  Serial.println(pulses >> 2);
  47.  attachInterrupt(1, A_RISE, RISING);  
  48. }
  49.  
  50. void B_RISE(){
  51.  detachInterrupt(0);
  52.  B_SIG=1;
  53.  
  54.  if(A_SIG==1)
  55.  pulses++;//moving forward
  56.  if(A_SIG==0)
  57.  pulses--;//moving reverse
  58.  Serial.println(pulses >> 2);
  59.  attachInterrupt(0, B_FALL, FALLING);
  60. }
  61.  
  62. void B_FALL(){
  63.  detachInterrupt(0);
  64.  B_SIG=0;
  65.  
  66.  if(A_SIG==0)
  67.  pulses++;//moving forward
  68.  if(A_SIG==1)
  69.  pulses--;//moving reverse
  70.  Serial.println(pulses >> 2);
  71.  attachInterrupt(0, B_RISE, RISING);
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement