1

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);     
 }
2
  • You want a for loop. Commented Jan 3, 2017 at 20:51
  • The AVR chip has operations that affect multiple pins at once, however this is guided/limited by their organization into ports at hardware level; the Arduino framework slaps numbers on these that bridge the divisions between the several distinct ports, and then gives you "one pin at a time" functions that hide all that detail. I don't believe there are any Arduino-type bulk methods short of using the individual methods in a loop, and targeting the actual AVR port registers is usually only recommended where speed, rather than cleanliness, is the goal. Commented Jan 4, 2017 at 19:34

4 Answers 4

0

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);
     }
 }
2
  • This code does the job I was looking for exactly. Thanks for your help.
    – 1fastk
    Commented 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
    Commented Jan 3, 2017 at 22:14
1

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.

0

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.

4
  • I made an edit so hopefully it is easier to understand now.
    – 1fastk
    Commented 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. Commented 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
    Commented Jan 3, 2017 at 21:32
  • 1
    I mean that while the first button is HIGH, your program does not check the other buttons, as it is stuck inside a while loop. Commented Jan 3, 2017 at 21:41
0

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]));
}
1
  • 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 help
    – 1fastk
    Commented Jan 3, 2017 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.