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++)
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
}