Sunday, February 23, 2014

Project3 | Light Detector




Your children or your brothers or sisters having fear of darkness! Make a night light that automatically goes on when you turn off the lights. by using a light sensor, some wires, and LEDs you can easily create an automatic night light that will turn on when you turn off the lights.



Configuration



By following the diagram, you can notice the following:

The photocell (light sensor) is connected via there wires:
(1)Blue and black wires to one of photocell legs and (2) red wire to the other leg. A resistance is added along the photocell connected to ground (black wire). These wires are for:
·         Black wire à ground
·         Red wire à 5V
·         Blue wire à analog input A0 ( because photocell give analog data)

Code


1.       Some required definitions, which PIN the LEDs and photocell is connected to?
int LDR = 0;   //analog pin = A0
int LED1=3;
int LED2=4;
a.       Define light sensitivity, the threshold we want to control lights according to it, if the photocell senses a light less than this threshold, then turn LEDs on
int light_sensitivity1 = 300;
int light_sensitivity2 = 50;
b.      Define a variable to store the value which photocell senses
int LDRValue = 0;

2.       Set the LEDs pins to output mode, open the serial (optional) for output on screen
Serial.begin(9600);               
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);

3.       Start listen to the photocell, put what the sensor reads to LDRValue
LDRValue = analogRead(LDR);              Serial.println(LDRValue);  // cout << LDRValue;
delay(50);                                               

4.       We will turn on the first LED, if the value read by the sensor less than the first light sensitivity (i.e. 300), the second will turn on, only when the LDRValue is less than the second light sensitivity ( more darkness)
if (LDRValue < light_sensitivity1){
     digitalWrite(LED1, HIGH);
     
     if (LDRValue < light_sensitivity2) 
           digitalWrite(LED2, HIGH);
     else
           digitalWrite(LED2, LOW);
}else{
        digitalWrite(LED1, LOW);
      }

Saturday, February 8, 2014

Project1-Phase2 | "Say what Requested! "


In the previous phase of the project, we automatically display the number on the 7-segement display in ascending manner. Now we want to add another feature, using Serial Port/USB, we want to send the number to display from computer to Arduino(i.e. the 7-segement will display the number I choose, when I send 1 from computer, I want the 7-segment to draw the 1 number, send 2 then display number 2 and so on) how to do that?




Send Data using Serial Port/USB connection
Using the USB connection between Arduino and the computer, we can send data from computer to Arduino. How to do that? We will modify in the previous code in the following meaner:  We will add 4 functions:

(1)Serial.begin(9600);
Serial is a part of existence library in Arduino. Using serial means you want a connection between the PC and Arduino. To ask Arduino to start a connection between him and the PC we call this function begin() .. begin() take a great number, the question will rise immediately what is this number? This number is the speed of connection, 9600 bits per second .. each second serial can send 9600 bits... it’s like your network download speed.

(2)  Serial.println(“Enter the number you want to display please”);
This is a message which Arduino will send to the computer request the user to enter a number..Well we need a receiver!

(3)Serial.read();
This is what receives bytes from user, on Arduino, to read over the serial connection, we use this function as our cin operator.
As cin need a variable to receive the input in ( cin >> x ) then her two we need a variable to hold the  user input.. well give me a variable.. But what does the function return? What’s the type? It returns the first byte received as integer.
int byteReceived= Serial.read();
With this byte, I can do many things, I can print it (send it back to computer), I can do condition on it to light up the LED and we can us it in our project to print the specific number to the 7 segment. But I can to receive it in char/byte format, as the returned type can be casting to these types.

(4)  Serial.available() this function this is there any one send data over the serial port? If you send a data this function return true, you then can take what sent by calling Serial.read()
The complete code:
Using the function of pervious project, we will add on the setup the Serial.begin(9600) in the loop we will make some if conditions that restrict the output to the display on the 7-segement.
if (Serial.available()){
char byteReceived= Serial.read();
if (byteReceived == ‘0’ ) // if you receive a character 0
lightUpSegement(0); // display zero on the 7-segement
if (byteReceived == ‘1’ ) // if you receive a character 1
lightUpSegement(1); // display one on the 7-segement
if (byteReceived == ‘2’ ) // if you receive a character 2
lightUpSegement(2);   // display on the 7-segement
...
}

Or simply by using for-loop:
if (Serial.available()){
for(int i=0; i<9 ; i++)
if (byteReceived == (char)(i+‘0’) ){ // if you recive the ith character
lightUpSegement(i);   // display this ith number
break; //enough execution :)
}
}

How to open connection with Arduino and send data?

(1) Form the Arduino IDE, Serial Monitor

(2)You will find Arduino asks you to enter a number:

(3)Enter the number in the text box and click send
(4) Check Your Robot:

Saturday, February 1, 2014

Project1-Phase1 | "Robo,Say the Numbers!"

Phase1: Basic Counter

When you need to teach your child the numbers you start to teach them numbers in ascending order. “Hey .. first number is 1.. 1 means one thing.. When I have 1 and another 1 that means we have two things.. Then 2 is a number means 1+1 .. etc” You then will ask him/her to counting up the numbers: “Tell me the numbers from 0 to 9”.. and the child start to be like a counter ( 0,1,2,3,4,5,6,7,8,9). Our first robot in this series, is this child.. We make a child with 7 figures (as bulb) to say the numbers..


