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:




No comments:
Post a Comment