Hi There,
I'm currently creating a project where I use 3 x Ultrasonic Distance Sensors in a line to detect when an object is placed on each individual one and then the results co-ordinate to a traffic light system.
RED- All sensors have an object on them
YELLOW- some have objects on them but not none and not all, e.g 010, 001, 101, 011, 100 =YELLOW
GREEN - None of the sensors have object on them.
Below is my basic logic circuit that works with the dip switches in place of the sensors.
Once i had the above working i switched to attempting to attach and the code the ultrasonic sensors.
Below is the circuit:
I changed none of the logic circuit and only attached the sensors to their pins. Unfortunately, I cannot get the code to work. I cannot see where my error would lie as i've researched the different codes and then applied them to this project but it seems that the sensors just aren't giving an output. Currently, when the simulation starts, the Green LED is on for a few seconds, the Yellow blips on, and the Red LED stays on. I have a feeling this may be something to do with the sequencing of how its reading and completing tasks but I don't know how to solve the issue. Any help would be much appreciated.
const int trigPinA = A0;
const int trigPinB = A1;
const int trigPinC = A2;
const int AechoPin = A3;
const int BechoPin = A4;
const int CechoPin = A5;
const int logicPinA = 2;
const int logicPinB = 3;
const int logicPinC = 4;
float timingA = 0.0;
float timingB = 0.0;
float timingC = 0.0;
float distanceA = 0.0;
float distanceB = 0.0;
float distanceC = 0.0;
void setup() {
pinMode(trigPinA, OUTPUT);
pinMode(trigPinB, OUTPUT);
pinMode(trigPinC, OUTPUT);
pinMode(AechoPin, INPUT);
pinMode(BechoPin, INPUT);
pinMode(CechoPin, INPUT);
pinMode(logicPinA, OUTPUT);
pinMode(logicPinB, OUTPUT);
pinMode(logicPinC, OUTPUT);
digitalWrite(trigPinA, LOW);
digitalWrite(trigPinB, LOW);
digitalWrite(trigPinC, LOW);
digitalWrite(logicPinA, LOW);
digitalWrite(logicPinB, LOW);
digitalWrite(logicPinC, LOW);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPinA, LOW);
delay (2);
digitalWrite(trigPinA, HIGH);
delay(10);
digitalWrite(trigPinA, LOW);
digitalWrite(trigPinB, LOW);
delay (2);
digitalWrite(trigPinB, HIGH);
delay(10);
digitalWrite(trigPinB, LOW);
digitalWrite(trigPinC, LOW);
delay (2);
digitalWrite(trigPinC, HIGH);
delay(10);
digitalWrite(trigPinC, LOW);
timingA = pulseIn(AechoPin, HIGH);
timingB = pulseIn(BechoPin, HIGH);
timingC = pulseIn(CechoPin, HIGH);
distanceA = (timingA * 0.034 / 2);
distanceB = (timingB * 0.034 / 2);
distanceC = (timingC * 0.034 / 2);
if (distanceA < 20 ) {
digitalWrite (logicPinA, HIGH);
}
else if (distanceA >= 20) {
digitalWrite (logicPinA, LOW);
}
if (distanceB < 20 ) {
digitalWrite (logicPinB, HIGH);
}
else if (distanceB >= 20) {
digitalWrite (logicPinB, LOW);
}
if (distanceC < 20 ) {
digitalWrite (logicPinC, HIGH);
}
else if (distanceC >= 20) {
digitalWrite (logicPinC, LOW);
}
}


