Pages: [1]   Go Down
Author Topic: Button deboncing in simon game  (Read 2961 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi. Im preety new to programming and microcontrolers world, and i want to make simon game, so i did it without copying any code. It is working, but the problem is that i made button debouncing but looks like i made some mistake and i dont know where, Also tryed to set debounce time to 50ms, but it didnt help.
Code:
#include <TrueRandom.h>
#include <EEPROM.h>
int randled;
int val;
int allow=1;
int leds[50];
int buttons[50];
int button1=2;
int button2=3;
int button3=4;
int button4=5;
int turn=0, stepp=1;
int bt1, bt2, bt3, bt4, exbt10, exbt20, exbt30, exbt40, bt10, bt20, bt30, bt40, milis1, milis2, milis3, milis4, debounce=50, test1=0, test2=0, test3=0, test4=0;
unsigned long time1, time2, time3, time4;
int play=1;
int glava=1,konec=0,rec;



void setup (){
pinMode(9,OUTPUT);
pinMode(8,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(5,INPUT);
Serial.begin(9600);
}

void loop (){

if(glava==1){
  
if(allow==1){
  
  for(int i=0; i<49; i++){
    leds[i]=TrueRandom.random(8,12);
  }
allow=0;
}

//RECORDING BUTTONS

bt10=digitalRead(button1);
if(bt10==HIGH && exbt10==LOW){
milis1=millis();
test1=1;
}
if(test1==1 && (millis()-milis1)>debounce){
bt1=digitalRead(button1);
if (bt1==HIGH){
  buttons[turn]=8;
//Serial.println(buttons[turn]);
digitalWrite(8,HIGH);
time1=millis();
turn++;
test1=0;
}
}
if(millis()-time1>200){
  digitalWrite(8,LOW);
}


bt20=digitalRead(button2);
if(bt20==HIGH && exbt20==LOW){
milis2=millis();
test2=1;
}
if(test2==1 && (millis()-milis2)>debounce){
bt2=digitalRead(button2);
if (bt2==HIGH){
  buttons[turn]=9;
//Serial.println(buttons[turn]);
digitalWrite(9,HIGH);
time2=millis();
turn++;
test2=0;
}
}
if(millis()-time2>200){
  digitalWrite(9,LOW);
}


bt30=digitalRead(button3);
if(bt30==HIGH && exbt30==LOW){
milis3=millis();
test3=1;
}
if(test3==1 && (millis()-milis3)>debounce){
bt3=digitalRead(button3);
if (bt3==HIGH){
  buttons[turn]=10;
//Serial.println(buttons[turn]);
digitalWrite(10,HIGH);
time3=millis();
turn++;
test3=0;
}
}
if(millis()-time3>200){
  digitalWrite(10,LOW);
}


bt40=digitalRead(button4);
if(bt40==HIGH && exbt40==LOW){
milis4=millis();
test4=1;
}
if(test4==1 && (millis()-milis4)>debounce){
bt4=digitalRead(button4);
if (bt4==HIGH){
  buttons[turn]=11;
//Serial.println(buttons[turn]);
digitalWrite(11,HIGH);
time4=millis();
turn++;
test4=0;
}
}
if(millis()-time4>200){
  digitalWrite(11,LOW);
}




if(play==1){
  
  for (int k=0;k<stepp; k++){
    digitalWrite(leds[k], HIGH);
    delay(300);
    digitalWrite(leds[k], LOW);
    delay(200);
  }
  play=0;
}


        for(int j=0; j<turn;j++){
          if (buttons[j]!=leds[j]){
            val=(stepp-1);
            rec=EEPROM.read(0);
            
            digitalWrite(8,HIGH);
            digitalWrite(9,HIGH);
            digitalWrite(10,HIGH);
            digitalWrite(11,HIGH);            
            
            if(rec>=val){
            Serial.print("Napaka, tvoj dosezek je: ");
            Serial.println(val);
            Serial.print("Rekord je: ");
            Serial.println(rec);
            }
                    
            if(rec<val){
            Serial.print("Dosegli ste nov rekord: ");
            Serial.println(val);
            Serial.println("CESTITAM!!!");
            EEPROM.write(0, val);            
            }
            
            glava=0;
            konec=1;
          }
        }
        
        if(turn==stepp && konec!=1){
          delay(200);
          digitalWrite(8,LOW);
          digitalWrite(9,LOW);
          digitalWrite(10,LOW);
          digitalWrite(11,LOW);
          stepp++;
          play=1;
          turn=0;
          delay(1000);
        }
    

}


exbt10=bt10;
exbt10=bt10;
exbt20=bt20;
exbt30=bt30;
exbt40=bt40;

}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure I know where your problem lies, but can I recommend you use the Auto-format (ctrl-T) facility in the IDE before you post your code?

Code:
int bt1, bt2, bt3, bt4, exbt10, exbt20, exbt30, exbt40, bt10, bt20, bt30, bt40, milis1, milis2, milis3, milis4, debounce=50, test1=0, test2=0, test3=0, test4=0
Mixing constant and variable declarations is a recipe for hair-loss.
« Last Edit: January 22, 2011, 08:54:43 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh sorry, and thanks for that tip. I also changed debounce time to 100ms so you have to hold button 100ms to count, but still sometimes one click counts as 2.. very rare but it does :s
Code:
#include <TrueRandom.h>
#include <EEPROM.h>
int randled;
int val;
int allow=1;
int leds[50];
int buttons[50];
int button1=2;
int button2=3;
int button3=4;
int button4=5;
int turn=0, stepp=1;
int bt1, bt2, bt3, bt4, exbt10, exbt20, exbt30, exbt40, bt10, bt20, bt30, bt40, milis1, milis2, milis3, milis4;
int debounce=100;
int test1=0;
int test2=0;
int test3=0;
int test4=0;
unsigned long time1, time2, time3, time4;
int play=1;
int glava=1,konec=0,rec;



void setup (){
  pinMode(9,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(2,INPUT);
  pinMode(3,INPUT);
  pinMode(4,INPUT);
  pinMode(5,INPUT);
  Serial.begin(9600);
}

void loop (){

  if(glava==1){

    if(allow==1){

      for(int i=0; i<49; i++){
        leds[i]=TrueRandom.random(8,12);
      }
      allow=0;
    }

    //RECORDING BUTTONS

    bt10=digitalRead(button1);
    if(bt10==HIGH && exbt10==LOW){
      milis1=millis();
      test1=1;
    }
    if(test1==1 && (millis()-milis1)>debounce){
      bt1=digitalRead(button1);
      if (bt1==HIGH){
        buttons[turn]=8;
        Serial.println(buttons[turn]);
        digitalWrite(8,HIGH);
        time1=millis();
        turn++;
        test1=0;
      }
    }
    if(millis()-time1>200){
      digitalWrite(8,LOW);
    }


    bt20=digitalRead(button2);
    if(bt20==HIGH && exbt20==LOW){
      milis2=millis();
      test2=1;
    }
    if(test2==1 && (millis()-milis2)>debounce){
      bt2=digitalRead(button2);
      if (bt2==HIGH){
        buttons[turn]=9;
        Serial.println(buttons[turn]);
        digitalWrite(9,HIGH);
        time2=millis();
        turn++;
        test2=0;
      }
    }
    if(millis()-time2>200){
      digitalWrite(9,LOW);
    }


    bt30=digitalRead(button3);
    if(bt30==HIGH && exbt30==LOW){
      milis3=millis();
      test3=1;
    }
    if(test3==1 && (millis()-milis3)>debounce){
      bt3=digitalRead(button3);
      if (bt3==HIGH){
        buttons[turn]=10;
        Serial.println(buttons[turn]);
        digitalWrite(10,HIGH);
        time3=millis();
        turn++;
        test3=0;
      }
    }
    if(millis()-time3>200){
      digitalWrite(10,LOW);
    }


    bt40=digitalRead(button4);
    if(bt40==HIGH && exbt40==LOW){
      milis4=millis();
      test4=1;
    }
    if(test4==1 && (millis()-milis4)>debounce){
      bt4=digitalRead(button4);
      if (bt4==HIGH){
        buttons[turn]=11;
        Serial.println(buttons[turn]);
        digitalWrite(11,HIGH);
        time4=millis();
        turn++;
        test4=0;
      }
    }
    if(millis()-time4>200){
      digitalWrite(11,LOW);
    }




    if(play==1){

      for (int k=0;k<stepp; k++){
        digitalWrite(leds[k], HIGH);
        delay(300);
        digitalWrite(leds[k], LOW);
        delay(200);
      }
      play=0;
    }


    for(int j=0; j<turn;j++){
      if (buttons[j]!=leds[j]){
        val=(stepp-1);
        rec=EEPROM.read(0);

        digitalWrite(8,HIGH);
        digitalWrite(9,HIGH);
        digitalWrite(10,HIGH);
        digitalWrite(11,HIGH);            

        if(rec>=val){
          Serial.print("Napaka, tvoj dosezek je: ");
          Serial.println(val);
          Serial.print("Rekord je: ");
          Serial.println(rec);
        }

        if(rec<val){
          Serial.print("Dosegli ste nov rekord: ");
          Serial.println(val);
          Serial.println("CESTITAM!!!");
          EEPROM.write(0, val);            
        }

        glava=0;
        konec=1;
      }
    }

    if(turn==stepp && konec!=1){
      delay(200);
      digitalWrite(8,LOW);
      digitalWrite(9,LOW);
      digitalWrite(10,LOW);
      digitalWrite(11,LOW);
      stepp++;
      play=1;
      turn=0;
      delay(1000);
    }


  }


  exbt10=bt10;
  exbt10=bt10;
  exbt20=bt20;
  exbt30=bt30;
  exbt40=bt40;

}
Logged

Pages: [1]   Go Up
Jump to: