Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int trigPin = 12;
- const int echoPin = 13;
- const int motorPin = 5;
- const int motor2Pin = 6;
- const int limitCm = 30;
- #define Buzzer1 10
- #define Buzzer2 11
- int distancia;
- void setup()
- {
- Serial.begin(9600);
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(motorPin, OUTPUT);
- pinMode(motor2Pin, OUTPUT);
- pinMode(Buzzer1, OUTPUT); // configura pino como saída
- pinMode(Buzzer2, OUTPUT); // configura pino como saída
- }
- void loop()
- {
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- unsigned long duracao = pulseIn(echoPin, HIGH);
- distancia = duracao / 58;
- Serial.print("Distancia em CM: ");
- Serial.print(distancia);
- Serial.println("cm");
- checkLimit();
- delay( 1000 );
- }
- void checkLimit()
- {
- if ( distancia < limitCm ) {
- digitalWrite( motorPin, HIGH);
- analogWrite(Buzzer1, 127);
- digitalWrite( motor2Pin, HIGH);
- analogWrite(Buzzer2, 127);
- Serial.println( "ALERTA!...");
- } else {
- digitalWrite( motorPin, LOW);
- analogWrite(Buzzer1, 0);
- analogWrite(Buzzer2, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement