Pages: [1]   Go Down
Author Topic: ping sensor to 6 led  (Read 703 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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
« Last Edit: October 08, 2012, 08:16:21 pm by newbie22 » Logged

Sydney, Australia
Online Online
Edison Member
Karma: 27
Posts: 1185
Big things come in large packages
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Parola hardware & library

Global Moderator
Offline Offline
Brattain Member
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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")
« Last Edit: October 05, 2012, 05:28:08 am by AWOL » Logged

"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.

Pages: [1]   Go Up
Jump to: