Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. const int trigPin = 38;
  3. const int echoPin = 39;
  4. const int detection = 160;
  5. const int rgreenb = 31;
  6. const int redgb = 30;
  7. const int rgblue = 33;
  8.  
  9.  
  10. long duration;
  11. int distance;
  12. int reserved;
  13. int inuse;
  14.  
  15. void setup() {
  16.   pinMode(trigPin, OUTPUT);
  17.   pinMode(echoPin, INPUT);
  18.   Serial.begin(9600);
  19. }
  20.  
  21. void loop() {
  22.   digitalWrite(trigPin, LOW);
  23.   delayMicroseconds(2);
  24.   digitalWrite(trigPin, HIGH);
  25.   delayMicroseconds(10);
  26.   digitalWrite(trigPin, LOW);
  27.   duration = pulseIn(echoPin, HIGH);
  28.   distance = duration * 0.034 / 2;
  29.  
  30.   if (distance <= detection) {
  31.     rgb(0, 255, 255);
  32.     inuse = 1;
  33.   }
  34.   if (distance > detection) {
  35.     rgb(255, 0, 255);
  36.     inuse = 0;
  37.   }
  38.   if (reserved == 1 && distance > detection) {
  39.     rgb(255, 255, 0);
  40.   }
  41.   Serial.println(distance);
  42.   delay(250);
  43. }
  44.  
  45. void rgb(int r, int g, int b)//finkcia na zjednodusenie pouzivania rgb led zaradeny
  46. {
  47. #ifdef pre
  48.   red = 255 - r;
  49.   green = 255 - g;
  50.   blue = 255 - b;
  51. #endif
  52.   analogWrite(redgb, r);
  53.   analogWrite(rgreenb, g);
  54.   analogWrite(rgblue, b);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement