Array problem

I have two arrays. Selection of Array1() and Array2() is selected by a switch being held down. i.e Button released (modePin/high) selects Array(1) and modePin held down (low) selects Arrary2()

I get no problems with the first loop for Array1, however when modePin is made low then when I press any 1 of 5 buttons the serial monitor cycles through all Array2 values instead of the one being selected by a button.

int buttonPins[6] = {7, 8, 9, 10, 11}; // Pins used for buttons
int modePin = 12; // Hold to select OSD operation
int MyArray1[5] = {10, 11, 12, 13, 14 };
int MyArray2[5] = {15, 16, 17, 18, 19};
int nbts = 5; // Number of buttons
int i;

void setup()
{

  for (i = 0; i < nbts; i = i + 1)
  {
    pinMode(buttonPins[i], INPUT_PULLUP);
  }

  pinMode(modePin, INPUT_PULLUP);
  digitalWrite(modePin, HIGH);

  Serial.begin(9600);
}

void loop()
{

  /* if (digitalRead(modePin) == HIGH)
     for (int i = 0;  i < nbts-1; i++)
     {
       if (digitalRead(buttonPins[i]) == LOW)
       {
         Serial.println(MyArray1[i]);   // Send to Serial monitor
         delay(50);
       }
     }
  */

  if (digitalRead(modePin) == LOW)
    for (int i = 0;  i < nbts; i++)
    {
      if (digitalRead(buttonPins[i]) == LOW)
      {
        Serial.println(MyArray2[i]);   // Send to Serial monitor
        delay(50);
      }

    }

}

In the second loop if I change modePin to HIGH it works perfectly showing selected Array1 values for each button.

How is whatever is connected to modePin actually wired?

Why does one (the commented out one) of the for loops iterate while i is less than nbts minus 1, while the other (the one actually being executed) iterates while is is less than nbts?

Hello Paul,

Sorry I will try to explain.

There are 5 pushbuttons on a panel mounted as what the 5 would look like on a dice. They represent Up/Down/Left/Right. They are for moving a camera pan/tilt. The middle button is only to get used in the second loop, it's a menu button for setting settings on the camera. When the button modePin is selcted then the five buttons get used for OSD (On screen display camera settings)

So the first loop is for 4 buttons, the second loop is for the 5 buttons, but only when the modePin switch is pressed. I have checked that the modePin does get made low when pressed.

Hope thats a bit clearer. I am basically trying to get 2 uses for the button arrays. They get sent by wireless as remote functions. Regards

I have gone round in circles, my problem is definately in this part of the code. Maybe there is another way to write this. I just want to be able to press a button using it's index value to send an item from an array. As it is at the moment the buttons index "i" is not just sending MyArray2(i) to the serial monitor.

I am trying to use an array instead of giving all buttons a name and doing an if, else bit to find which button is pressed.

Thanks

I hate anonymous printing. Print some identification in front of the value being printed. You need to print the state of the modePin, the value in i, and the value of buttonPins[ i ] to really understand what the code is doing.

I tried to re-create your problem with the following code:

  byte buttonState[] = {HIGH, LOW, HIGH, HIGH, HIGH};
  if (true )
    for (int i = 0;  i < nbts; i++)
    {
      if (buttonState[i] == LOW)
      {
        Serial.println(arrayPtr[i]);   // Send to Serial monitor
        delay(50);
      }

    }

but it behaved as expected, only printing one number. I did notice that you are mixing up the old and new ways to set input pullup, you don;t need to write HIGH to the mode pin. I can’t see how that would explain your problem, though.

You might try pointing to the arrays and setting the pointer based on the mode pin, then the code will run the same loop no matter what.

int buttonPins[6] = {7, 8, 9, 10, 11}; // Pins used for buttons
int modePin = 12; // Hold to select OSD operation
int MyArray1[5] = {10, 11, 12, 13, 14 };
int MyArray2[5] = {15, 16, 17, 18, 19};
int nbts = 5; // Number of buttons
int i;

void setup()
{

  for (i = 0; i < nbts; i = i + 1)
  {
    pinMode(buttonPins[i], INPUT_PULLUP);
  }

  pinMode(modePin, INPUT_PULLUP);
  // digitalWrite(modePin, HIGH);

  Serial.begin(9600);

}

