Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const byte TRIGGER_PIN = 2;
- const byte ECHO_PIN = 3;
- const float SOUND_SPEED = 340.0 / 1000;
- int a = 0;
- void setup() {
- // put your setup code here, to run once:
- pinMode(13, OUTPUT);
- pinMode(7, OUTPUT);
- pinMode(TRIGGER_PIN, OUTPUT);
- pinMode(ECHO_PIN, INPUT);
- digitalWrite(TRIGGER_PIN, LOW);
- Serial.begin(9600);
- }
- void loop() {
- digitalWrite(TRIGGER_PIN,HIGH);
- delay(10);
- digitalWrite(TRIGGER_PIN,LOW);
- long measure = pulseIn(ECHO_PIN, HIGH, 25000UL);
- float distance_mm = measure / 2.0 * SOUND_SPEED;
- Serial.println(distance_mm);
- if(distance_mm<300){
- tone(7, 400, 5000);
- delay(500);
- }
- else{
- noTone(7);
- }
- //tone(7, 440, 2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement