This week's assignment is make a smart circuit, using two input &output components.
I made a game called Guess the number, which is used to simply to teach people searching algorithms.
I was inspired by this project on arduinoHub.
TinkerCad to simulate the circuit before connecting the hardware.
Arduino IDE for uploading the code on the board.
Breadboard for wiring all the components
Arduino uno to take inputs and perform action.
Keypad to enter the number
Lcd with i2c module to display messages.
Green and red leds.
Resistor 330 ohm to limit the current.
Switch acts as input for new game or not.
Jumper wires for connection.
Cardboard for enclosing the components.
First, I opened tinkercad for wiring the circuit, I connected the components as shown in the figure.
Note that the simulation didn't work because I think the i2c module library on tinkercad is a bit different from the one I use.
Second, I opened Arduino IDE for writing the code,
At first I included the library for both keypad &lcd and then started by defining each character for the keypad and the lcd address.
After that I defined the green led, the switch and the four leds as an array.
Also some variables that I'll use later.
Then I defined both the red& green leds as output while the key as input and I used INPUT_PULLUP due to bouncing problem.
At The beginning of the game there will be different messages displayed on the lcd containing the rules of the game, The user only has 4 attempts to guess the random number generated by the arduino, each attempt is represented by a red led.
At first I start the loop by generating a random number, I'm using the if condition and the flag variable to prevent the arduino from continuously changing the Secret_Number every time the void loop() gets executed.
Then getting the character entered by the player from the keypad, increasing the attempt variable by one, red led glows and comparing it to the Secret_Number, if they're equal the green led will glow and the game will continue using the playAgain function.
The playAgain() function will get called when the user wins or when the game is over.
it's function is to display for the user to press the button if he wants to continue playing and then the game repeats.
but if he doesn't want the arduino quits the void loop.
After checking that the number is not the correct one, check if the attemps equal 4 and the game is over.
If both conditions weren't true, this means the game continues and the lcd displays a hint for the user whether the last number he entered is smaller or larger the number he's looking for.
after one attempt, clear the user_input variable and start the loop again.
The best way to win at the game is to do the BinarySearch algorithm.
At first I wired the circuit, uploaded the code and tested it.
After that I made a cardboard box for the components, started by cutting the cardboard and making spaces for each component, then glued the box together.
Then started mounting the components on the carboard.
I didn't know how the random() function works but I found the documentation here.
At first I wrote the line that has The random() function in void setup which always generated the same number and I didn't know why, it turned out that the arduino random() generates random numbers in a certain sequence 7 9 5 ... so when I wrote the code in void setup, it always returned the first number from the sequence 7, this is why I had to write the random() in void loop.
While wiring I used a push button but after I made the cardboard I found out that it would be so hard for me to use the push button so instead I used on/off switch.
When comparing the value returned from the keypad to the Secret_Number, the condition was always not satisfied even if I entered the same number, I tired using type casting to cast the keypad char to int :
((int)user_input ) == Secret_Number
but this returned the ascii value of the number I entered, I didn't know this until I used the serialprint to print (int)user_input, after that I checked the ascii table, found out that zero starts at 48 then I subtracted 48 from (int)user_input.
We made a smart Home that turns on/off a lamp depending on the light condition and turns on/off a fan if the weather is Hot.