void loop()
{

  int *arrayPtr;
  int nPins;

  if (digitalRead(modePin) == HIGH)
  {
    Serial.println("*** mode pin HIGH ***");
    arrayPtr = MyArray1;
    nPins = nbts - 1;
  } else {
    Serial.print("*** mode pin LOW ***");
    arrayPtr = MyArray2;
    nPins = nbts;
  }

  for (int i = 0;  i < nPins; i++)
  {
    if (digitalRead(buttonPins[i]) == LOW)
    {
      Serial.println(arrayPtr[i]);   // Send to Serial monitor
      delay(50);
    }
  }

}

Many thanks Blue Eyes. I tried your code and it still puts all Array2 values up on serial monito when I hold hown the modePin and press any key.

I tried naking Serial.println(arrayPtr*); to be Serial.println(arrayPtr); thinking it had held one number, but I could not get the right syntax to try. I could not understand why one minute it's not an array unless I am wrong arrayPtr = MyArray2; and then indexed. Thanks*

Anybody know an answer?

ZOR2:
Anybody know an answer?

You apparently did not read reply #4.

Thanks Paul.

Blue Eyes did that but still produced more than 1 array value to be printed to serial monitor.

Both my loops are the same except for the modePin being made low, and written in code to deal with MyArray2.

I think I will forget using an index arrangement and go the long way with button names and buttonstates because what looks like a simple code is causing so much problem in sorting.

I think I will forget using an index arrangement and go the long way with button names and buttonstates because what looks like a simple code is causing so much problem in sorting.

The code you have SHOULD work. If it doesn't, then you have a problem with how you are diddling with/reading from the pins. It would take less than 10 minutes to sort out the problem, by adding some useful Serial.print() statements.

But, feel free to dump that code, and start over.

I did put other Serial.print() statements in, but all I could see running through the monitor were all array vaues of the Array2 going through. I double checked no pins were floating and that pins were stable lows where they should be.

I have now dumped it and just finishing longhand version. I was hoping my problem would have been spotted by someone as being a simple code error as all I was doing was using the same buttons to relate themselves to either array by just holding down another button (modePin) Never mind soon be on track again. Thanks for help.

Just out of interest try this :

const byte buttonPins[] = {A2, A3}; // Pins used for buttons
const byte modePin = A1;  //mode button
const byte myArray1[] = {99, 100};
const byte myArray2[] = {101, 102};
const byte nbts = sizeof(myArray1) / sizeof(myArray1[0]);

void setup()
{
  for (int i = 0; i < nbts; i++)
  {
    pinMode(buttonPins[i], INPUT_PULLUP);
  }
  pinMode(modePin, INPUT_PULLUP);
  Serial.begin(115200);
}

void loop()
{
  if (digitalRead(modePin) == HIGH)    //mode button not pressed
  {
    for (int i = 0; i < nbts; i++)
    {
      if (digitalRead(buttonPins[i]) == LOW)
      {
        Serial.println(myArray1[i]);   // Send to Serial monitor
        delay(50);
      }
    }
  }
  else                                //mode button is pressed
  {
    for (int i = 0; i < nbts; i++)
    {
      if (digitalRead(buttonPins[i]) == LOW)
      {
        Serial.println(myArray2[i]);   // Send to Serial monitor
        delay(50);
      }
    }
  }
}

Adjust the button pins to match your setup and report what you see in the serial monitor. Needless to say it works on my test system.

Many thanks. I changed my pins to what I have. However still have the problem. On pressing the buttons I get more than 1 value going to the serial monitor. Attached picture shows pressing the button associated to pin 7

As it works OK for me and not for you I am very suspicious of your wiring.

Can you please post a drawing of your wiring taken from the actual wiring rather the theoretical circuit. Is the wiring connected using a breadboard and if so are the longitudinal bus connections continuous or do they have a break in the centre ?

Thanks for the confirmation of it working at your end. I now know what the problem is. It’s buttons pullup. My board with switches has pullup resistors however I stopped using them as everyone kept asking why did I use when there were pullups in the Arduino.

I ran your code and as soon as I took the common resistors to 5 volts your arrary data came out perfectly on the serial monitor. I will also use external resistors now.

Many thanks for your help. Blue Eyes code was showing print detail saying high/low?

Now I can move on. Regards

Thanks Blue Eyes, your code works perfectly now I have external pullup resistors to buttons, causing my problems.

And thanks to Paul for coming in.