const int led_1 = 11 , led_2 = 12 ,led_3 = 13 ;
const int Trig=6,Echo=7;
float distance;
unsigned long duration;
unsigned long startMillis,startMillis_2,startMillis_3;
unsigned long currentMillis;
byte Blink_1,Blink_2,Blink_3;
const short passiveDelay=400;
//const short passiveDelay=400;--- amount of delay for the timer delay
//short hold around 20000 value it can handle i think?
void setup() {
Serial.begin(115200);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
}
void Ultrasonic(){
// Start a new measurement:
// Clears the trigPin
digitalWrite(Trig, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(Echo, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.print("cm |");
}
void Led_1(){
if(distance >= 30){
//if the distance is greater then 30 is true do nothing if it's false do another statement
Serial.print(" Led 1:OFF");
digitalWrite(led_1,LOW);
Blink_1=0;
//reset the value
}
else if(distance <= 30){
//distance less than 30 true turn led 1 on if false off
Serial.print(" Led 1:ON");
if(Blink_1 != 2){
// if the blink value is not equal 2 than true if it equal false
digitalWrite(led_1,LOW);
}else{
digitalWrite(led_1,HIGH);
}
if (currentMillis - startMillis >= passiveDelay && Blink_1 != 2)
//test whether the period has elapsed and the blink amount
{
startMillis = currentMillis;
//IMPORTANT to save the start time of the current LED state.
digitalWrite(led_1,HIGH);
//if so, change the state of the LED. Uses a neat trick to change the state
Blink_1=Blink_1+1;
}
}
}
void Led_2(){
if(distance <= 30 || distance >= 45 ){
//if the distance is less then 30 or Greater then 45 is
Serial.print(" Led 2:OFF");
digitalWrite(led_2,LOW);
Blink_2=0;//reset the value
}
else if(distance <= 45){
//distance less than 30 true turn led 2 on if false off
Serial.print(" Led 2:ON");
if(Blink_2 != 4){
// if the blink value is not equal 2 than true if it equal false
digitalWrite(led_2,LOW);
}else{
digitalWrite(led_2,HIGH);
}
if (currentMillis - startMillis_2 >= passiveDelay && Blink_2 != 4){
//test whether the period has elapsed and the blink amount
//act as a delay
startMillis_2 = currentMillis;
//IMPORTANT to save the start time of the current LED state.
digitalWrite(led_2,HIGH);
//if so, change the state of the LED. Uses a neat trick to change the state
Blink_2=Blink_2+1;
}
}
}
void Led_3(){
if(distance <= 45 || distance >= 60 ){
//if the distance is less then 45 or Greater then 60 is
Serial.print(" Led 3:OFF");
//true do nothing if it's false do another statement
digitalWrite(led_3,LOW);
Blink_3=0;
//reset the value
}
else if(distance <= 60){
//distance less than 30 true turn led 2 on if false off
Serial.print(" Led 3:ON");
if(Blink_3 != 6){
// if the blink value is not equal 2 than true if it equal false
digitalWrite(led_3,LOW);
}else{ digitalWrite(led_3,HIGH);
}
if (currentMillis - startMillis_3 >= passiveDelay && Blink_3 != 6){
//test whether the period has elapsed and the blink amount
//act as a delay of 300 miliseconds (delay(300);)
startMillis_3 = currentMillis;
//IMPORTANT to save the start time of the current LED state.
digitalWrite(led_3,HIGH);
//if so, change the state of the LED. Uses a neat trick to change the state
Blink_3=Blink_3+1;
//count the amount of blink
}
}
}
void loop() {
currentMillis = millis();
//milis timer
Ultrasonic();
//Read the UltraSonic Sensor
Led_1();
//Trigger at 30 cm and lower also blink 2
Led_2();
//Trigger at 45/below cm only the middle led and blink 4
Led_3();
//Trigger at 60/below cm only the Last led and blink 6
Serial.println(" ");
}