Pages: [1]   Go Down
Author Topic: Need help with game!  (Read 862 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys,

I'm really new to arduino and need your assistance with something. I am planning on building one of those games, where you have about 8 LEDs and one of them is a different color. So they light up in order from bottom to top and top to bottom. The goal of the game is to stop the light at the red LED by pressing a push button.

Can any of you please show me a simple code for that? I really need it for a class.

Thank you very much.
Logged

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

please can someone help me?
Logged

0
Offline Offline
God Member
*****
Karma: 2
Posts: 854
Arduino rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First, the way you've asked, you're not looking for help, you want someone to do it for you.

Second, no, I will not do your homework for you.
Logged

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

what I want is a starting point so I can customize it and make it better myself. I don't want you to do my homework for me I'm just asking you to lead me in the right direction. All I can do is to make the LEDs work in sequence now and thats it.

Will try to come up with something and post it here later.  
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A good place to start: http://www.ladyada.net/learn/arduino/lesson5.html
But really, if you're new to programming you ought to start at lesson 1.

All in all, this is not an easy, do it in a night project (not for someone new to the Arduino,  and new to programming).
I would recommend that you find a different project that's not quite as difficult.

Here's an easy way to do a "wave" of lights, but in order to capture when someone's pressed a button, you'll have to work out a non-delay method of lighting up the LEDs
(this code lifted from ladyada)
Code:
  digitalWrite(led5Pin, LOW);
    digitalWrite(led1Pin, HIGH);
    delay(50);
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(50);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(50);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(50);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(50);
    digitalWrite(led5Pin, LOW
Logged

0
Offline Offline
God Member
*****
Karma: 2
Posts: 854
Arduino rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Here's an easy way to do a "wave" of lights, but in order to capture when someone's pressed a button, you'll have to work out a non-delay method of lighting up the LEDs

For a project like this where you don't have to do anything except check for the button during the delay, you can poll the button over and over x number of times to create the delay rather than simply calling delay (this is called busy-waiting).

For a more complex project with more things going on, you would use interrupts handle the button.  It wouldn't hurt for you to understand what they do, but you don't need to use them here.
Logged

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

ok I got the leds working but i cant get the push button to freeze the light where it is. It should be simple but I just cant figure it out. here is my code:
Code:
int timer = 100;                   // The higher the number, the slower the timing.
int pins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbers
int num_pins = 6;                  // the number of pins (i.e. the length of the array)

int ledPin = 13;                // choose the pin for the LED
int inputPin = 8;               // choose the input pin (for a pushbutton)
int val = 0;                    // variable for reading the pin status



void setup()
{
  Serial.begin(9600);
  int i;

  for (i = 0; i < num_pins; i++)   // the array elements are numbered from 0 to num_pins - 1
    pinMode(pins[i ], OUTPUT);      // set each pin as an output


  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare pushbutton as input
}


void loop()
{
  int i;
  
 Serial.println(digitalRead(8));
  
  for (i = 0; i < num_pins; i++) { // loop through each pin...
    digitalWrite(pins[i], HIGH);   // turning it on,
    delay(timer);                  // pausing,
    digitalWrite(pins[i], LOW);    // and turning it off.
  }
  for (i = num_pins - 1; i >= 0; i--) {
    digitalWrite(pins[i], HIGH);
    delay(timer);
    digitalWrite(pins[i], LOW);
  }



  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(pins[i], LOW);  // turn LED OFF
  } else {
    digitalWrite(pins[i], HIGH); // turn LED ON
  }
  
  
}

Logged

0
Offline Offline
God Member
*****
Karma: 2
Posts: 854
Arduino rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
ok I got the leds working but i cant get the push button to freeze the light where it is. It should be simple but I just cant figure it out.

As I said in my previous replay.  Each of those delays is where you want to check if the button is pressed.  Instead of a delay, check over and over for the length of time you want to wait.

What exactly are you trying to do in that code if the buttons is pressed.  It makes no sense, and what do you think your i variable is doing at that point?
Logged

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

As said in the previous post, you cannot use "delay" to do what you want. If you are familiar with programming, try implementing a state machine. Think of what states you system can be in, and how do you move between states -- "times up", "button pressed" etc. The tightly loop over checking time and button input (don't forget to debounce) and swtich between different states.
Logged

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

I actually made one of these while I was working through the tutorials on the ladyada site (http://www.ladyada.net/learn/arduino/).

Mine is probably uglier than it needs to be... and most likely could benefit from a much larger breadboard, but it works.

I wrote the code to check the state of the button while the LED is lit, as opposed to trying to catch it just before or after the light changes.  This worked much better than the earlier revision where I was effectively checking the state of the button JUST BEFORE the target light was lit.

Here's a video of how it turned out:


and here's my sketch:
Code:
/*
 *  Chasing lights skill stop game!  Version 1.0.0.0.0.0.1 by sstrong.
 */
int potPin = 5;
int potVal = 0;
int switchPin = 13;              // switch is connected to pin 2
int led1Pin = 2;
int led2Pin = 3;
int led3Pin = 4;
int led4Pin = 5;
int led5Pin = 6;
int led6Pin = 7;
int led7Pin = 8;
int speakerOut = 10;
int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state
int lightMode = 0;              // What mode is the light in?
int litPin = 0;
int i = 0;

void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  pinMode (speakerOut, OUTPUT);
  
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(led4Pin, OUTPUT);
  pinMode(led5Pin, OUTPUT);
  pinMode(led6Pin, OUTPUT);
  pinMode(led7Pin, OUTPUT);
  
  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
}

void loop(){
  
  potVal = analogRead(potPin);    // read the value from the sensor
    Serial.println(potVal);
  
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (lightMode == 2) {          // light is off
          lightMode = 1;               // turn light on!
        } else {
          lightMode++;               // turn light off!
        }
      }
    }
    buttonState = val;                 // save the new state in our variable
    Serial.print(lightMode);
  }

  // Now do whatever the lightMode indicates
  if (lightMode == 0) {
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led7Pin, LOW);
    
  }
  if (lightMode == 1) {
    if (litPin == 9){
       litPin = 0;
    }
    digitalWrite(litPin, LOW);
    litPin++;
    digitalWrite(litPin, HIGH);
    delay(potVal);
  }
  if (lightMode == 2){
    
    if (litPin == 5){
       // maybe flash a winner light.
      digitalWrite(litPin, HIGH);
      delay(100);
      digitalWrite(litPin, LOW);
      delay(100);
      digitalWrite(speakerOut, HIGH);
      delay(20);
      digitalWrite(speakerOut, LOW);
      for(i = 1000; i > 500; i--){
        digitalWrite(speakerOut,HIGH);
        delayMicroseconds(i);
        digitalWrite(speakerOut,LOW);
      }
     }
     else{
      digitalWrite(litPin, HIGH);
      for(i = 0; i < 500; i++){
        digitalWrite(speakerOut,HIGH);
        delayMicroseconds(i);
        digitalWrite(speakerOut,LOW);
      }
     }
  Serial.print(lightMode);
  Serial.print(":");
  Serial.println(litPin);
  
    
  }  
}

