hello. I am new to programming and i am sorry if this is posted elsewhere. I have been looking for last few days. Been watching videos etc But there is something i am not understanding. I seem to just be repeating the same code over and over in programmes. I started using arrays and for loops to clean up the code. Its all good for the setup section. But when i comes to the loop section i seem to be having difficulty.
At the moment i am just trying to set up eight pushbuttons to each turn on an LED.
Below is the long code that i am trying to clean up. As you can see the loop just repeats the same statement over and over. But the programme works as expected.
int leds[] = {9, 8, 7, 6, 5, 4, 3, 2};
int buttons[] = {10, 11, 12, A0, A1, A2, A3, A4};
// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int buttonState7 = 0;
int buttonState8 = 0;
void setup() {
for(int i; i<10; i++)
{
pinMode(leds[i], OUTPUT);
pinMode(buttons[i], INPUT);
}
}
void loop() {
buttonState1 = digitalRead(buttons[0]); // read the state of the pushbutton value:
if (buttonState1 == HIGH) { // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
digitalWrite(leds[0], HIGH); // turn LED on:
} else {
digitalWrite(leds[0], LOW); // turn LED off:
}
buttonState2 = digitalRead(buttons[1]); // read the state of the pushbutton value:
if (buttonState2 == HIGH) { // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
digitalWrite(leds[1], HIGH); // turn LED on:
} else {
digitalWrite(leds[1], LOW); // turn LED off:
}
buttonState3 = digitalRead(buttons[2]); // read the state of the pushbutton value:
if (buttonState3 == HIGH) { // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
digitalWrite(leds[2], HIGH); // turn LED on:
} else {
digitalWrite(leds[2], LOW); // turn LED off:
}
buttonState4 = digitalRead(buttons[3]); // read the state of the pushbutton value:
if (buttonState4 == HIGH) { // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
digitalWrite(leds[3], HIGH); // turn LED on:
} else {
digitalWrite(leds[3], LOW); // turn LED off:
}
buttonState5 = digitalRead(buttons[4]); // read the state of the pushbutton value:
if (buttonState5 == HIGH) { // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
digitalWrite(leds[4], HIGH); // turn LED on:
} else {
digitalWrite(leds[4], LOW); // turn LED off:
}
buttonState6 = digitalRead(buttons[5]); // read the state of the pushbutton value:
if (buttonState6 == HIGH) { // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
digitalWrite(leds[5], HIGH); // turn LED on:
} else {
digitalWrite(leds[5], LOW); // turn LED off:
}
buttonState7 = digitalRead(buttons[6]); // read the state of the pushbutton value:
if (buttonState7 == HIGH) { // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
digitalWrite(leds[6], HIGH); // turn LED on:
} else {
digitalWrite(leds[6], LOW); // turn LED off:
}
buttonState8 = digitalRead(buttons[7]); // read the state of the pushbutton value:
if (buttonState8 == HIGH) { // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
digitalWrite(leds[7], HIGH); // turn LED on:
} else {
digitalWrite(leds[7], LOW); // turn LED off:
}
}
So amongst other things i tried this. (this one made the most sense to me)
int leds[] = {9, 8, 7, 6, 5, 4, 3, 2};
int buttons[] = {10, 11, 12, A0, A1, A2, A3, A4};
int buttonState[8];
void setup() {
for(int i; i<8; i++)
{
pinMode(leds[i], OUTPUT);
pinMode(buttons[i], INPUT);
}
}
void loop() {
for(int i; i <8; i++)
{
buttonState[i] = digitalRead(buttons[i]); // read the state of the pushbutton value:
for(int i; i<8; i++)
{
if (buttonState[i] == HIGH) { // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
digitalWrite(leds[i], HIGH); // turn LED on:
} else {
digitalWrite(leds[i], LOW); // turn LED off:
}
}
}
}
This however, did not work.
Can anyone offer some suggestions or tell me where i am going wrong please.
Thank you.