Go Down

Topic: ping sensor to 6 led (Read 813 times) previous topic - next topic

newbie22

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++)
pinMode(ledPins[i],OUTPUT);
 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);

   Serial.println("OFF");

   delay(100);

  int cm = findDistance();

 }

else

{



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



 Serial.print(cm);

 Serial.println("cm, ");

 Serial.print("brightness = ");

 Serial.println(brightness);



 analogWrite(ledPins[i], brightness);

 delay(100);

 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);

 delayMicroseconds(2);

 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

}


marco_c

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 http://arduinocode.codeplex.com<br />Parola for Arduino http://parola.codeplex.com

AWOL

#2
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