Similarly to the last Circuit of the first Project Set, this final Circuit will include a little bit of everything from Project Set 2 and will also incorporate the use of some LED's to play a game.

Transcript

Hey guys Christian here from Core Electronics. Today we're going to finish off project set 2, by undertaking circuit 2C, which is a Simon Says game.

In this circuit, we're going to expand on our last circuit as well as including some other components from projects set 1. So, the components that we're going to include improved from project set 1, LEDs once again and some 330-ohm resistors. So, we can keep the circuit setup the same way as we did in the last video, we're just going to include the extra components. So, one of the extra components is another push button, just to increase the difficulty of this Simon Says game, so we're going to plug that in the same way that we did the other push buttons and we're going to connect it once again the same way that we did with the others. So that is one of the pins going to ground and the other pin going to pin 8.

So, the LEDs in this one will change the way that these are connected, so we'll connect the LEDs and then we'll make sure that all the connections are good. So, for this section to omit the use of some wires we're going to be using these 330-ohm resistors to go straight to the ground connection from the negative terminal of the LED. So, it is a little tough to see given the number of wires but essentially what we're doing, is we're putting the positive terminal of this one is going into 19 H and the negative terminal is going in to 20 H, from there I'm connecting 20 on the outer most pin to ground through that resistor and I'm just going to do the same thing. So, I'm putting the positive polarity of the LED on the uppermost side and the negative on a lower side and that way that I know that I'm consistent with my connections. So, an important thing to not do here is, do not connect the LED to any of the pins from the push buttons because they are inputs and an LED is an output component. So, with that said that is the negative connections of the LEDs connected, so we'll go across and we will rewire the positive connections.

So, your positive terminal from the red LED I've put in row 25. So, row 25 following a book will go to pin 3, so the push-button pin for the red one stays the same the positive red connection goes to pin 3, then yellow push-button pin goes to pin 4 and then the yellow LED pin goes to pin 5, so it is essentially, push button, LED, push button, LED. The green connection goes to 6 and green LED connection goes to 7, the blue push-button connection we put in 8 and the blue LED will go in 9. So, from this, we will have a straight connection or a straight set up of all the pins from pin 2 to pin 10 with the first one being the red push button and the last one being the signal pin for the buzzer.

So now that that's all done we're going to like the circuit and then we're going to open guide for Simon Says. So, the last program is currently running on there and that's having the input pull up on pin 3, which is making this LED look high. So, what we're going to do is we're going to upload this code so that it stops that. It'll upload it'll reset the board and then it will run the new code and you'll see all the lights go on, so if we want to see that again we can hit reset, all the lights will go on and I'll refresh, it'll also place a tone and it will give us a lead for which way we want to go. So, all the lights load up it'll give us a first one, we press that one it'll give us a pattern, and all want us to output that pattern continually. So, if we're going to look at the code here we're going to see Int button. So, they've saved all the button pins in an array and the reason that they've done this is, so they can scroll through it later. Similarly, all the LEDs are saved in an array and all the tones that can be played for each of the buttons and LEDs are also saved in an array.

So, to win we apparently must get ten in a row or ten selections in a row, so it'll go 1, 2, 1, 2, 3, 1, 2, 3, 4, we'll have to get up to 10. If we want to make the difficulty easier we can say we only need to go up to 5, we'll leave it at 10 and once it reaches 10 it will start to play a song for us a victory. So, the button sequence apparently must go up to 16, so that is the amount that we'll have to remember to get there. So, a buzzer pin once again is on 10 we didn't change that we have a variable to indicate if a button has been pressed, so we'll probably have 0, 1, 2, 3, and 4 means no button is pressed and this round counter identifies what around we're on. So, we have time functions, so we have a start time and we have a time limit, so 2,000 is in milliseconds so it's 2 seconds. So, it's going to say that we have two seconds to get to the next pin otherwise we fail we end our game and we're going to be using Boolean values. So Boolean values are named after I believe George Bull and he started Boolean algebra and that is where your "or" statements and "and" statements come from, they're very useful in programming and he's got some awesome states called "true" and "false" which are Boolean values, so false means not true and true means true.

