I'm making a copy of Cyclone arcade game with an Arduino.
Using a Mega with 40 LEDs in a circle. The lignts sequnece from 2-42 one at a time.
If the player hits the stop button, (NO pushbutton) when a specific LED is lit (say 25) they win.
Branch to win sequence.
If a LED other than 25 is lit when they press the button they loose and game ends until PB 2 is pressed. to start the game again.
Need assistance with the coding methodology.
I can get the ligths to sequence using 40 high, 40 delay and 40 low statements.
But I'm wondering if I can use for ?
Something like for what I'm doing to set the pins to output.
But where I'm running into dificulty is getting the PB press in the for statement.
The logic would be
When PB 1 is pressed stop the chasing of the lights leaving the one when the button was pressed on and bliknking.
If blkiking LED = 25 goto win sequence of LEDs for 10 secounds
Otherwise wait for PB 2 to be pressed to sart game again.
I thought about an array, but seems like for would be easier and with a lot less code.
Things get easier if you use an array to store the pin numbers of the LEDs. Then you can use loops for things like:
void setup() {
for (int i = 0; i < NUMBER_OF_LEDS; i++) {
digitalWrite(LEDPins[i], LOW);
pinMode(LEDPins[i], OUTPUT);
}
}
In your loop() function you would check for the button press first. Then, if the sequence should be running, check to see if it is time to change which LED is lit. If it isn't time yet, there is nothing more for loop() to do.
If the LEDs are being lit in sequence:
digitalWrite(currentLED, LOW);
// The modulo (%) operation makes it wrap to 0 if it reaches NUMBER_OF_LEDS
currentLED = (currentLED + 1) % NUMBER_OF_LEDS;
digitalWrite(LEDPins[currentLED, HIGH);
Thanks I will study this in a bit. At first glance I'm not getting it.
With an array don't I have to specifiy the elements? (2, 3, 4, 5......40, 41)
Seems to me a for loop would be simpler. x=x+1 type of logic.
Maybe my loging is wrong too.
I'm thinking the lights are chasing around in the circle.
Code is wrong, but follow the logic.
{
Start
Turn LED X on
IF PB is pressed and it's LED lit is 20 goto win routinte.
Turn LED X off
X = X +1 // goto the next LED and light
(Keep looping)
Win routine,
Flash lights in swquence from 2 up (X+1) and from 42 (X-1) to 20
for 10 seconds
With an array don't I have to specifiy the elements
Yes that is the entire point of them. The array has an address or index, the contents of that address is the PIN number of that specific LED.
So if you have three LEDs on pins 10,11 & 12 then a loop to light up each LED in turn, you loop needs only run from 0 to 2 and you use the loop index to address an array to convert it into a PIN number.
This way the LED’s pins do not have to be contagious or in any specific order..
Still not understanding why I would not want to use a for loop.
If I use an arrray don't I have to define the array as in (2, 3, 4 .... 41,42)
Would it not be more efficient to use a for
Doug101:
Still not understanding why I would not want to use a for loop.
If I use an arrray don't I have to define the array as in (2, 3, 4 .... 41,42)
Would it not be more efficient to use a for
Loops and arrays go together like peanut butter & jelly. The array uses the loop value as the index.
Still not understanding why I would not want to use a for loop.
A for loop will do exactly what you want because the pin numbers are contiguous so there is no need to use an array of pin numbers in this case if you don't want to.
However, it is quite common for pin numbers not to be contiguous in the first place and this is where the advice to use an array of pin numbers arises. It involves very little typing, which in any case only has to be done once, allows the Arduino pin names such as A0, A1 etc to be used to make the program easier to understand, allows for easy changes to the order of pins used either because of hardware changes of to implement effects and in general it future proofs the program.
In my case I purpose designed it so I would use a contentions block of 40 pins.
If you are doing nothing with the Mega other than controlling 40 LEDs then OK, but look out for gotchas such as later needing to use Serial1, 2 or 3 or interrupts 2, 3, 4 or 5, both of which will prevent you using a congruous block of 40 pins as will SPI and I2C
Grumpy_Mike:
Strange new usage of the words "got it" that I had previously been unaware of.
Maybe one day you will learn modern English. We here in the states have been trying to teach it to you folks for a few hundred years. But for some reason you want to spell words like color the old way and not use our modern terms. Hasn't Australia been doing the same thing with you folks too? But then again they are still driving on the wrong side of the road.....
UKHeliBob:
If you are doing nothing with the Mega other than controlling 40 LEDs then OK, but look out for gotchas such as later needing to use Serial1, 2 or 3 or interrupts 2, 3, 4 or 5, both of which will prevent you using a congruous block of 40 pins as will SPI and I2C
I'm getting it. And I suppose if one uses an uno with an expander board or two the addressing is no longer linear so the best/real solution would be an array.
Doug101:
Maybe one day you will learn modern English. We here in the states have been trying to teach it to you folks for a few hundred years. But for some reason you want to spell words like color the old way and not use our modern terms. Hasn't Australia been doing the same thing with you folks too? But then again they are still driving on the wrong side of the road.....