Alright, what's wrong?

So I am just beginning to use arduino. I am trying to make a code but in the if statement, the outputs continue to be HIGH.

Here’s my question:
Can someone find the error and also make the digitalWrite for pin 2 to 7 in fewer lines.

Please compile a new code.

int timer = 100;
const int ledPins = {
2, 7, 4, 6, 5, 3 };

int pinCount = 6;

const int analogPin = 0;
const int threshold = 500;
const int ledPin = 2;
const int ledPin1 = 3;
const int ledPin2 = 4;
const int ledPin3 = 5;
const int ledPin4 = 6;
const int ledPin5 = 7;

void setup() {
int thisPin;
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
pinMode(ledPin, OUTPUT);

}
}

void loop() {
int analogValue = analogRead(analogPin);

if (analogValue > threshold) {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPin,LOW);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
while (digitalRead(ledPin) == HIGH) {
}
}
}
else {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
}
}

// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin–) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
delay(10000);
digitalWrite(ledPin,HIGH);
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,HIGH);
digitalWrite(ledPin4,HIGH);
digitalWrite(ledPin5,HIGH);
delay(100);

}
}

Thanks

You need to make some changes first. Then, post your modified code.

You have an array of pin numbers:

const int ledPins[] = {
 2, 7, 4, 6, 5, 3 };

You also have a bunch of variables with pin numbers:

const int ledPin = 2;
const int ledPin1 = 3;
const int ledPin2 = 4;
const int ledPin3 = 5;
const int ledPin4 = 6;
const int ledPin5 = 7;

You set the pinMode using the array:

for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
   pinMode(ledPins[thisPin], OUTPUT);

Then, you set the pins LOW strangely:

 for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPin,LOW);
digitalWrite(ledPin1,LOW);  
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);

What happened to the array?

What are you expecting this code to do?

while (digitalRead(ledPin) == HIGH) {
 }

Remember that you set the pin to be an OUTPUT pin. Reading from an output pin is like listening to a microphone. Won’t hear a whole lot.

So

You have an array of pin numbers:
Code:
const int ledPins = {
2, 7, 4, 6, 5, 3 };

You also have a bunch of variables with pin numbers:
Code:
const int ledPin = 2;
const int ledPin1 = 3;
const int ledPin2 = 4;
const int ledPin3 = 5;
const int ledPin4 = 6;
const int ledPin5 = 7;

Can I use the array and set all those pins to HIGH or LOW?
That is why I have all the variables.

What are you expecting this code to do?
Code:
while (digitalRead(ledPin) == HIGH) {
}

I resolved that.

Remember that you set the pin to be an OUTPUT pin. Reading from an output pin is like listening to a microphone. Won’t hear a whole lot

How do I make it work then?

I want the WhileStatement to activate pin 13 when at the end all the pins are HIGH.

digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,HIGH);
digitalWrite(ledPin4,HIGH);
digitalWrite(ledPin5,HIGH);

Thanks for the quick response.

The new code:

int timer = 100;
const int ledPins = {
2, 7, 4, 6, 5, 3 };

int pinCount = 6;

const int analogPin = 0;
const int threshold = 500;
const int ledPin = 2;
const int ledPin1 = 3;
const int ledPin2 = 4;
const int ledPin3 = 5;
const int ledPin4 = 6;
const int ledPin5 = 7;
const int Endpin = 12;
void setup() {
int thisPin;
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
pinMode(ledPin, OUTPUT);

}
}

void loop() {
int analogValue = analogRead(analogPin);

if (analogValue > threshold) {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPin,LOW);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
while (digitalRead(ledPin) == HIGH) {
digitalWrite(Endpin,HIGH);
}
}
}
else {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
}
}

// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin–) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
delay(10000);
digitalWrite(ledPin,HIGH);
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,HIGH);
digitalWrite(ledPin4,HIGH);
digitalWrite(ledPin5,HIGH);
delay(100);

}
}

Can I use the array and set all those pins to HIGH or LOW?

Yes, you can.

while (digitalRead(ledPin) == HIGH) {
 digitalWrite(Endpin,HIGH);
 }

You can't read from an OUTPUT pin.

Can I use the array and set all those pins to HIGH or LOW? Yes, you can.

HOW? Can you make an example?

You can't read from an OUTPUT pin.

I understand but how must I change my code?

HOW?
Can you make an example?

Exactly like you set the pinMode.

for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin],LOW);

I understand but how must I change my code?

That depends on what you are trying to do. It is not necessary to read the mode of a pin you just set. It is what you set it to. If you need to keep track of whether the pin is HIGH or LOW, use an array. You must have your code remember the setting. You can’t read it.