Hello All
I couldn't find what I was looking to accomplish my end result. I have a project where all my PING)) sensors (4 sensors) are firing simultaneously (I found a code written by mjmcondor in the forum that accomplished most of what I needed)
Now what i need to do is to have a red light and a green light (led's) come on at a given distance, and tell me which sensor was triggered.
I'm using the "if/else" at the bottom of the code to trigger the red led if it's less that 5 inches
then have both led's come on everything at 6 to 9 inches,
and only the green led at anything above 10 inches.
I have played with this different variation of this code, and only made matters worse, so I took it back to where it was working. However, only the "Rear right" Ping seems to trigger the LED's. I'm assuming because it is the last value entered.
What I need help with;
-
having the LED's fire at the right time (and as fast as possible)
-
any suggestions as to how i can indicate which ping sensor is being triggered?
I was thinking of simply having 4 red LED's and 4 GREEN and just placing them respectively to the sensors, but I'm hoping there's a simpler method that I haven't thought of.
All help welcomed, I am very new to this.
Thank you
int ultraSoundSignalPins[] = {6,8,9,12}; // Front Left (6),Front Right(8), Rear Left(9), & Rear Right(12) Ultrasound signal pins
char *pingString[] = {"Front Left ","Front Right ", "Rear Left ", "Rear Right "}; // just something to print to indicate direction
const int redPin = 10;
const int greenPin = 11;
void setup()
{
Serial.begin(9600);
}
//Ping function
unsigned long ping(int index)
{
unsigned long echo;
pinMode(ultraSoundSignalPins[index], OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignalPins[index], LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignalPins[index], HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignalPins[index], LOW); // Holdoff
pinMode(ultraSoundSignalPins[index], INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignalPins[index], HIGH); // Turn on pullup resistor
echo = pulseIn(ultraSoundSignalPins[index], HIGH); //Listen for echo
//return (echo / 58.138); //convert to CM
return (echo / 58.138) * .39; //convert to CM then to inches
}
void loop()
{
unsigned long ultrasoundValue;
for(int i=0; i < 4; i++){
ultrasoundValue = ping(i);
Serial.print(pingString[i]);
Serial.print(ultrasoundValue);
//Serial.print("CM, ");
Serial.print("in, ");
delay(50);
}
{
if ( ultrasoundValue <= 5){
digitalWrite(redPin,LOW);
}
else {
digitalWrite(redPin,HIGH);
}
}
if ( ultrasoundValue >= 10){
digitalWrite(greenPin,LOW);
}
else {
digitalWrite(greenPin,HIGH);
}
Serial.println();
delay(200);
}