Led Counter

Hey guys im working on a small project using a tilt switch. Basically theres for LED’s and a button. Every time i press the the button it should light up the equivilant amount of LED’s couting up to four. So for example 1 push 1 led, 2 button pushes 2 leds and so on.The next step will be to have the tilt switch restart the program from scratch and turn all the LED’s off.

This is what i have so far but the problem is when i run the code on my board it starts off with 2 LED’s after one click instead of just one.

int ledPins={ //Create array for Led pin values
int currentLed=0; //Current Led used to locate the correct LED to light
int direction=1; //Direction will be reversed (* -1) to reverse lighting direction
int buttonPin = 2; //Button Pin in 13

int previous = LOW; // the previous reading from the input pin
int tSwitch=7; // Input pin for Tilt switch

void setup()

pinMode(tSwitch, INPUT_PULLUP); //Enable the internal Pull-Up resistor

pinMode(buttonPin, INPUT); //Set up input stream from the button

for (int i=0; i < 4; i++) //For all LEDs in the array
pinMode(ledPins*,OUTPUT); //Set up LED output *

  • }*
    void loop()

  • if (digitalRead(buttonPin) == HIGH) //If the button has been pressed*

  • {*

  • digitalWrite(ledPins[currentLed],HIGH); //Turn the current LED off*

  • currentLed += direction; // +1 on to the Current LED moving it to the next LED*

  • digitalWrite(ledPins[currentLed],HIGH); //Now turn this new current LED on*

  • delay(250);*

  • }*

  • if(digitalRead(tSwitch)!=previous)*

  • {*

  • for (int i=0; i < 4; i++) {*
    _ digitalWrite(ledPins*,LOW);_
    currentLed = 0;*_

* }*

Any help would be appreciated!

You should post your code between tags of "and"

It makes it more readable.

Your tilt switch is INPUT_PULLUP so I presume when it is active you ground the input pin, how is your button pin wired? (Ideally it should be the same way in which case you are looking for a button low)

    digitalWrite(ledPins[currentLed],HIGH);    //Turn the current LED off
    currentLed += direction;                  // +1 on to the Current LED moving it to the next LED
    digitalWrite(ledPins[currentLed],HIGH);   //Now turn this new current LED on

One of those digitalWrites should be LOW?

Ah so simple! thank you so much :)