Go Down

Topic: ping sensor to 6 led (Read 1 time) previous topic - next topic


Oct 05, 2012, 04:22 am Last Edit: Oct 09, 2012, 03:16 am by newbie22 Reason: 1
i'm a starter in Arduino programs and I wanted to make the pingsensor convert distance to brightness for 6 led lights all at once
I've finished the code with the help of my teacher but the only led that works is pin 5
Is anyone experienced in understanding malfunctions within the code?
Thanks in advance

Code: [Select]

const int PingPin = 7;     //sets signal pin for the PING

int ledPins[] = {3,5,6,9,10,11};

int brightness = 0;

void setup()


for(int i = 0; i <= 6; i++)
 Serial.begin(115200); //Sets up serial should you need it

void loop()
  int i = 0; i <= 6; i++;
 digitalWrite(ledPins[i], LOW);

int cm = findDistance();  //calls down to the code block at the bottom of the sketch to run the Ping and find CM

if (cm >50 || cm ==0)


   digitalWrite(ledPins[i], LOW);



  int cm = findDistance();




  brightness = map(cm, 50, 0, 0,255);


 Serial.println("cm, ");

 Serial.print("brightness = ");


 analogWrite(ledPins[i], brightness);


 int cm = findDistance();



// When this block of code is called, it runs the PING sensor and returns CM

int findDistance()


 long duration, inches, cm;

 pinMode(PingPin, OUTPUT);

 digitalWrite(PingPin, LOW);


 digitalWrite(PingPin, HIGH);

 delayMicroseconds(5);          // The same pin is used to read the signal from the PING))): a HIGH

 digitalWrite(PingPin, LOW);   // pulse whose duration is the time (in microseconds) from the sending

 pinMode(PingPin, INPUT);   // of the ping to the reception of its echo off of an object.

 duration = pulseIn(PingPin, HIGH);

 cm = duration / 29 / 2; // convert the time into a distance

 return cm; // sends CM back to the block that called it



What is the value of i when you use it in the if/else statement?

Also, why are you reading the sensor after you have processed the first read of the sensor.  The value is not stored or used, so it is a wasted effort.
Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com


Oct 05, 2012, 12:24 pm Last Edit: Oct 05, 2012, 12:28 pm by AWOL Reason: 1
Code: [Select]
int i = 0; i <= 6; i++;
Declare a variable i, and assign it the value 0.
Test to see if i is less than or equal to six, and discard the result.
Increment i.

Did you forget a "for", perhaps? (if you did, that should be "i < 6")
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131