LED Changes on first loop

Hi,
I've used this as a base to test my setup. http://www.instructables.com/id/Arduino-Button-Tutorial/ this originally worked fine but I've modified it to have 2 buttons and 2 LED's.

The problem I have is that on the first loop 1 LED changes to ON and I can not figure out why. In the setup I initialize them as LOW and the button has not been pressed so it shouldn't change.

Here is my code.

int Buttons[] = { 0, 11, 15};
int LEDs[] = { 0, 9, 7 };
int numofButtons = 2;
int numofLEDs = 2;

int j;
int i;
int k;
int l;
int m;

#define DELAY            50  // Delay per loop in ms

//////////////////////////////////////////////////////////////////////////////
//   Setup Buttons

int buttons_now_pressed[] = {};
boolean buttons_was_pressed[] = { false, false, false }; // previous state
boolean events[] = { 0, 1, 2 };
boolean raising_edge[] = {};

int running;

void setup()
{
  Serial.begin(9600);
  for (j = 1; j < (numofButtons); j++)
  {
    pinMode(Buttons[j], INPUT);
    digitalWrite(Buttons[j], LOW); // push-down
    Serial.println("Initialize Button " + j);
  }
  for (i = 1; i < (numofLEDs); i++)
  {
    pinMode(LEDs[i], OUTPUT);
    digitalWrite(LEDs[i], LOW);
    Serial.println("Initialize LEDs " + i);
  }
  for (k = 1; k < (numofButtons); k++)
  {  
    buttons_was_pressed[k] = false;
    Serial.println("Initialize  Buttons_was_pressed " + k);
  }
}

boolean handle_button()
{
  for (l = 1; l < (numofButtons); l++)
  {
    buttons_now_pressed[l] = !digitalRead(Buttons[l]); // pin low -> pressed
  
    events[l] = buttons_now_pressed[l] && !buttons_was_pressed[l];

    buttons_was_pressed[l] = buttons_now_pressed[l];
  }
}

void loop()
{
  delay(5000);
  // handle button
  handle_button();
//  Serial.println("Start Loop");
  for (m = 1;m < (numofButtons); m++)
  {
    raising_edge[m] = events[m];
  
    Serial.print(raising_edge[m]);
    // do other things
    Serial.print(raising_edge[m] ? "^" : ".");
    if (raising_edge[m] == true)  
    {
      running = digitalRead(LEDs[m]);
      running = !running;
      Serial.print(" Change LED" + String(m) + " ");
      digitalWrite(LEDs[m], running);
    }

    // add newline sometimes
    static int counter = 0;
    if ((++counter & 0x3f) == 0)
      Serial.println();

    delay(DELAY);
  }
}
int Buttons[] = { 0, 11, 15};
int LEDs[] = { 0, 9, 7 };
int numofButtons = 2;
for (j = 1; j < (numofButtons); j++)

So why have your arrays got three elements?
How many times through that loop?

Serial.println("Initialize Button " + j);

?

I got that from another example I was using to put me in the right direction and because the for loops are starting at 1 it doesn't ever use the first element in each array.

Cheers

  for (j = 1; j < (numofButtons); j++)

You have three elements in the array, with positions numbered 0, 1, and 2. You have numOfButtons (uselessly surrounded by parentheses) set to 2. So, your loop sets the state of one pin.

 for (i = 1; i < (numofLEDs); i++)

Same problem here.

 for (k = 1; k < (numofButtons); k++)

And here.

And this:

int j;
int i;
int k;
int l;
int m;

is plain stupid. None of these variables need to be global. Global variable names should NOT be one letter names. Ever!

PaulS, thanks for your help on this. I have added a '+1' after each of the above like for (j = 1; j < (numofButtons +1); j++) I had forgotten to add these back in as they were originally there but removed while testing. This was why the brackets were there.

This did fix that only 1 button and 1 LED were actually being initialized in the setup, but it hasn't fixed the main issue of LED1 turns on during the first loop.

Here is the output I see.

Initialize Button 
Initialize Button 
Initialize LEDs 
Initialize LEDs 
Initialize  Buttons_was_pressed 
Initialize  Buttons_was_pressed 
1^ Change LED1 0.0.0.0.

I would expect the last line to be

0.0.0.0.0.0.
pinMode(Buttons[j], INPUT);
digitalWrite(Buttons[j], LOW); // push-down

Yeah, but will it ↴∿∿∿∿↗ // stay-down?

Hi Guys,
Got it sorted but some replies anyway.

I cleaned up the code a bit as PaulS mentioned.

dlloyd I have a pull down resistor to force it to stay down.

Cheers

Get into good habits:

const int numofButtons = 2;
const int numofLEDs = 2;
int Buttons[numofButtons] = { 11, 15 };
int LEDs[numofLEDs] = { 9, 7 };

...

  for (j = 0; j < numofButtons; j++)
...