简体   繁体   中英

Arduino array and if statement

I have a problem. My code for Arduino is:

void count (int a){
  if (a == 0) {
    int led_pin [4] = {3, 4, 5, 6};
  }

  else{
    int led_pin [2] = {4, 5, 6, 9};
  }

  for (int i = 0; i <= 7; i++){
    digitalWrite(led_pin[i], HIGH);
  }
}

and I get output:

'led_pin' was not declared in this scope

How to declared array or change values of array?

You are getting the error because you have bounded the scope of your led_pin variable inside the if and else condition.

Also you are trying to change the whole variable, you should not do that.

Try this out.

void count(int a){
    int led_pin[2][4]={{3,4,5,6},{4,5,6,9}};
    if( a !=0)
    {
        a=1;
    }
    for(int i=0;i<4;i++) //I don't know why you used 7 in your code.
    {
        digitalWrite(led_pin[a][i],HIGH);
    }
}

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM