that_one_nigga_you_k

o sensor de ré para tinkercad

Dec 5th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #define buzzer 7
  2.  
  3. unsigned long echo = 0;
  4. int ultraSoundSignal = 9; //pino do ultrassom
  5. unsigned long ultrasoundValue = 0;
  6. int led[6] = {0, 1, 2, 3, 4, 5};
  7. int freq = 2573;  /* essas frequencias são notas musicais específicas / these frequences are in musical notes*/
  8. int dlay[6] = {100, 150, 200, 250, 300, 350};
  9.  
  10. void setup()
  11. {
  12.   Serial.begin(9600);
  13.   pinMode(ultraSoundSignal,OUTPUT);
  14.   for(int i = 0; i<6;i++){
  15.     pinMode(led[i], OUTPUT);
  16. }
  17.   pinMode(buzzer, OUTPUT);
  18. }
  19. unsigned long ping()
  20. {
  21.   pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
  22.   digitalWrite(ultraSoundSignal, LOW); // Send low pulse
  23.   delayMicroseconds(2); // Wait for 2 microseconds
  24.   digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
  25.   delayMicroseconds(5); // Wait for 5 microseconds
  26.   digitalWrite(ultraSoundSignal, LOW); // Holdoff
  27.   pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
  28.   digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
  29.   // please note that pulseIn has a 1sec timeout, which may
  30.   // not be desirable. Depending on your sensor specs, you
  31.   // can likely bound the time like this -- marcmerlin
  32.   // echo = pulseIn(ultraSoundSignal, HIGH, 38000)
  33.   echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
  34.   ultrasoundValue = (echo / 58.138); //convert to CM then to inches
  35.   return ultrasoundValue;
  36. }
  37.  
  38. void loop()
  39. {
  40.   int x = 0;
  41.   x = ping();
  42.   Serial.println(x);
  43.   delay(250); //delay 1/4 seconds.
  44.  
  45.   Serial.print("Distancia: ");
  46.  
  47.   Serial.print(x);
  48.   Serial.print(" cm");
  49.   if(x> 0 && x <= 10){
  50.     ativarLeds(6, 0);
  51.   }
  52.   else if(x > 10 && x <= 20){
  53.     ativarLeds(5, 1);
  54.   }
  55.   else if(x > 20 && x <= 30){
  56.     ativarLeds(4, 2);
  57.   }
  58.   else if(x > 30 && x <= 40){
  59.     ativarLeds(3, 3);
  60.   }
  61.   else if(x > 40 && x <= 50){
  62.     ativarLeds(2, 4);
  63.   }
  64. }
  65. void ativarLeds(int qntdLeds, int f){
  66.   for(int i = 0; i < qntdLeds; i++){
  67.     digitalWrite(led[i], HIGH);  
  68.   }
  69.   for(int i = 5; i > qntdLeds - 1; i--){
  70.     digitalWrite(led[i], LOW);
  71.   }
  72.   noTone(buzzer);
  73.   delay(dlay[f]);
  74.   tone(buzzer, freq);
  75. }
Add Comment
Please, Sign In to add comment