So, in the setup, we're going to use once again the "INPUT_PULLUP" and the INPUT_PULLUP's are going to tell us that these push buttons of an input pull up variety. So that means that we can safely press them with that the red board shorting out from power to ground. So next we have the LEDs, we have them set as outputs and once again you can see that they've declared the button pins by referencing them in the array. So, arrays in Arduino start at 0, so this here is position 0 this here's position 1, 2, and 3 and this is helpful for when we're writing for loops because we want to start at 0 and we want to go until we get a certain amount. So, that's a nice side note so we're starting at 0, 1, 2, 3 and they are all outputs here so pins 3, 5, 7 and 9 all outputs and the buzzer pin once again is an output. So, if the game started is false, we want to run the start sequence. So, it's saying that if our game has not commenced we want to run start sequence. Now start sequence will be a void function down here, somewhere, start sequence and that start sequence will initialize the game. So, from there we have round counter, so our counter here that tells us what round we're on will be reset back to zero and game started will now equal true, so that once we go through this loop it will not run this again, it won't start the game partway through we've already got a started game. So, in each round we're going to flash out the new sequence and this sequence is generated in our start sequence, so it's going to flush out the sequence, so we know what we're doing, so an example so I must press green, green, green and that is our next. That is how we can see the button sequence, so the button sequence is randomly generated using this random seed function and they've made it extra random or tried to make it extra random because nothing computer-generated is truly random but it's random enough for the purposes of this game, they're doing that by reading the values of the floating A0 pin.

So, from there it is going to run the game, it's going to delay and then it's going to go to the next one and then it's going to go to the next one until we have reached the end of the round counter. Then it's going to wait for us to read it in, so it's going to start through the process to see if we the user are pressing the correct button at the correct time, so to do this they use a function called "Millis" and Millis or Millie’s, determines the amount of time that's run since the red board has been initialized. So, Millis is good for timekeeping purposes and as you'll see in our projects at 4 when we do our display will display this on the LCD screen. So, it's going to start this timer essentially and then we're going to check this until the end of the sequence or until the player messes up. So, we're checking in the button, button check will read in essentially this value that we had up here, this pressed button. It'll save a value there, from the button check state, where it's comparing the digital read values of each of the buttons in the array and then it will return the value here. So, if it's this button it's going to return zero, this button that's going to return 1, 2 and 3 and if nothing's returned as stated before or return 4. So, it's going to first check that it's not 4 and then it's going to compare this button to the button in the sequence that we need to press. If that's correct it'll keep going it'll break out of this "if" statement here and I'll do it again. Then "if" wrong it's going to run the loose sequence, where it'll play the losing tone, for instance, that tone there. Then it will break from the sequence, otherwise, it's going to turn all the LEDs off again. so, to make sure that the buttons are pressed in the appropriate time, as I didn't go there, it's going to compare Millis which is the time running at that present stage to start time, which it was registered up here at the start of the function and then it's going to compare that, to the time limit. So, if it's less than two seconds because time limit was set to two seconds or 2,000 milliseconds, if that's done there if it's greater then it will also lose the sequence, otherwise, it won't read this section in.

Once we get to the end it'll increase the round counter by one, once we correctly input all these button sequences and then if the round counter is greater than the rounds to win, we will win, it will run the win sequence and it will play a winning tone for us and it'll delay again and then if we want to play again, we're going to have to press this reset button. So, in here they've got a numerous set of functions flash led, which is this initialized start all led off, which essentially just turns all the LED off button. Check which we discussed earlier it reads the state of which button was pressed, start sequence starts the game by randomly selecting a value that's going to keep going up for this random sequence, it's going to populate the button sequence array essentially, with random numbers from 0 to 3 which are 0,1,2,3 and then if this is done correctly then it will flash all these on as it did at the start and it will get us ready to play the game. So, it's going to flash and play tones, which are the initialization tones, it will repeat four times as we could see with the four flashes and the four noises at the start. Then this is our win sequence so if we end up winning, which I could probably show you hereby decreasing the rounds the win to 2, hopefully, I don't ruin this and mess this up. Then it will play a winning sequence and if we are wrong, I have won the game. If I hadn't done that it plays the losing sequence by flashing the pins and turning on the buzzer to make these noises, which are sad noises. Then it will reset game starter back to false, there we go it does reset the game started back to false so that we can play again when we want to.

So that is circuit 2C, it's a lot to take in but essentially we now understand at the end of project set 2, what an array is, how to access individual parts of an array, what a for loop is and how to use it to access parts of an array, what pull-up resistors are and how to use them and how to connect a push-button and how to ultimately use a buzzer, which will become much more focused on in future topics because they are good for warning systems or detection systems, which we'll be using in the very next project set.

So, with that said congratulations on finishing project set 2 using SparkFun Inventors Kit for Arduino and I'll see you in the next project set on Motion.

SKU: KIT-14265 https://core-electronics.com.au/sparkfun-inventors-kit-v4-0.html
CAT: https://core-electronics.com.au/arduino.html

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.