lab: sensors /lighting and serial communication
[the breadboard set up] *two IR sensors with red and blue leds

[the ball made in Processing]

[ documentation]
SERIAL COMMUNICATION (PROCESSING) PART 1.
************************************************
import processing.serial.*;
Serial port; // The serial port
int[] serialInArray = new int[2]; // ***Where we'll put what we receive
int serialCount = 0; // ***A count of how many bytes we receive
int xpos, ypos; // Starting position of the ball
boolean firstContact = false; // Whether we've heard from the microcontroller
void setup() {
size(256, 256); // Stage size
noStroke(); // No border on the next thing drawn
// Set the starting position of the ball (middle of the stage)
xpos = width/2;
ypos = height/2;
// Print a list of the serial ports, for debugging purposes:
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Keyspan adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
port = new Serial(this, Serial.list()[2], 9600);
port.write(65); // Send a capital A to start the microcontroller sending
}
void draw() {
background(255);
fill(255,200,150);
// Draw the shape
ellipse(xpos, ypos, 20, 20);
// If no serial data has beeen received, send again until we get some.
// (in case you tend to start Processing before you start your
// external device):
if (firstContact == false) {
delay(300);
port.write(65);
}
}
void serialEvent(Serial port) {
// if this is the first byte received,
// take note of that fact:
if (firstContact == false) {
firstContact = true;
}
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = port.read();
serialCount++;
// If we have 2 bytes:
if (serialCount > 1 ) {
xpos = serialInArray[0];
ypos = serialInArray[1];
// print the values (for debugging purposes only):
// println(xpos + "\t" + ypos + "\t" );
// Send a capital A to request new sensor readings:
port.write(65);
// Reset serialCount:
serialCount = 0;
}
}
SERIAL COMMUNICATION (ARDUINO) PART2
********************************************
int potPin = 0; // Analog input pin that the potentiometer is attached to
int firstpotValue = 0; // value read from the pot
int secondpotValue=0;
int firstled = 9; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9
int secondled=10; //PWM pin that the LED is on.
int inByte = 0; // ***incoming serial byte
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
analogWrite(firstled, firstpotValue/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
analogWrite(secondled, secondpotValue/4); //PWM the LED with the pot value.
/*
Serial.print(firstpotValue); // print the pot value back to the debugger pane
Serial.print('\t');
Serial.println(secondpotValue);
Serial.print('\t');
delay(10); // wait 10 milliseconds before the next loop
*/
if (Serial.available() > 0) { //***set and start to send the serial
// get incoming byte:
inByte = Serial.read();
firstpotValue = analogRead(0); // ***see if there is serial number and read the pot value
secondpotValue = analogRead(1);
//( read first analog input, divide by 4 to make the range 0-255:) tom did
// IR sensor is around 500 range, divide by 2 to make the rang 0-255
firstpotValue = analogRead(0)/4;
// delay 10ms to let the ADC recover:
delay(10);
// read second analog input, divide by 4 to make the range 0-255:
secondpotValue = analogRead(1)/4;
// read switch, multiply by 255
// so that you're sending 0 or 255:
// send sensor values:
Serial.print(firstpotValue, BYTE);
Serial.print(secondpotValue, BYTE);
}
}