The assignment this week was to build a project using an Arduino that is connected at least to two input components and two output components....
The project (FREEEEze game) should have an enclosure make of cardboard or recycled scraps....
The idea of my project is to make a game that can be played by let's say 5 to 7 people in a closed room... Try to remember your childhood games and special that one which is called (كهربا شد الكبس ) or something like that ... The main object is to not move from you position until the other person touches you...
My game is explained in this video.....>
The final out come ^^
I used:
For coding: I used Arduino IDE
Components:
A slide switch (input)
A motion sensor (RCWL-0516)(input)
2x16 LCD screen with the I2C module(output)
3 LEDs (1xgreen, 1xyellow, 1xred)(output)
3x 220ohms resistors
A piezo buzzer(output)
Arduino Uno R3
Breadboard
Jumpers
Used cardboard for the enclosure..
The preparation process has three parts:
Understanding how the project works.
Hardware connections (circuit design).
Coding
Understanding how the project works:
The program will start when you connect it to tehe power source whether it was the USB cable or a 5V adaptor.....
The LCD shows a message that the game will start in 5 secs a counter will appear on the screen with a peep sound from the buzzer...
If the slide switch is already on the game will start immediately but it it is off the LCD screen will show a message "SlideToStart".. the game will start but it will give the user 3 seconds before the motion detection..
if the user is not moving the LCD screen will show a message "WhereRU" until the user moves, it lights up the green LED with a peep sound from the buzzer and then shows "detected" on the LCD..
and this will be repeated until the three LEDs are lighted, now the game is over and this will be shown on the LCD screen...
to restart the game the user needs to slide the switch to turn of the LEDs and then slide it back to start the game...
Hardware connections:
Connection of the motion sensor
Connection of the Slide switch
Connection of the LCD screen
Connection of the buzzer and the LEDS
The colour code and connections is EXPLAIND but it will destroy your eye sight HAVE FUN ^^.
Hardware connections:
(1) Connection of motion sensor:
the sensor has 5 pins but we will use 3 pins which are:
3V3 pin (which is connected to the 3.3V on the Arduino through the breadboard) (the red Jumper is connected to a brown jumper connected to an orange one to the 3.3V Arduino pin)
GND pin (which is connected to the Arduino ground through the breadboard)(The orange jumper is connected to a brown one on the breadboard which is connected to the GND rail which is connected to the Arduino ground through a grey jumper)
OUT pin (which is connected to pin 7 as an input to the Arduino)(the yellow jumper is connected to a black jumper on the breadboard to pin 7)
(2) Connection of the slide switch:
the switch has 3 pins:
GND (the blue jumper is connected to a black jumper on the breadboard to the ground rail)
VCC (the purple jumper is connected to a white jumper connected to 3.3v rail on the breadboard)
Signal (the grey jumper is connected to a yellow jumper to pin 12)
(3) Connection of the LCD:
the LCD with the I2C module has 4 pins :
GND (the orange jumper is connected to the ground pin on the Arduino which is next to the 5V pin)
VCC (the yellow jumper is connected to the 5V pin on the Arduino)
SDA (the green jumper is connected to A4 pin on the Arduino)
SCL (the blue jumper is connected to A5 pin on the Arduino)
(4) Connection of The buzzer and the LEDs
all of these components has two pins or terminals and frankly they have a positive and a negative terminal...
We will take the negative terminal of the buzzer and the LEDs and connect them to the ground rail on the breadboard. (The LEDs each one should be connected to a 220ohms resistor to avoid damaging it..)
As for the positive terminal:
Green LED >>>>>> pin 8
Yellow LED >>>>>> pin 9
Red LED >>>>>> pin 10
Buzzer >>>>>> pin 11
A full view of the connections
Coding:
Here you will find the actual used code and to understand it you will have these comments to guide your way through.....
As for the LCD screen I used the Library in this Tutorial..
/*A game created by Amr Y. Abdulrahiem as the assignment for Week 7 of the MakerDiploma
try it and if you have ideas to improve it or fix some lags or whatever i'd be more than happy to listen...
HAVE FUN>>>>*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//OUTPUTs + LCD screen
int greenled = 8;
int yellowled = 9;
int redled = 10;
int buzzer = 11;
// INPUTS
int motionsensor = 7;
int slideswitch = 12;
//INPUTS statusses
int switchstatus;
// INPUT read variables
int motionsensorread;
int flag2 = 0; // for the sensor..... you could use it or not...
int counter = 1; ///// A variable is made to count the tries and if it reaches 4 this will be game over...
void setup() {
Serial.begin(9600);
// preparing the LCD screen to receive what we need...
lcd.begin();
lcd.backlight();
lcd.clear();
lcd.setCursor(2, 0);
/////// defining the pins that is outputs and inputs
pinMode (greenled, OUTPUT);
pinMode (yellowled, OUTPUT);
pinMode (redled, OUTPUT);
pinMode (buzzer, OUTPUT);
pinMode (motionsensor, INPUT);
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Game_starts");
delay(5000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("In");
delay(3000);
lcd.clear();
lcd.setCursor(6, 0);
/////////////////////////A loop to count 5 seconds to begin the game
for (int c = 1; c <= 5; c += 1)
{
lcd.setCursor(6, 0);
lcd.print("!");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print(" ");
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
}
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Begin!!");
delay(3000);
// put your setup code here, to run once:
}
void loop() {
switchstatus = digitalRead(slideswitch); /// reads the switch status to see whether to start the game immediately or wait until the switch is slided.
if (switchstatus == 1)
{
motionsensorread = digitalRead(motionsensor); ///Reads the sensor signals as digital signals which is if there is something moving it will send (1) if not it will send (0)..
Serial.println(motionsensorread);
if (motionsensorread == 1)
{
if (counter == 1)
{
//flag2 = 1;
lcd.clear();
lcd.setCursor(2, 0); lcd.print("detected");
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
digitalWrite(greenled, HIGH); // indicates the user moved once..
digitalWrite(yellowled, LOW);
digitalWrite(redled, LOW);
}
if (counter == 2)
{
//flag2 = 1;
lcd.clear();
lcd.setCursor(2, 0); lcd.print("detected");
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
digitalWrite(greenled, HIGH);
digitalWrite(yellowled, HIGH); // indicates the user moved twice..
digitalWrite(redled, LOW);
}
if (counter == 3)
{
// flag2 = 1;
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("detected");
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
digitalWrite(greenled, HIGH);
digitalWrite(yellowled, HIGH);
digitalWrite(redled, HIGH); // indicates the user moved three times ..
}
Serial.print (" ");
Serial.print (counter);
counter = counter + 1; // increasing the counter by one .....
delay (3000);
Serial.print (" ");
Serial.println (counter);
}
if (motionsensorread == 0) //when nothing moooooves
{
// flag2 == 0;
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("WhereRyou??");
delay(500);
}
//////////////////////////////////when the counter reaches 4 which means the game is over...
if (counter >= 4 && motionsensorread == 1 )
{
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("GameOver!!!");
delay(5000);
}
if (counter >= 4 && motionsensorread == 0 )
{
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("GameOver!!!");
delay(5000);
}
}
else
/////////////////////////// to begin and to restart the game slide the switch ........
{
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Slidetostart");
digitalWrite(greenled, LOW);
digitalWrite(yellowled, LOW);
digitalWrite(redled, LOW);
delay(5000);
counter = 1; // the counter will begin again from 1 ....
}
// put your main code here, to run repeatedly:
}
Here I am trying to simulate the game ^^...
Things I 've learnt:
I didn't understand how to create a function and hwy we do them so I asked the Instructor Ahmad Ibrahiem about that and he told me all about functions that there are two type of functions one that can return you a value and another one that do something without sending you any values...
In the start of the week I asked how to make a push button act like a switch and Instructor Ahmad Ibrahiem told me to search "how to make a toggle switch Arduino" and i got the idea that you will use two variables to store the state of the LED (output) and the state of the push button (input) and use if statements to control the actions....
Challenges I faced:
The biggest challenge I faced while making this project is using this motion sensor and get the desired results, I think this is because it is very very sensitive....
What I did to overcome this challenge :
I kept changing the values that is set to the delay function so that it waits to a certain time to read the next value but this caused some lags in the actions to be taken but almost it is working so to improve it and remove these lags... we need to do use another function that I was introduced to by Instructor Maha Mortada which is " attachinterrupt()" which allows the microcontroller to do things in parallel like doing something and monitaring the inputs of the motion sensor.. for more info click here and go to Using Interrupts section.
Used another simple code (you can see it here) that used Flag variables to store the read of the sensor but I missed this up when implement this method to my code..
Title of Media
Title of Media