lighting LED

finished

I don’t currently have access to an arduino so can’t test. But see how this goes

int ledPins[]={
  0,2,4,6};
int currentLed=0; 
int direction=1;

int buttonPin = 13;


void setup()
{
  for (int i=0; i < 4; i++)
  {
    pinMode(ledPins[i],OUTPUT);
  }
pinMode(buttonPin, INPUT);

}

void loop()
{
  if (digitalRead(buttonPin) == HIGH)
  {
    digitalWrite(ledPins[currentLed],LOW);
    currentLed += direction;
    digitalWrite(ledPins[currentLed],HIGH);
    if((currentLed==0)||(currentLed==3))  
      direction *= -1;
    delay(250);
  }
}

Thanks for the code KenF, i've just tried it out and it works great. I'm working on a similar project which requires a tilt sensor. Once the tilt sensor is activated all the leds should turn off and the array should start from the beginning again. Do you have any idea how this would work?

If you just want one LED to light then use a loop to turn them all off followed immediately by turning the desired LED on.

…R

Arsenal26:
Thanks for the code KenF, i’ve just tried it out and it works great. I’m working on a similar project which requires a tilt sensor. Once the tilt sensor is activated all the leds should turn off and the array should start from the beginning again. Do you have any idea how this would work?

I’d have thought that All you need to do is detect the tilt switch and then recreate the same conditions that existed at start up.

something like this

int tiltSwitch=7;//or whatever
int ledPins[]={
  0,2,4,6};
int currentLed=0; 
int direction=1;

int buttonPin = 13;


void setup()
{
//Assume tilt swith is going to connect to ground when active
pinMode(tiltSwitch, INPUT_PULLUP);

  for (int i=0; i < 4; i++)
  {
    pinMode(ledPins[i],OUTPUT);
  }
pinMode(buttonPin, INPUT);
}

void loop()
{
  if (digitalRead(buttonPin) == HIGH)
  {
    digitalWrite(ledPins[currentLed],LOW);
    currentLed += direction;
    digitalWrite(ledPins[currentLed],HIGH);
    if((currentLed==0)||(currentLed==3))  
      direction *= -1;
    delay(250);
  }
  
if(digitalRead(tiltSwitch)==LOW)
  {//Just recreate conditions of startup
    digitalWrite(ledPins[currentLed],LOW);
    currentLed=0;
    direction=1;
  }
}

Please no cross-posting, KenF gave you a working code.

Do not cross-post. Threads merged.