Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <NewPing.h>
- #define TRIGGER_PIN 2
- #define ECHO_PIN 3
- #define ALERT_PIN 4
- #define MAX_DISTANCE 200
- NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
- int time_on = 2000;
- void setup() {
- Serial.begin(115200);
- pinMode(ALERT_PIN,OUTPUT);
- pinMode(13,OUTPUT);
- digitalWrite(ALERT_PIN,LOW);
- }
- void loop() {
- int test = (sonar.ping_cm());
- if (test < 50 && test >0){
- digitalWrite(ALERT_PIN,HIGH);
- digitalWrite(13,HIGH);
- delay(time_on);
- digitalWrite(ALERT_PIN,LOW);
- digitalWrite(13,LOW);
- }
- delay(100);
- Serial.print("Ping: ");
- Serial.print(test);
- Serial.println("cm");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement