Solved: Pressing a button in a rhythm


so i'm making game has press button 5 times rhythm of staying alive of bee gees. know, how if in situation have revive someone, keep rhythm of song in head. it's 103 bpm that's 580ms/beat. put large window around of 500-700ms.

now code doing right:
- if player presses in right rhythm 5 times in row, led flashes 10times indicate they've won.

what can't work when "break chain", i.e. press button fast or slow. more detail in code below. i've worked around slow part adding bit have finish within 2.7 seconds or game restarts. of course not neat as: "you fast or slow game restarted".

any ideas?

code: [select]
const int buttonpin = 2;     // number of pushbutton pin
const int ledpin =  13;      // number of led pin

int count = 0;               // count button presses
int buttonstate = 0;         // variable reading pushbutton status
int lastbuttonstate = 0;
long detection_start = 0;
long detection_range = 0;
int buttonpushcounter = 0;

void setup() {
  pinmode(ledpin, output);     
  pinmode(buttonpin, input);
  serial.begin(9600);   
}

void loop() {

  buttonstate = digitalread(buttonpin);
 
  if (buttonstate != lastbuttonstate && buttonstate == high) {

    serial.println(millis() - detection_range);
    serial.println(millis() - detection_start);
   
    if (buttonpushcounter == 0){
     detection_start = detection_range = millis();
     buttonpushcounter++;
     serial.print("number of button pushes: ");
     serial.println(buttonpushcounter);
     digitalwrite(ledpin, high);   
    }
   
    if (buttonpushcounter > 0 && millis() - detection_range >= 500 && millis() - detection_range <= 700) {
      buttonpushcounter++;
      serial.print("number of button pushes: ");
      serial.println(buttonpushcounter);
      detection_range = millis();
    }

[b]/* these lines below don't work because fall loop buttonpushcounter reset 0,
 *  because of on if buttonpushcounter == 0, millis , detection_range set equal.
 *  automatically means difference < 500, triggering reset again.
 */[/b]
 
//    if (buttonpushcounter > 0 && millis() - detection_range < 500) {
//      buttonpushcounter = 0;
//      serial.print("reset");
//      }
//     
    delay(50);
  }
 
  if (buttonpushcounter > 0 && millis() - detection_start > 2700){
      buttonpushcounter = 0;
    }
   
  lastbuttonstate = buttonstate;

  if (buttonpushcounter >= 1 && buttonpushcounter <= 5) {
    digitalwrite(ledpin, high);
  } else {
    digitalwrite(ledpin, low);
  }

  if (buttonpushcounter == 5){
    (int = 0; <= 10; i++){
    digitalwrite(ledpin, low);
    delay(100);
    digitalwrite(ledpin, high);
    delay(100);
    }
//    delay(5000);
//    buttonpushcounter = 0;
   }
}

code: [select]
detection_start = detection_range = millis();

don't this.  make 2 statements.  yes, know legal hurts readability , need help.  try keep things simple if you're new this. 

instead of relying on buttonpushcounter everything, make couple of new variables don't have reset buttonpushcounter 0 every time make mistake.  maybe boolean variable called onewasoutofrange, or boolean everythingisstillallgoodforthisplayer can set false when miss one.  want new value millis when button pressed.

which leads me question rules game.  if late on third press, need press fourth little make , on time song or want still wait entire 500 - 700ms? 


Arduino Forum > Using Arduino > Programming Questions > Solved: Pressing a button in a rhythm


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts