Hi @JohnRob, Thanks for the response.
The code I've written is as follows
/*
*/
#define rotA = 6
#define rotB = 7
#define ballSpeed = 8
#define speedLimit
// Variables for rotary function
int speedLimit = 0;
int posA;
int posB;
//Variables for the Ultra Sensor 1
const int trigPin1 = 5;
const int echoPin1 = 6;
float duration1, distance1;
//Variables for Ultra Sensor 2
const int trigPin2 = 9;
const int echoPin2 = 10;
float duration2, distance2;
//Variable for calculating Speed
unsigned long time1;
unsigned long time2;
unsigned long delT;
const float distance = 0.0007;
int speed;
//Buzzer and Led
int buzzer = 12;
int led = 13;
bool reset = 11;
void setup()
{
pinMode(rotA, INPUT);
pinMode(rotB, INPUT);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(ked, OUTPUT);
Serial.begin(9600);
posB = digitalRead(rotA);
}
void loop()
{
posA = digitalRead(rotA);
if(rotA != posB)
{
noTone(buzzer);
if(digitalRead(rotB) != posA)
{
speedLimit = speedLimit + 5;
tone(buzzer,349);
delay(6);
noTone(buzzer);
}
else
{
speedLimit = speedLimit - 5;
tone(buzzer,330);
delay(6);
noTone(buzzer);
}
if(speedLimit < 0)
{
speedLimit = 0;
tone(buzzer,440);
delay(6);
noTone(buzzer);
}
else if(speedLimit > 120)
{
speedLimit = 120;
tone(buzzer,440);
delay(6);
noTone(buzzer);
}
noTone(buzzer);
Serial.print("Speed Limit: ");
Serial.println(speedLimit);
Serial.println("kmph");
}
posB = posA;
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1*.0343)/2;
Serial.print("Distance: ");
Serial.println(distance1);
delay(100);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = (duration2*.0343)/2;
Serial.print("Distance: ");
Serial.println(distance2);
delay(100);
noTone(buzzer);
if(distance1 < 180)
{
tone(buzzer,261);
delay(300);
noTone(buzzer);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
time1 = millis();
delay(1000); //we might have to remove this if the second sensor is not getting triggered
}
if(distance2 < 180)
{
tone(buzzer,392);
delay(300);
noTone(buzzer);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
time2 = millis();
delay(1000);
}
if(time2>time1)
{
delT = time2 - time1;
delT = delT*3600000; //converting it to seconds
speed = distance/delT;
Serial.print("Speed: ");
Serial.pintln(speed);
}
else
{
delT = time1 - time2;
delT = delT*3600000; //converting it to seconds
speed = distance/delT;
Serial.print("Speed: ");
Serial.pintln(speed);
}
if(reset==LOW)
if(speed>speedLimit)
{
tone(buzzer,261);
delay(300);
tone(buzzer,392);
delay(300);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
}
else
{
noTone(buzzer);
digitalWrite(led, LOW)
}
speed = ballSpeed
Serial.print("Speed Limit: ");
Serial.println(speedLimit);
Serial.print("Speed: ");
Serial.println(ballSpeed);
}
The Problem I have is, The HC-SR04 keeps on calculating the distance even after both the sensor have gathered the required information, and they keep populating the serial monitor.
What I want is,
noTone(buzzer);
if(distance1 < 180)
{
tone(buzzer,261);
delay(300);
noTone(buzzer);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
time1 = millis();
delay(1000); //we might have to remove this if the second sensor is not getting triggered
}
if(distance2 < 180)
{
tone(buzzer,392);
delay(300);
noTone(buzzer);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
time2 = millis();
delay(1000);
}
After this part of the code has been executed successfully,
I want the result to be sent to the I2C Display
and I want the result to be on the I2C display, until I press the start button (Physical push button, not the one on the Arduino)
I do not know what logic or function I should be using