Pause Adruino with push button

Hi,

I’m a new Arduino owner. I have a fair understanding of programming with loops in Visual Basic, but the C++ syntax is still very strange to me.

I’ve worked through the beginner tutorials that came with my Arduino kit an decided to experiment by combining two tutorials - the one tutorial makes a single LED glow when the board gets digital input from a push button connected to 5V. The other tutorial simulates a LED chase effect between 6 LED’s. I tried to make the board wait for me to press the button before it starts the loop which switches the LED’s on and off. I works in that the LED’s cycles on and off while I hold in the button. What surprised me however, is that all 6 the LED’s light up an stay on when I power up the board or load the sketch. Please can someone explain to me why this happens as the only place where the instruction is given to power any LED is after the button is pressed. (Sketch is below…)

Thanks in advance!

Johan

int BASE = 2;
int NUM = 6;
int inpin = 8;
int val;
void setup()
{
  pinMode(inpin, INPUT);
  for (int i = BASE; i < BASE + NUM; i++)
  {
    pinMode(i, OUTPUT);
  }
}

void loop()

// tried this first For loop to switch everything off which might be on
{
  for (int i = BASE; i < BASE + NUM; i++)
  {
    digitalWrite(i, 0);
    delay(200);
  }

  val = digitalRead(inpin);
  if (val == 1)
  {
    for (int i = BASE; i < BASE + NUM; i++)
    {
      digitalWrite(i, 0);
      delay(200);
    }
    for (int i = BASE; i < BASE + NUM; i++)
    {
      digitalWrite(i, 1);
      delay(200);
    }
  }
  else
  {
    for (int i = BASE; i < BASE + NUM; i++)
    {
      digitalWrite(i, 0);
      delay(200);
    }
  }
}

Try these changes:

pinMode(inpin, INPUT);

to

pinMode(inpin, INPUT_PULLUP);

and

if (val == 1)

to

if (val == 0)

and wire your button to connect the pin the Gnd when pressed.

Thanks for the quick response CrossRoads! I’m not sure if I understand you correctly on the wiring of the button? It now starts cycling the LED’s without any button input. (I would like everything to remain off untill I press the button.) The button currently connects to Gnd via a 10K resistor whn pressed - is that right?

What makes the button go High then? Using the INPUT_PULLUP method holds the pin High until the button is pressed. Once the button is released it goes high again. With just the external 10K pulldown, there is nothing to make the pin go high and it just floats around, with a read maybe coming back as a high and maybe as a low when the button is not pressed.

I must add - I'm a complete novice when it comes to electronics! I will have to go reead up on the INPUT_PULLUP function and how it is used.

Let me get the original sketch with a wiring diagram perhaps for you tomorrow (in my part of the world it is 23:30 now!) so that you can see where I'm coming from...

Thanks for your help and patience! I'm on a steep learning curve here!

Here’s the circuit diagram for the push button…

The sketch that came with it, is looking like this…

int ledpin = 11;
int inpin = 8;
int val;
void setup()
{
  pinMode(ledpin, OUTPUT);
  pinMode(inpin, INPUT);
}

void loop()
{
  val = digitalRead(inpin);
  if (val == 0)
  {
    digitalWrite(ledpin, 0);
  }
  else
  {
    digitalWrite(ledpin, 1);
  }
}