Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define buzzer 7
- unsigned long echo = 0;
- int ultraSoundSignal = 9; //pino do ultrassom
- unsigned long ultrasoundValue = 0;
- int led[6] = {0, 1, 2, 3, 4, 5};
- int freq = 2573; /* essas frequencias são notas musicais específicas / these frequences are in musical notes*/
- int dlay[6] = {100, 150, 200, 250, 300, 350};
- void setup()
- {
- Serial.begin(9600);
- pinMode(ultraSoundSignal,OUTPUT);
- for(int i = 0; i<6;i++){
- pinMode(led[i], OUTPUT);
- }
- pinMode(buzzer, OUTPUT);
- }
- unsigned long ping()
- {
- pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
- digitalWrite(ultraSoundSignal, LOW); // Send low pulse
- delayMicroseconds(2); // Wait for 2 microseconds
- digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
- delayMicroseconds(5); // Wait for 5 microseconds
- digitalWrite(ultraSoundSignal, LOW); // Holdoff
- pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
- digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
- // please note that pulseIn has a 1sec timeout, which may
- // not be desirable. Depending on your sensor specs, you
- // can likely bound the time like this -- marcmerlin
- // echo = pulseIn(ultraSoundSignal, HIGH, 38000)
- echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
- ultrasoundValue = (echo / 58.138); //convert to CM then to inches
- return ultrasoundValue;
- }
- void loop()
- {
- int x = 0;
- x = ping();
- Serial.println(x);
- delay(250); //delay 1/4 seconds.
- Serial.print("Distancia: ");
- Serial.print(x);
- Serial.print(" cm");
- if(x> 0 && x <= 10){
- ativarLeds(6, 0);
- }
- else if(x > 10 && x <= 20){
- ativarLeds(5, 1);
- }
- else if(x > 20 && x <= 30){
- ativarLeds(4, 2);
- }
- else if(x > 30 && x <= 40){
- ativarLeds(3, 3);
- }
- else if(x > 40 && x <= 50){
- ativarLeds(2, 4);
- }
- }
- void ativarLeds(int qntdLeds, int f){
- for(int i = 0; i < qntdLeds; i++){
- digitalWrite(led[i], HIGH);
- }
- for(int i = 5; i > qntdLeds - 1; i--){
- digitalWrite(led[i], LOW);
- }
- noTone(buzzer);
- delay(dlay[f]);
- tone(buzzer, freq);
- }
Add Comment
Please, Sign In to add comment