help me to code this program
- when i press the button once, sequence s1() play.
- when i press the button twice, sequence s2() play.
- when i press the button thrice, sequence s3() play.
- when i press the button 4 times, sequence s4() play.
//code
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);
}
}
ArK
21.1k67 gold badges113 silver badges136 bronze badges
-
Could you explain your problem better?DamianoPantani– DamianoPantani2016年09月08日 10:53:48 +00:00Commented Sep 8, 2016 at 10:53
-
Just a note, there is a full forum here, on StackOverflow about Arduinos, you might get better/faster help there... This part of the forums are mostly for programming related questions. So, please, read this (how to ask) and this (mcve) before asking, as those will help you get more and better answers from the community.Bonatti– Bonatti2016年09月08日 12:30:03 +00:00Commented Sep 8, 2016 at 12:30
1 Answer 1
You mean something like that? I've used internal pull-up and button as active-low switch so I've used comparing with LOW:
const int btn = 4;
byte btn_tim_on = 0;
byte btn_tim_off = 0;
byte btn_val = 0;
void setup() {
pinMode(btn, INPUT_PULLUP);
Serial.begin(57600); // debug purposes
}
void loop() {
if (digitalRead(btn) == LOW) {
if (++btn_tim_on == 5) { // 5 checks before it's considered as key press and every 256*4ms repeat
++btn_val;
}
btn_tim_off = 80; // about 0.32 second to confirm
} else {
btn_tim_on = 0; // reset button pressed timer
if (btn_tim_off > 0) {
if (--btn_tim_off == 1) { // call action
Serial.println(btn_val); // print in serial monitor
switch (btn_val) {
case 1: s1(); break;
case 2: s2(); break;
case 3: s3(); break;
case 4: s4(); break;
default: Serial.println(F("no action")); break;
}
}
} else {
btn_val = 0; // reset button press counter
}
}
delay(4);
}
// where to place s1, s2, s3 and s4 ...
// playing these actions is blocking, so no keys are detected during "play"
answered Sep 8, 2016 at 12:22
1 Comment
eleos
Thanks! is there a way to break the loop when i press the button when the loop is running?