Is it possible to read a digital input from an array without using the exact element? Such as
while(digitalRead(myButtonArray[i]))
digitalWrite(myLEDArray[i], HIGH);
Reading the myButtonArray one by one and turning on the led one by one doesnt make for clean written code. The following is just an example code. When button on pin 2 which is element 0 is pressed turn on led on pin 7 and turn off when released. When button on pin 3 is pressede turn on led on pin 8 and turn off when released and continue on in the same fashion with the rest of the buttons and leds as well.
int myButtonArray[] = {2,3,4,5,6};
int myLEDArray[] = {7,8,9,10,11};
void setup() {
for(int i = 0;i<5;i++){
pinMode(myButtonArray[i], INPUT);
pinMode(myLEDArray[i], OUTPUT);
}
}
void loop() {
while(digitalRead(myButtonArray[0]))
digitalWrite(myLEDArray[0], HIGH);
while(digitalRead(myButtonArray[1]))
digitalWrite(myLEDArray[1], HIGH);
while(digitalRead(myButtonArray[2]))
digitalWrite(myLEDArray[2], HIGH);
while(digitalRead(myButtonArray[3]))
digitalWrite(myLEDArray[3], HIGH);
while(digitalRead(myButtonArray[4]))
digitalWrite(myLEDArray[4], HIGH);
}
4 Answers 4
Is this what you want?
#define NUM_ITEMS 5
int myButtonArray[NUM_ITEMS] = {2,3,4,5,6};
int myLEDArray[NUM_ITEMS] = {7,8,9,10,11};
void setup() {
for(int i = 0;i<NUM_ITEMS;i++){
pinMode(myButtonArray[i], INPUT);
pinMode(myLEDArray[i], OUTPUT);
}
}
void loop() {
for (int i=0 ; i<NUM_ITEMS ; ++i) {
while(digitalRead(myButtonArray[i]))
digitalWrite(myLEDArray[i], HIGH);
digitalWrite(myLEDArray[i], LOW);
}
}
-
This code does the job I was looking for exactly. Thanks for your help.1fastk– 1fastk2017年01月03日 22:09:43 +00:00Commented Jan 3, 2017 at 22:09
-
You're welcome. Worth spending some time understanding the other answers as well because there are some interesting points made.Mark Smith– Mark Smith2017年01月03日 22:14:21 +00:00Commented Jan 3, 2017 at 22:14
I think, you did not realized, that digitalWrite(i,value)
sets the pin i
to value
AND the pin have the value
until set otherwise. So you probabelly want something like this:
void loop() {
for (int i=0;i<5;i++)
digitalWrite(myLEDArray[i], digitalRead(myButtonArray[i]));
}
where each LED follows each respective Button both HIGH and LOW.
It isn't clear from the question or the code what you actually want to do. Perhaps back up a step: edit the question and write a clear-cut problem specification.
For example, a specification for what the code you have actually does, is:
For each of five buttons,
If button k is on, turn on LED k, and stall in a loop until button k is released. Leave LED k on when the button is released.
-
I made an edit so hopefully it is easier to understand now.1fastk– 1fastk2017年01月03日 21:07:07 +00:00Commented Jan 3, 2017 at 21:07
-
@1fastk: It is still unclear whether the "stall in a loop" behavior is a desired feature or a bug in your code.Edgar Bonet– Edgar Bonet2017年01月03日 21:13:55 +00:00Commented Jan 3, 2017 at 21:13
-
@EdgarBonet: Im not exactly sure what you mean by "stall in a loop" but what I am trying to achieve is to not have to have multiple digitalRead and digitalWrite statement for every element inside of the arrays. I want to just have only one digitalRead statement to read which button is HIGH inside of the array and then to have one digitalWrite statement to write the led HIGH of the same element. If the element 3 goes high on myButtonArray[] to write element 3 in the myLEDArray HIGH as well. I hope this make better since.1fastk– 1fastk2017年01月03日 21:32:21 +00:00Commented Jan 3, 2017 at 21:32
-
1I mean that while the first button is
HIGH
, your program does not check the other buttons, as it is stuck inside awhile
loop.Edgar Bonet– Edgar Bonet2017年01月03日 21:41:45 +00:00Commented Jan 3, 2017 at 21:41
In your question you wrote
and turn [the LED] off when [the corresponding button is] released
and yet you never turn the LEDs off in your code. Then, I assume the code in your question is irrelevant, and you just want the LEDs to show the states of the buttons at all times. This can be done by writing to the LEDs the values you read from the buttons. Do this for each button/LED pair, and loop over the pairs:
void loop() {
for (int i = 0; i < 5; i++)
digitalWrite(myLEDArray[i], digitalRead(myButtonArray[i]));
}
-
Yes the code here is irrelevant, I wrote it just for a quick example to see if it was possible to digitalRead from a array and then to digitalWrite to an LED in another array of the same element. Thanks for your help1fastk– 1fastk2017年01月03日 23:17:54 +00:00Commented Jan 3, 2017 at 23:17
for
loop.