Presentation is loading. Please wait.

Presentation is loading. Please wait.

3.1 Serial Communication Part 1

Similar presentations


Presentation on theme: "3.1 Serial Communication Part 1"— Presentation transcript:

1 3.1 Serial Communication Part 1
Using the Serial Port to communicate with an external application.

2 3.1 Serial Communication Send data from a Potentiometer and a Photoresistor through the Serial Port and utilize the data in Processing software.

3 3.1 Learning Objectives Receiving and Transmitting data over Serial
Using Processing to Receive Information Save Information on an external file

4 3.1 Functions - Arduino mySerial.available(); Start Serial Monitor
mySerial.begin(); Start Serial Communication mySerial.write(); Write data to the port mySerial.read(); Read Data mySerial.listen() Choose which port to listen to

5 3.1 Connecting to Applications
Arduino has no built in interface This is why we use serial communication to send our data to an external Application Arduino > Serial Port; Processing xCode Visual Studios Unity Etc.

6 3.1 Connecting to Applications
Arduino also cannot save data This data has to be sent to an application that has the ability to save to a file to your computer Example Arduino > Serial Port > Processing > .csv File

7 3.1 Bits and Bytes Group of binary digits or bits operated on as a unit. Byte is a unit of memory size One byte = Eight bits One byte can hold an integer between 0 and 255.

8 3.1 Byte to Char A Character on your keyboard can be represented as one byte. An ASCII Table is an encoding that represents letters as a byte. Example: (byte) = 65(int) = A(char)

9 3.1 Sending and Receiving Bytes
Serial communication allows us to only send one byte of data at a time. For example if were trying to send three different bytes of data, we cannot send them all at once over the serial port. We have to send them one at a time.

10 3.1 Sending and Receiving Bytes
Therefore its hard to obtain these bytes in proper order in our external application. To overcome this issue we send and receive these bytes through an Array to keep our data nice and organized when it’s being sent through the serial port.

11 3.1 Photoresistor A photoresistor is a light-controlled variable resistor The more light the less resistance is provided We are able to get an analog reading from this sensor

12 3.1 Breadboard Diagram Hardware: Potentiometer Photoresistor 330 Ohm Resistor

13 3.1 Variable Initialization - Arduino
Initialize the variables at the top of the program #include <SoftwareSerial.h> SoftwareSerial mySerial(0,1); // Initialize location (Pin Hole) for the Temperature sensor int xValue; int yValue; int zValue; int myByte[3] = {0, 0, 0}; int potValue; int photoValue; int POT_PIN = A0; int PHOTO_PIN = A1; CODE

14 3.1 Loop Function - Arduino
Start reading our analog values from our sensors and map them to to be sent through the serial port. void loop() { potValue = analogRead(POT_PIN); potValue = map(potValue, 0, 1023, 0, 255); photoValue = analogRead(PHOTO_PIN); photoValue = map(photoValue, 0, 1023, 0, 255); xValue = potValue; yValue = photoValue; zValue = 30; CODE

15 3.1 Variable Initialization - Arduino
Initialize our variables to 0 in our setup void setup() { // Start our software serial communication mySerial.begin(9600); xValue = 0; yValue = 0; zValue = 0; potValue = 0; photoValue = 0; } CODE

16 3.1 Loop Function - Arduino
Loop function will run on the Arduino infinite times till reset or power is disabled. // Going to write the byte in order in a for loop we initialize the values into our loop. myByte[0] = xValue; myByte[1] = yValue; myByte[2] = zValue; for (int i = 0; i < 3; i++) { mySerial.write(myByte[i]); } delay(100); CODE

17 3.1 Variable Initialization - Processing
Now we launch our application Processing to receive all our variables import processing.serial.*; String PORT = “COM3”; Serial mySerial; PrintWriter mLogger; int xValue; int yValue; int zValue; int myByte[3] = {0, 0, 0}; float mValue; float mInterpolate; float time; CODE

18 3.1 Void Setup - Processing
In our setup we establish our connection by selecting which port we are going to connect void setup() { // Establish connection through our port mySerial = new Serial(this, PORT, 9600); if (mySerial.available() == 0) { println("Port connection established..."); } delay(2000); mValue = 0; mInterpolate = 0; ... CODE

19 3.1 Void Setup - Processing
... xValue = 0; yValue = 0; zValue = 0; mLogger = createWriter("DataOutput.csv"); mLogger.println("Time" + "," + "x" + "," + "y" + "," + "z" + "," + "mVal" + "," + "mInter"); size(500, 500); } CODE

20 3.1 Void Draw - Processing After initialization the variables, we then assign specific functions in order to program. void draw() { println("x:", xValue, "y:", yValue, "z:", zValue, "mVal: ", radians(mValue), "mInter: ", radians(mInterpolate)); mLogger.println(time + "," + xValue + "," + yValue + "," + zValue + "," + radians(mValue) + "," + radians(mInterpolate)); ... CODE

21 3.1 Void Draw - Processing After initialization the variables, we then assign specific functions in order to program. ... while (mySerial.available() > 3) { for (int i = 0; i < 3; i++) { myByte[i] = mySerial.read(); } // After all the values are stored align them properly in each variable to be used later on. xValue = myByte[0]; yValue = myByte[1]; zValue = myByte[2]; CODE


Download ppt "3.1 Serial Communication Part 1"

Similar presentations


Ads by Google