Go Down

Topic: My Arduino Game (Read 3087 times) previous topic - next topic

MathematicsFanatic

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: [Select]
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;

CrossRoads

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.

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

MathematicsFanatic

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

CrossRoads

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

Code: [Select]

  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
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

MathematicsFanatic

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

CrossRoads

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

Time to do some reading my friend.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Nick Gammon

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. :-)
Please post technical questions on the forum, not by personal message. Thanks!

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

AWOL

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

MathematicsFanatic

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.

AWOL

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: [Select]
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.
"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.

MathematicsFanatic

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!
 

CrossRoads

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

MathematicsFanatic

Quote
while ( (millis ( ) - currentTime ) <=100){  // go into a loop for 100mS

If currentTime = millis(), than wouldn't millis() - currentTime always be 0?

CrossRoads

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up