LED blink via ultrasonic sensor


so i'm using code fine when not blinking, respective led should light @ specific distance range.

but after added code blink, led specific range still blinks 1 should blinking on correct range.

}

void loop() {
  long duration, distance;
  digitalwrite(trigpin, low);
  delaymicroseconds(2);
  digitalwrite(trigpin, high);
  delaymicroseconds(10);
  digitalwrite(trigpin, low);
  duration = pulsein(echopin, high);
  distance = (duration/2) / 29.1;
 

  if (distance <= 100) {
    digitalwrite(led, high);
    delay(200);                  // waits second
    digitalwrite(led, low);    // sets led off
    delay(200);                  // waits second
    digitalwrite(led, high);
    sound = 300;
}
  else {
    digitalwrite(led,low);
 
  }
  if (distance < 60) {
      digitalwrite(led,low);
      digitalwrite(led3, high);
      delay(100);                  // waits second
      digitalwrite(led3, low);    // sets led off
      delay(100);                  // waits second
     
     
      sound = 375;
}
  else {
    digitalwrite(led3, low);
    digitalwrite(led,low);
   
  }
 
  if (distance < 20) {
    digitalwrite(led5, high);
    delay(50);                  // waits second
    digitalwrite(led5, low);    // sets led off
    delay(50);                  // waits second
    digitalwrite(led3, low);
    digitalwrite(led,low);
    sound = 450;
}
  else {
    digitalwrite(led5,low);
 
  }
 
  if (distance > 100 || distance <= 0){
    serial.println("out of range");
    notone(buzzer);
  }
  else {
    serial.print(distance);
    serial.println(" cm");
    tone(buzzer, sound);
   
  }
  delay(500);
}

you can range using distance.

if (distance >= 60 && distance < 100)

have looked @ state machine option this?
also, rid of delays , master blink without delay example.

see:
http://www.thebox.myzen.co.uk/tutorial/state_machine.html


Arduino Forum > Using Arduino > Programming Questions > LED blink via ultrasonic sensor


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code