Need Help

i am trying to make a led pattern Halt at its current stage when button is pressed but when i press th button all led’s go off here’s my code plz help.

//led Halt on Button press
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int button = 8;
int buttonstate;
int x; //store number of led on
int i; //for loop
void ledpattern();
void patternhalt();

void setup() {
// put your setup code here, to run once:
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(led5,OUTPUT);
pinMode(button,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(button,HIGH);
buttonstate = digitalRead(button);

if(buttonstate == LOW){
//halt led patterns
void patternhalt();
delay(2000);
buttonstate = digitalRead(button);
}else{
//continue led pattern
ledpattern();
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
digitalWrite(led5,LOW);
}

}

void ledpattern() {
for(int s = 0; s <= 7 ; s++){
x = s;
digitalWrite(s,HIGH);
delay(100);
}
}

void patternhalt(){
for(int t = 0 ; t<=x;t++){
digitalWrite(t,HIGH);
}
}

Please edit your post and put the code into code-tags (the little </> button in the top left). Why do you expect the pattern to be “halted” when you explicitly tell it to turn of the leds?

void patternhalt(){
  for(int t = 0 ; t<=x;t++){
    digitalWrite(t,HIGH);
  }
}

Just do nothing to halt the pattern (i.e. remove the call to patternhalt).

Also what kind of button is this and why are you writing to it? What exactly do you want to achieve with the code?

harsimran29:
i am trying to make a led pattern Halt at its current stage when button is pressed but when i press th button all led’s go off here’s my code plz help.

your patternhalt() function to me seems unnecessary based on what you said you want to achieve.

maybe something like this would work for you…

//led Halt on Button press
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int button = 8;
int buttonstate;
int x; //store number of led on
int i; //for loop

void setup() {
  // put your setup code here, to run once:
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(button, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(button, HIGH);
  buttonstate = digitalRead(button);

  if (buttonstate == LOW) {
    while (buttonstate == LOW) {
      buttonstate = digitalRead(button);
      delay(100);
    }
  } else {
    //continue led pattern
    ledpattern();
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led5, LOW);
    delay(100);
  }

}

void ledpattern() {
  for (int s = 0; s <= 7 ; s++) {
    x = s;
    digitalWrite(s, HIGH);
    delay(100);
  }
}

hope that helps.

void patternhalt()
{
  for (int t = 0 ; t <= x; t++)
  {
    digitalWrite(t, HIGH);
  }
}

How are the LEDs wired ?

In loop() you put void in front of patternhalt() but not in front of ledpattern(). Why? What do you think the void keyword does?

And if your LEDs are on pins 2-7 why do the for loops that write to them start from 0?

But the main problem is that you don't seem to realise that a for loop loops round until it's finished. You can't break into it by reading a pin from outside the loop because the read never happens until the for loop is complete.

Steve