Advertisement
Guest User

relerelerele

a guest
Dec 8th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #define pin_ping 4
  2. #define pin_led 13
  3.  
  4. volatile int pwm_value = 0;
  5. volatile int prev_time = 0;
  6. volatile int ping_time = 0;
  7. volatile boolean state_ping = false;
  8.  
  9. void setup()
  10. {
  11.   Serial.begin(115200);
  12.   // when pin D2 goes high, call the rising function
  13.   attachInterrupt(1, rising, RISING);
  14.   pinMode(pin_led, OUTPUT);
  15.   pinMode(pin_ping, OUTPUT);
  16. }
  17.  
  18. void loop()
  19. {
  20.   if (ping_time + 1000 > micros())
  21.   {
  22.     state_ping = !state_ping;
  23.     digitalWrite(pin_ping. state_ping);
  24.     ping_time = micros();
  25.   }
  26. }
  27.  
  28. void rising()
  29. {
  30.   attachInterrupt(1, falling, FALLING);
  31.   prev_time = micros();
  32. }
  33.  
  34. void falling()  
  35. {
  36.   attachInterrupt(1, rising, RISING);
  37.   pwm_value = micros() - prev_time;
  38.   Serial.println(pwm_value);
  39.  
  40.   if (pwm_value > 1800)
  41.   {
  42.     digitalWrite(pin_led, LOW);
  43.   }
  44.   else if (pwm_value < 1200)
  45.   {
  46.     digitalWrite(pin_led, HIGH);
  47.   }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement