Set up the Lab and Read and Send the Serial Data
The first step of the lab was to set up the board and connect the analog sensor to analog pin 0 as we had done in the analog lab.
Program the Arduino module to read the analog sensor and print the results to the Serial monitor. Instead of using Serial.println(), however, use Serial.write() to send the sensor value serially.
The Code
void setup() {
Serial.begin(9600);
}
void loop() {
int analogValue = analogRead(A0) /4; // read the pot value
Serial.write(analogValue); // print the value in the serial monitor as a binary value
}
}
void loop() {
int analogValue = analogRead(A0) /4; // read the pot value
Serial.write(analogValue); // print the value in the serial monitor as a binary value
}
Read Data Using Processing
The first thing we need to do is to import the Processing Serial Library. Choose the Sketch menu in Processing, then
Import Library...-->serial, or you can type:import processing.serial.*;
To use the serial library, create an instance of the library in a global variable.
Serial myPort; //the serial port
Then, In the setup() method, set the window size, and use the serial library to get a list of the serial ports:
void setup () {
size(800, 600); // window size// List all the available serial ports
println(Serial.list());
}
size(800, 600); // window size// List all the available serial ports
println(Serial.list());
}
If you run what you’ve typed so far, you should get a list of the serial ports in the monitor pane that looks a bit like this on a mac.
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
[0] ”/dev/tty.usbserial-A6004osg”
[1] ”/dev/cu.usbserial-A6004osg”
[2] ”/dev/tty.Bluetooth-Modem”
[3] ”/dev/cu.Bluetooth-Modem”
[4] ”/dev/tty.Bluetooth-PDA-Sync”
[5] ”/dev/cu.Bluetooth-PDA-Sync”
Experimental: JNI_OnLoad called.
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
[0] ”/dev/tty.usbserial-A6004osg”
[1] ”/dev/cu.usbserial-A6004osg”
[2] ”/dev/tty.Bluetooth-Modem”
[3] ”/dev/cu.Bluetooth-Modem”
[4] ”/dev/tty.Bluetooth-PDA-Sync”
[5] ”/dev/cu.Bluetooth-PDA-Sync”
Experimental: JNI_OnLoad called.




