# Very simple sketch to light LEDs according to PING reading- SOLVED

I have simple sketch to light LEDs according to the reading from a PING sensor. If distance is short, light up three of them, if distance is long light up only one.

``````// begin code

// set the constants
const int pingPin = 10;
int ledPin2 = 2;  //use this one to show a zero or very long reading
int ledPin3 = 3;
int ledPin4 = 4;
int ledPin5 = 5;
long duration, inches;

void setup()
{
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
}

// main loop
void loop()
{

// take a PING reading- straight from the example sketch
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

inches = microsecondsToInches(duration);

//  blink 3 LEDs if close and only 1 if far away
//  blink another LED on Pin2 quickly if distance=0 and slowly if distance is big

if (inches = 0)
{
digitalWrite(ledPin2, HIGH);
delay(200);
digitalWrite(ledPin2, LOW);
delay(200);
}
else if (inches > 0 && inches <=6)
{
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin5, HIGH);
delay(200);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);
delay(200);
}
else if (inches > 6 && inches <=12)
{
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
delay(200);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
delay(200);
}
else if (inches > 12 && inches <=18)
{
digitalWrite(ledPin3, HIGH);
delay(200);
digitalWrite(ledPin3, LOW);
delay(200);
}
else
{
digitalWrite(ledPin2, HIGH);
delay(1000);
digitalWrite(ledPin2, LOW);
delay(1000);
}

//  end main loop
}

// conversion from PING sketch
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}

// end code
``````

All I get is the long slow blink on LED 2. Even with my hand right in front of the sensor. I don’t get any reaction on the PING itself, but I don’t know if I am supposed to. I think the LED behavior indicates everything has fallen through to the last ELSE condition.

The PING is getting 5v- I see it blink right before LED 2 comes on so I am pretty sure it is reading. The PING part of the code is straight from the PING example sketch.

I looked around the Web and this guy http://iwearshorts.com/portfolio/diy-parking-sensor/ seems to be doing something roughly along this line, but I didn’t get any insight on my error.

I don’t see the problem. The code complies, uploads, and runs. I think the PING works. The LEDs work. My multimeter says I have 5v on the bread board and to the Arduino.

Ideas?

if (inches = 0) if (inches == 0)

LarryD,

Looked at your post and said to myself "I am going to be so pissed off if it one lousy little equals sign that is hanging me up".

I am so pissed off right now. LOL.

Thank you for the fast and accurate reply.

You win 12 Internets.