The 7-segments Display
Robot’s fingers, they aren't like ours it’s more simple.. It’s a collection of LEDs which make the basic element in most of display monitors in our digital life.
We see the 7 segment every day! We see and use it everywhere in our digital life and get benefits from it, without the 7 segment we think the life will be a little bit horrible!

The 7-segments this is our story hero, the figures, the basic element of display monitors in calculators digital alarm clock, gas station & price changer signs, stock market, on microwave oven display screens, عداد اجرة التاكسي... etc.
This element to be used correctly we have to understand its physical construction, we have to understand how we can give a power to it, and how to control the lighting of each segment?

Anatomy of 7-Segment Display
When you are going to explore the component, you will figure that there are 10 pins in the 7 segment; each PIN wired one segment! 10 PINs and you said 7 Segments? There is a decimal point, the small dot on the bottom right, and then now we have another pin for dot, 8 PINs know now. There are still two PINs? For what these PINs for, you will know soon.
First, we need to identify PIN1 in order to probably assign a power (+) and ground (-) to the pins. This information is found on the technical specifications sheet also known as the "data sheet”. Second, every PIN lights up a segment; we can give each segment a letter:
PIN1 -> E  , PIN2 -> D , PIN4 -> C , PIN5 -> DP "Decimal Point" , PIN6 -> B, PIN7 -> A
PIN1
E
PIN2
D
PIN4
C
PIN5
DP
PIN6
B
PIN7
A
For PIN3, PIN8, they will be connected to the ground if you work with "7 segment Common Anode" but if you work with "7 segment Common Cathode “then you have to connect the these PINs to the power(5V).. What’s the common cathode and anode stands for? Follow this tutorial for more information


Now, we know the parts of the 7 segment and we are ready to learn how to use it and light them up!

Build the Robot
Hardware Components:
·         Arduino chip ( we use Arduino Uno)
·         Breadboard
·         1x 7segement display
Steps to make robot ready:
(1) Put the 7segment in the bread board 
(2)Start to connect Arduino Digital PINs with 7-segement PINs, in the following manner:
PIN  in the 7segmant
Digital Pin in the ARDUINO
1
2
2
3
3
5V *
4
4
5
-
6
5
7
6
8
5V *
9
7
10
8

* Because we use common cathode 7-segment display
(3) Connect the ARDUINO  to PC to start uploading the sketch to Arduino

Teach the Robot

We are ready now to teach the robot(write and upload the sketch to the Arduino). Well, first we want to identify our problem:
We want to display the numbers starting from 0 to 9 in the 7-segement display.
To start search of where we can start write the code, we want fist to identify the PINs we will turn it on and off (the PINs of Arduino). By the way we used in connecting the Arduino to 7-segement, we can say:
int segementPINS[]={2,3,4,6,7,8,9}; // 1 2 4 6 7 9 10
                                    // E D C B A F G
This array say that the PIN number 2 in Arduino, connected to the E segment, so when we send a high signal to this pin, E segment will turn one(note: that we are using common cathode, that the segment won’t turn on except we send a low signal..make sure for your 7-segment component). The same is  for the other elements of the array.
To display the number on 7 segments, we have to say for each number which segment will turn on/off. For instance to display the number 0, all segments should be turned on except segment G. so we can draw a truth table for 7-segments with numbers

E
D
C
B
A
F
G
0
1
1
1
1
1
1
0
1
0
0
1
1
0
0
0
2
1
1
0
1
1
0
1
3
0
1
1
1
1
0
1
4
0
0
1
1
0
1
1
5
0
1
1
0
1
1
1
6
1
1
1
0
1
1
1
7
0
0
1
1
1
0
0
8
1
1
1
1
1
1
1
9
0
1
1
1
1
1
1
We store this table in 2D array:
int nums[10][7]={{1,1,1,1,1,1,0}, /* 0 */
                 {0,0,1,1,0,0,0}, /* 1 */
                 {1,1,0,1,1,0,1}, /* 2 */
                 {0,1,1,1,1,0,1}, /* 3 */
                 {0,0,1,1,0,1,1}, /* 4 */
                 {0,1,1,0,1,1,1}, /* 5 */
                 {1,1,1,0,1,1,1}, /* 6 */
                 {0,0,1,1,1,0,0}, /* 7 */
                 {1,1,1,1,1,1,1}, /* 8 */
                 {0,1,1,1,1,1,1}  /* 9 */
                 };
To turn the segment on we simply make a function “void lightUpSegement(int num)” that take the desired number to display num. this num is the first index of nums array(i.e. the row number). If we want to display number 3 ( num=3) then we want row=3 ({0,1,1,1,1,0,1}) we have this row, access each element by a for loop if the element equals 1 then we have to turn the segment on:
digitalWrite(segementPINS[j],LOW); // turn on : common cathode
Else (equals  to 0)
digitalWrite(segementPINS[j],HIGH); // turn off : common cathode
In the loop function, make a for loop that calls lightUpSegement function for each number between 0 and 9, and of course set some delay between each display
for(int j = 0 ;j <10 ; j++ )
   {
    lightUpSegement(j);
    delay(2000);
   }
Don’t forget to state in the setup function that segementPINS is used as output PINs
void setup(){
  for(int i = 0 ; i<7 ; i++){
  pinMode(segementPINS[i], OUTPUT);
  }
 }

And we are done! Verify, upload.. and your robot start to counting the numbers : )