#define redled 10
#define greenled 11
#define echoPin 12
#define trigPin 13
unsigned long interval=60000; //interval = 60 seconds
unsigned long previoustime=0;
int minutes = 0; //global variables
void setup() {
Serial.begin (9600); //Set Baud rate to 9600 to match serial monitor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(greenled, OUTPUT);
pinMode(redled, OUTPUT);
}
void loop() {
//-----------------------------------------------------------
unsigned long currenttime = millis(); //counts time in milliseconds and puts it into current time
if ((currenttime-previoustime) >= interval) { //When time is greater 60 seconds
previoustime = currenttime; //Make the previous time equalled to the minute to take away in the next if statement **Time since start section**
minutes = minutes + 1; //Add 1 minute
}
unsigned long time = (currenttime-previoustime) / 1000; //converts the milliseconds to seconds
//-----------------------------------------------------------
long distance = ultra_sensor(); //call function that now returns the distance
//-----------------------------------------------------------
if (distance < 4) { // If distance is less than 4cm
digitalWrite(redled,HIGH); // Turn on Red LED
digitalWrite(greenled,LOW); // Turn off Green LED
}
else { //If distance is greater than 4cm
digitalWrite(redled,LOW); // Turn off Red LED
digitalWrite(greenled,HIGH); // Turn on Green LED
}
//-----------------------------------------------------------
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.print("cm");
Serial.print(" after ");
Serial.print(minutes); //Printing distance and time to serial monitor
Serial.print(" minutes ");
Serial.print("and ");
Serial.print(time);
Serial.println(" seconds");
}
//-----------------------------------------------------------
delay(2000); //Get reading every 2 seconds
}
long ultra_sensor() { //Ultrasonic sensor reading is now a function. More efficient
digitalWrite(trigPin, LOW); //Set Trigger to idle
delayMicroseconds(2); //Wait 2 microseconds
digitalWrite(trigPin, HIGH); //Set Trigger to high which sends out a wave
delayMicroseconds(10); //Send the wave for 10 microseconds
digitalWrite(trigPin, LOW); //Turn the trigger to idle
long duration = pulseIn(echoPin, HIGH); //make duration the wave that echo recieves back
long dis = (duration/2) / 29.1; //divide by 2 for going there and back. ***Speed of sound***
return dis; //Send the distance to function call
}
#define redled 10
#define greenled 11
#define echoPin 12
#define trigPin 13
unsigned long interval=60000; //interval = 60 seconds
unsigned long previoustime=0;
int minutes = 0; //global variables
void setup() {
Serial.begin (9600); //Set Baud rate to 9600 to match serial monitor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(greenled, OUTPUT);
pinMode(redled, OUTPUT);
}
void loop() {
//-----------------------------------------------------------
unsigned long currenttime = millis(); //counts time in milliseconds and puts it into current time
if ((currenttime-previoustime) >= interval) { //When time is greater 60 seconds
previoustime = currenttime; //Make the previous time equalled to the minute to take away in the next if statement **Time since start section**
minutes = minutes + 1; //Add 1 minute
}
unsigned long time = (currenttime-previoustime) / 1000; //converts the milliseconds to seconds
//-----------------------------------------------------------
long distance = ultra_sensor(); //call function that now returns the distance
//-----------------------------------------------------------
if (distance < 4) { // If distance is less than 4cm
digitalWrite(redled,HIGH); // Turn on Red LED
digitalWrite(greenled,LOW); // Turn off Green LED
}
else { //If distance is greater than 4cm
digitalWrite(redled,LOW); // Turn off Red LED
digitalWrite(greenled,HIGH); // Turn on Green LED
}
//-----------------------------------------------------------
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.print("cm");
Serial.print(" after ");
Serial.print(minutes); //Printing distance and time to serial monitor
Serial.print(" minutes ");
Serial.print("and ");
Serial.print(time);
Serial.println(" seconds");
}
//-----------------------------------------------------------
delay(2000); //Get reading every 2 seconds
}
long ultra_sensor() { //Ultrasonic sensor reading is now a function. More efficient
digitalWrite(trigPin, LOW); //Set Trigger to idle
delayMicroseconds(2); //Wait 2 microseconds
digitalWrite(trigPin, HIGH); //Set Trigger to high which sends out a wave
delayMicroseconds(10); //Send the wave for 10 microseconds
digitalWrite(trigPin, LOW); //Turn the trigger to idle
long duration = pulseIn(echoPin, HIGH); //make duration the wave that echo recieves back
long dis = (duration/2) / 29.1; //divide by 2 for going there and back. ***Speed of sound***
return dis; //Send the distance to function call
}