Pages: 1 2 [3]   Go Down
Author Topic: My Arduino Game  (Read 2911 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes it does! However I am still not sure of how I am going to have the program tell which LED it chose. Here is what I tried and it didn't work as I would have liked it to.
Code:
case 3:
         for(int i=0; i < MAX; i++)
         {
           digitalWrite(LEDs[i], LOW);
         }
           randomLED = random(4,6);
           {
           digitalWrite(LEDs[randomLED], HIGH);
           }
           if(LED7 == HIGH)
           {
             if(BUTTON2 == HIGH)
             {
               digitalWrite(PIEZO, HIGH);
             }
           }
             else
             {
               if(BUTTON2 == LOW)
               {
                 digitalWrite(PIEZO, HIGH);
               }
             }
           
          break;
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27434
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That requires the button to have been read before the LED was selected.

You could read/compare the state for several times over the space of 1/10th of a second after the LED is selected, and then drive the piezo as needed.

Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you please give an example of what that should look like because I am completely lost?
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27434
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you need two arrays, one for LEDs, one for Buttons.

Code:
  randomLED = random(4,6);
// Returns a random number between min and max-1 (long)
// so 4 or 5 for your example?

//Then you turn on the selected LED
digitalWrite (LEDs[randomLED], HIGH);

//and start reading the associated button
currentTime = millis();
goodPress = 0;  // reset indicator
while ( (millis ( ) - currentTime ) <=100){  // go into a loop for 100mS
      buttonPressed = digitalRead (Buttons[randomLED]);  // read the button associated with the LED
       if (buttonPressed == 1){  // if it was pressed (assumes you have it pulled low externally, and button takes it high
       goodPress = 1;  // stores a good press, even if subsequently released during the 100mS
       }
// then when the 100mS of reading the button is done, do your piezo action based on goodPress
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you please recommend some websites that may help me understand what all of this means?!
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27434
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://arduino.cc/en/Reference/HomePage

Time to do some reading my friend.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19367
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Also Google "C tutorial" - it must be one of the most widely explained languages. Find a site whose style you like, and work through a few examples. In no time you'll become an expert. :-)
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

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

Code:
const int LED7 = 10;
Code:
if(LED7 == HIGH)
That, I confidently predict, is never going to happen.
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.

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for your replies gentlemen. However I have been on the Reference page multiple times and it is just not detailed enough for me to understand. I shall start reading before I continue with my game.
Logged

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

OK, the reason I'm so confident, is that LED7 has been given the value 10.
It is never, ever going to be equal to HIGH (aka 1).
To change the state of the LED attached to the pin described by LED7, you have to perform a "digitalWrite".

Similarly,
Code:
if(BUTTON2 == LOW)
BUTTON2 has the value 1.
To read the state of the switch attached to the pin described by BUTTON2, you have to perform a digitalRead.
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.

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have decided to approach this project in a completely different manner. Instead of trying to understand how to program it first, I am going to instead try to understand all the hardware aspects of it and how the current flows through it. I am currently in the process of understanding how a pull up/down resistor works. I am getting closer to understanding it but I would like if someone could please give me an example of how it works and how current flows through a circuit that has a pull up/down resistor. Also, in regards to the resistor, with my game I switched the hardware up a little. Instead of having LED0 and LED1 connected to an individual digital pin, I only made LED0 be connected to a pin and LED1 be connected through BUTTON1 so when the button is pressed, 5V passes directly to LED1. I did this to free up some pins because I was having trouble connecting things to RX<-0 and TX->1 which I guess are used for Serial communication? I made a diagram of what I think my circuit looks like below. Being a beginner to Electrical Engineering, I am not completely sure how to draw an accurate diagram. My question is this. Does adding the LED with its own resistor elliminate the need for the Pull Down resistor, does it act in place of the Pull Down resistor, or does it do neither and everything works fine without any floating voltage errors or things of the like?
Thanks!
 


* First Circuit Diagram.jpg (26.14 KB, 819x460 - viewed 17 times.)
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27434
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

With pin 8 set as an input, it will read low when the switch is open.
When the switch is closed, pin 8 will read high, the LED will turn on, and
(5V- ~1V)/330 = 12mA will flow thru the LED, and about 0.5mA thru the 10K resistor.
Adjust the 1V to whatever your LED actually has for a forward voltage drop.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
while ( (millis ( ) - currentTime ) <=100){  // go into a loop for 100mS
If currentTime = millis(), than wouldn't millis() - currentTime always be 0?
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27434
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

currentTime holds the value of millis() before the while loop starts.
The next time millis() is read, it is now bigger # than the older time that is stored in currentTime, so the difference will always be >0.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: 1 2 [3]   Go Up
Jump to: