Asynchronous Serial Communication Lab
Part 1: Completing the Lab
I was able to complete the lab as shown in the video below.
Codes:
Arduino:
const int switchPin = 2; // digital input
void setup() {
// configure the serial connection:
Serial.begin(9600);
// configure the digital input:
pinMode(switchPin, INPUT);
}
void loop() {
// read the sensor:
int sensorValue = analogRead(A0);
// print the results:
Serial.print(sensorValue);
Serial.print(",");
// read the sensor:
sensorValue = analogRead(A1);
// print the results:
Serial.print(sensorValue);
Serial.print(",");
// read the sensor:
sensorValue = digitalRead(switchPin);
// print the results:
Serial.println(sensorValue);
}
Processing:
import processing.serial.*;
Serial myPort;
float fgcolor = 0;
float xpos, ypos;
void setup(){
size(800, 600);
println(Serial.list());
// I know that the first port in the serial list on my computer
// is always my Arduino module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('\n');
}
void draw(){
background(#243780); // blue background
fill(fgcolor);
// Draw the shape
ellipse(xpos, ypos, 20, 20);
}
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
if (myString != null) {
println(myString);
}
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ','));
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
}
// add a linefeed at the end:
println();
if (sensors.length > 1) {
xpos = sensors[0];
ypos = sensors[1];
// the pushbutton will send 0 or 1.
// This converts them to 0 or 255:
fgcolor = sensors[2] * 255;
}
}
Video:
I was able to complete the lab as shown in the video below.
Codes:
Arduino:
const int switchPin = 2; // digital input
void setup() {
// configure the serial connection:
Serial.begin(9600);
// configure the digital input:
pinMode(switchPin, INPUT);
}
void loop() {
// read the sensor:
int sensorValue = analogRead(A0);
// print the results:
Serial.print(sensorValue);
Serial.print(",");
// read the sensor:
sensorValue = analogRead(A1);
// print the results:
Serial.print(sensorValue);
Serial.print(",");
// read the sensor:
sensorValue = digitalRead(switchPin);
// print the results:
Serial.println(sensorValue);
}
Processing:
import processing.serial.*;
Serial myPort;
float fgcolor = 0;
float xpos, ypos;
void setup(){
size(800, 600);
println(Serial.list());
// I know that the first port in the serial list on my computer
// is always my Arduino module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('\n');
}
void draw(){
background(#243780); // blue background
fill(fgcolor);
// Draw the shape
ellipse(xpos, ypos, 20, 20);
}
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
if (myString != null) {
println(myString);
}
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ','));
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
}
// add a linefeed at the end:
println();
if (sensors.length > 1) {
xpos = sensors[0];
ypos = sensors[1];
// the pushbutton will send 0 or 1.
// This converts them to 0 or 255:
fgcolor = sensors[2] * 255;
}
}
Video:
Part 2: having some fun with processing. I switched to two potentiometers and a light sensor. One potentiometer controls x, the other y, and both control an aspect of the color of the ball. The light sensors change the background color, one sensor controls the color black while the other controls white. They mix into grey when in neutral. Code is roughly the same as from the lab with a few changed fields.
Video:
Comments
Post a Comment