Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.59 KB | None | 0 0
  1. const int TRIG= PB3;
  2. const int ECHO = PB4;
  3.  
  4. void setup() {
  5.   pinMode(TRIG, OUTPUT);
  6.   pinMode(ECHO, INPUT_PULLDOWN);
  7.   attachInterrupt(ECHO, interrupt, CHANGE);
  8.   Serial.begin(9600);
  9. }
  10.  
  11. long time1 = 0;
  12. long time2 = 0;
  13. bool state = 0;
  14.  
  15. void loop() {
  16.   if(micros() - time1 > 1000000) //1 másodpercenként uj mérést kezd
  17.   {
  18.     digitalWrite(TRIG,HIGH);
  19.     digitalWrite(TRIG,LOW);
  20.     time1 = micros();
  21.   }
  22. }
  23.  
  24. void interrupt() {
  25.   if(state)
  26.   {
  27.     time2=micros();
  28.   }
  29.   else
  30.   {
  31.     Serial.println((float)(micros() - time2)/1000000.0 * 170.0);
  32.   }
  33.   state = !state;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement