1
  1. When I press the button once, sequence s1() plays.
  2. When I press the button twice, sequence s2() plays.
  3. When I press the button thrice, sequence s3() plays.
  4. When I press the button 4 times, sequence s4() plays.

const int buttonPin = 10;  
int buttonState = 0;

void setup(){
  Serial.begin(9600);
  //initialize pin 2 - 9 as output
  for(int i=2;i<=9;i++){ 
    pinMode(i,OUTPUT);
  }
  pinMode(buttonPin, INPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    s1();
    s2();
    s3();
    s4();
  }
}

void s1() {
  int i = 2 ;
  for(int z=0;z<=1;z++) {
    for(int x=i;x<=9;x++) {
      digitalWrite(i,HIGH);
      digitalWrite(x-1,LOW);
      delay(500);
      i = i + 1;
    }
    i = 2;
    if(i = 2) {
      digitalWrite(9,LOW);
    }
  }
}

void s2(){
  int y = 9;
  for(int i=0;i<=1;i++) {
    for(int x=2;x<=5;x++) {
      if(y>=1) {
        digitalWrite(x,HIGH);
        digitalWrite(y,HIGH);
        delay(500);
        y = y - 1;
      }
    }
    off();
    y = 9;
    delay(500);
  }
}

void s3() {
  for(int i=0;i<=2;i++) {
    for(int z=2; z<=9;z++) {
      if(z % 2 == 0) {
        digitalWrite(z,HIGH);
      }
    }
    delay(500);
    for(int z=2; z<=9;z++) {
      if(z % 2 == 0) {
        digitalWrite(z,LOW);
      }
    }
    delay(500);
    for(int z=2; z<=9;z++) {
      if(z % 2 != 0) {
        digitalWrite(z,HIGH);
      }
    }
    delay(500);
    for(int z=2; z<=9;z++) {
      if(z % 2 != 0) {
        digitalWrite(z,LOW);
      }
    }
    delay(500);
  }
}

void s4() {
  for(int i=0;i<=2;i++) {
    for(int x=2;x<=9;x++) {
      digitalWrite(x,HIGH);
      delay(500);
    }
    off();
    delay(500);
  }
}

void off() {
  for(int i=2;i<=9;i++) {
    digitalWrite(i,LOW);
  }
}
3
  • You should tell us the preblem you are having.
    – sassoPera
    Commented Sep 8, 2016 at 12:55
  • 2
    You didn't like my solution on Stack Overflow so you have to crosspost this question here?
    – KIIV
    Commented Sep 8, 2016 at 13:22
  • Someone suggested on his question stackoverflow.com/questions/39388020/… that he take it here. However cross-posting is normally discouraged.
    – Nick Gammon
    Commented Sep 9, 2016 at 3:00

2 Answers 2

1

That's not that complex, please have a look at the following code: not the most elegant solution ever, but should be working (I didn't compile it as I'm currently on launch break in the office)

#define LONGEST_PAUSE 1000
unsigned long lastClick;
unsigned int counter;

void loop() {
  if (digitalRead(buttonPin) == HIGH) {
    // button is pressed
    while(digitalRead(buttonPin) == HIGH)) {
      delay(1); // wait until button released
    }
    lastClick = millis();
    counter++;
  }
  if (counter > 0 && (millis() - lastClick) >= LONGEST_PAUSE) { // 1s passed since last click: let's play!
    switch(counter) {
      case 1:
        s1(); break;
      case 2:
        s2(); break;
      case 3:
        s3(); break;
      case 4:
        s4(); break;
      default:
        // what if I keep clicking?!?
    }
    counter = 0; // reset the counter
  }
}
3
  • is there a way to break the loop when i press the button when the loop is running?
    – eleos
    Commented Sep 8, 2016 at 13:49
  • The OP apparently wishes to (and I'm quoting him) "break the loop when I press the button the the loop is running" While this might be a question on it's own... NO, if you exit the loop() function then (by default) your Arduino stops doing anything and just sits there dissipating energy into heat, not much though. With some little effort you can instruct it to reboot. Commented Sep 8, 2016 at 20:58
  • 1
    If my answer solved the problem stated into your question you should accept it and create a new question accordingly to your updated needs. Commented Sep 8, 2016 at 22:42
0

I prefer you to use pinchange interrupt or simple INT0 interrupt for the buttons to count the press. Check this link.

on ISR, you can count the press.

int count=0;

void setup() {
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), countFn, CHANGE);
}

void loop() {
  switch(count){
      // all cases here
  }
  count=0;
  delay(2000); // this delay is important to give enough time for a number of presses for the user
}

void countFn() {
  count++;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.