It's not the cleanest code, but it does exactly what I think it's supposed to.
Logged

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

oh thanks for all your responses guys.

squares I watched your video and its amazing. Though when i look at your code I cant figure out what int "potPin = 5;"

also what do you mean by saying     "int switchPin = 13;              // switch is connected to pin 2"  is it connected to pin 13 or 2? there is a led connected to 2 tho..

thanks again, u are amazing!
Logged

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

Quote
oh thanks for all your responses guys.

squares I watched your video and its amazing. Though when i look at your code I cant figure out what int "potPin = 5;"

also what do you mean by saying     "int switchPin = 13;              // switch is connected to pin 2"  is it connected to pin 13 or 2? there is a led connected to 2 tho..

thanks again, u are amazing!

int potPin=5; is where I set up a variable to reference the potentiometer on pin 5... one of the analogue pins.

the //pin 2 thing is a leftover comment.  The switch really is connected to pin 13 which sucks a little because I had to run a long nasty jumper around to reach it.  If I ever remake this, I'm going to lay all of the cross-board sorts of connections down first, and then put the led's over that.  Or I'll just wire up all of the controls on that side of the board and avoid the whole problem.

Anyway, good luck with your game.

S
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 171
&quot;So...What does this thing do?&quot;
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmmm....This looks like something I made not so long ago...except in a much simpler form. I think you will find a goldmine if information, and probably answers to every single question you have had and will ever have regarding this in these forum posts:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1207506269/3#3
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1208042125/0#0

Hope it helps!  smiley-wink
« Last Edit: May 16, 2008, 02:44:24 pm by quackmaster7000 » Logged

Pages: [1]   Go Up
Jump to: