This week’s assignment was to create a smart device that has more than one input and more than one output. Initially I wanted to make an lcd game that is quite similar to the No internet ( dinosaur and trees) game but after I had done my research , it became obvious that it would take me a lot of time to understand / write the code needed . that’s why I searched for other games and settled on the MATH SOLVER game . the basic idea is that it gives the user a random equation and the user should enter the answer then press ‘#’ for indicating the end of his answer. If the answer is correct , the lcd displays a correct sign and the score that has been incremented but if the answer is wrong the lcd displays wrong and the last score without incrementing it , also the buzzer emits sound in case of a wrong answer . after a couple of seconds the lcd displays another equation to the user .
I used Arduino software to write my code , this software is mainly used in C language . the code is devided into three sections
- First section : in this section you first include all the libraries needed , the libraries are the driver ( file_prog.c / file_int.h) that the previous developer made to interface with the component and the atmegaxx , the file.c includes all the APIs ( functions this component might need ) for example in the LCD case it needs a function for
· Lighting the lcd at first
· Printing string
· Printing data of a variable
· Taking action ( clear lcd / shift right or left)
The file _int.h includes the bodies of the APIS and the variable definitions needed to avoid magic numbers .
- the definitions/ declarations of variables are written after include , you can define a variable in two ways either to wirte the type of the variable + the name + = + value ( optional) + semicolon
int k = 5 ;
or you define it using hash # + define + name + space + value ( optional)
# define k 5
Second part : void setup in Arduino is executed only one time , so that’s why we usually put the pins mode in this section because you need to set a pin either as INPUT / OUTPUT only once in the running code . but you can add anything in the void setup that you want to execute only once . the pinMode functions has two parameters ( the first is the pin number , the second is the pin’s mode either input / output ) if the pin is input then that means that I am taking a reading from it using digitalRead(pin_number) / analogRead(pin_number)
If I want to set a pin as a value from me then you set the pinMode as output and use digitalWrite ( pin_number , HIGH/LOW) / analogWrite ( pin_number , value from 0 to 1023)
Third part:
Void loop () is the part that keeps repeating unless you stop the Arduino , some functions used are the delay () function which takes one parameter ( the time in milliseconds ) and stops the next line in the code from being executed until this empty loop counter ends
You can also create a function’s body after the void loop and call this function in the void loop each time you want the actions of it to execute .
- First include the libraries
· I included the libraries of liquid crystal i2c & keypad library & EEPROM library
- The I initialized the variable that I would be using during my code ( unsigned long would be used for millis function )
- The array of type string has 16 problems that are displayed randomly
- The int solutions array is in order with each problem in the problems array that means problem[1] ‘s solution is solution[1] for example
- Signed int key array is the array we would store the entered answer in
- Int solcounter is the score that is incremented each time an answer is right
In void setup >> I initialized the LCD using it’s functions Then I called the initiate function and the start function , the random seed function is to initiate the random function and it was adviced to put the random seed on an unused anolog pin because the noise would put a different number each time as a seed and it would randomize better
Then I set pin 13 for the buzzer as output and pin 10 for the slider as input
In the void loop >> I called key_print() function and then I made a delay using millis function between the entering of each key .
Function initiate game
- First see the time at which this function started using millis
- Then set the cursor of the lcd to row 0 & col 0
- Print “welcome to ”
- Set the cursor to row 1 and col 0
- Print “norhan’s game ”
- Then we made a delay using millis for 2 seconds and we cleared the lcd and displayed
“high score ” which is the highest score stored in the eeprom up to this point
EEPROM.read function takes one parameter address at which the value is stored
- The do while loop is stuck in the do until the while condition is not satisfied that’s why our condition is another function change_state
· This function checks if the slider is still at HIGH then it returns still 0 but if the slider is low ( opening the game in our case ) then it returns 1 and it is free to execute the following lines .
-------------------------------
Start_Game function
- First it loops in the key array elements to make sure that key[0] which is for the sign either 1 or -1 but it’s initialized here as one unless changed later by the user
- After key[0] it enter the number digits as zeros
- Clearing the lcd
- Then we take a random number generated by the random function ( parameter minimum , parameter maximum) and store it in variable randnumber
- We display the equation that represents the problems array element (randnumber)
- And we set the cursor for row 1 & col 0 for entering the answer later on
-----------------
Fill_array function
Takes a value from fuction key_print and stores in the key array based on it’s order but it starts entering from 1 because 0 is for the sign
-----------------
Key_print function
It takes a character from the keypad function then If this character has any ascii code except 0 it enters the switch loop that checks on the digit case either ( * (-) , 1 ,2, 3,4,5,6,7,8,9,#(end of answer))
In each case it prints the digit and stores it’s value in the key array (by sending it to fill_array function)based on it’s location
The counter increments one to indicate the number of digits entered , when the user enter ‘#’ the lcd clears and it calls function check_answer();
----------------------
Check_answer function
It checks the value based on the number of digits entered ( using the counter variable ) and compares it with element solution[randnumber] , if answer is right is it displays correct and increments the score then displays the score , the a delay using millis , if wrong it displays wrong it displays wrong and a buzzer emits sound and the total score is also displayed
After checking if the answer is right or wrong it calls the Start function to display another question
The keypad connection
The rows from 1 to 4 are connected on pins 9 , 8 ,7 , 6
The columns from 1 to 3 are connected on pins 5, 4 ,3
The lcd connection for lcd with i2c shield
· The vcc to 5v on Arduino
· The gnd on the gnd on Arduino
· The SDA on A4 arduino
· The SLA on A5 arduino
The buzzer( emitts sound only in wrong answers )
- One negative (shorter) leg to the gnd
- One positive (longer) leg to the pin 13
The slider( to start the game)
- The outer pins one to gnd and one to vcc
The middle pin on pin 10 arduino
This week I learned a lot from my classmates , some of them had a good coding background that could be easily noticed by their work , for example one class mates made and type def struct that has four types two int and two pointer
Typedef struct
{
Int pin ;
Int value ;
Int * previous ;
Int * next ;
} led ;
So he created a type called led that could be used to declare a variable
Example led Led1 ;
LED1.pin = 5 that means that I stored in the pin value of led1 5
I tried as best as I can to explain the coding principle behind each line to my classmates ,
For example I explained that
the pinMode function has two parameters ( the first is the pin number , the second is the pin’s mode either input / output ) if the pin is input then that means that I am taking a reading from it using digitalRead(pin_number) / analogRead(pin_number)
If I want to set a pin as a value from me then you set the pinMode as output and use digitalWrite ( pin_number , HIGH/LOW) / analogWrite ( pin_number , value from 0 to 1023)
I also tried to debug the error that they faced during coding
Two important notes that I learned this week from my instructor are
- that inorder to stop the rotation of the dc motor you have to state the two pins connected from the H-bridge to the adruino as low I thought because I am using only one direction I need only to state LOW one pin
- also notice that the relay has three pins for each voltage input( 5v – 12v -24) that needs to be adjusted otherwise the relay wont work
My final project has lcd implementation , so after this week I know which library to include and how to connect it and the functions for the lcd used while coding .
The password detecting was quite cool because the number stayed for 0.5 seconds then disappeared into a star also the smart home was fun to make esciaplly with my team mate
We struggled a lot to put it all together for both the manual and automatic mode but I worked at the end so the effort was worth it 😊 .