LED Game!  (Very simple lol)

I just completed my first project on my Arduino I got yesterday. Here is a demonstration, tell me what you guys think:

http://www.youtube.com/watch?v=ButBWXPgTu4

If you would like the code just let me know.

Pretty cool! I love flashing light games :o! :)

Hey that's pretty nifty!

You should know, nobody will ever say no to uploading your code! :) (Just make sure you use the code button, looks like a '#' above where you type.) :D

I think your next project should be to do a matrix of LED's, and have it randomly light up a row.. use the same concept but for like 8 rows! :D Have it randomly do like ROW 1, then COLUMN 4, then ROW 6, then COLUMN 8, something of the sort, and have a button on each side of the box, have it go backwards and forwards...

okay, sorry, I'll stop.. haha alot of good ideas for this one though. :)

Haha I guess I’ll post the code. I used pin 2 for the interrupt from the button (which has a pull up resistor), pins 3 through 10 for the Leds, and pin 13 for the red LED.

#define BUTTON 2
#define LED 13

int ledPins[8] = {3,4,5,6,7,8,9,10};
double tempo = 300;
int score = 0;
boolean blueLed = false;

void interrupt();
void endFunction();

void setup(){
  for( int i = 0 ; i < 8 ; i++ ){
    pinMode( ledPins[i] , OUTPUT ); 
  }
  pinMode( LED , OUTPUT );
  pinMode( BUTTON , INPUT );
  attachInterrupt( 0 , interrupt , FALLING ); 
}

void loop(){
  for( int i = 0 ; i < 7 ; i++ ){
    digitalWrite( ledPins[i] , HIGH );
    delay( tempo );
    digitalWrite( ledPins[i] , LOW );
  }
  digitalWrite( ledPins[7] , HIGH);
  blueLed = true;
  delay( tempo );
  digitalWrite( ledPins[7] , LOW);
  blueLed = false;
  for( int i = 6 ; i > 0 ; i-- ){
    digitalWrite( ledPins[i] , HIGH );
    delay( tempo );
    digitalWrite( ledPins[i] , LOW );
  }
}

void endFunction(){
  Serial.begin(9600);
  Serial.print( "Your score: " );
  Serial.println( score );
  digitalWrite( LED , HIGH );
  while(1){
    //game over
  } 
}

void interrupt(){
  delayMicroseconds(20000);
  if( digitalRead( BUTTON ) == HIGH ){
    return; 
  }
  tempo = tempo * 9 / 10;
  if( blueLed ){
    score++; 
  }
  else{
    endFunction(); 
  }
}

Just curious, did you know about the internal pull-ups available? I haven't worked with interrupts much, so I'm not sure if using them will make a difference.. but, each pin has an internal 10k pull-up resistor, you can enable by using these two lines:

pinMode(buttonPin, INPUT); // tells button it's an input! digitalWrite(buttonPin, HIGH); // when it's set to "input", writing the button HIGH will enable the 10k pull-up internally

Just makes for easier hardware setup. :)

I didn't know that, thanks for the tip.

I haven't worked with interrupts much

I haven't worked with them at all :P. Why didn't you just use digitalRead()? Are interrupts quicker/simpler or something?

Bear in mind, I'm too lazy to read through the whole code, so if it's something in there that causes digitalRead() to not work with it, my bad :P.

Well it's easier to work with interrupts because otherwise you would have to poll digitalRead() a lot. The interrupt allows you do to the reverse, check the condition of the blue LED when the button is pressed. This is much more efficient.

Also, the LED stays lit based on delay() and you couldn't poll digitalRead() during this time. There would be no way to tell if the button was actually pressed and released during the delay. I hope this makes sense.

Well it's easier to work with interrupts because otherwise you would have to poll digitalRead() a lot. The interrupt allows you do to the reverse, check the condition of the blue LED when the button is pressed. This is much more efficient.

Also, the LED stays lit based on delay() and you couldn't poll digitalRead() during this time. There would be no way to tell if the button was actually pressed and released during the delay. I hope this makes sense.

Makes perfect sense. Thanks for the explanation :D.