Midterm Prototype Notes
Following is some of the initial Arduino and Processing code for use with our first midterm protype. The interface will entail cubes of seven colors and four slots. The player will need to choose the correct combination of colors (a la Mastermind) to solve the puzzle. Their selection will be recorded based on data from photocells located at the bottom of each slot--the glass cubes will return different results based on their relative opacity based on color. The history of their selections and the results (on the model, again, of Mastermind feedback) will be printed to a Processing interface, along with a photographic puzzle that will be solved when the correct selections are made.
Initial Arduino Code
int firstSensor = 0; // first photocell sensor (connect to analog pin 0)
int secondSensor = 1; // second photocell sensor (connect to analog pin 1)
int thirdSensor = 2; // third photocell sensor (connect to analog pin 2)
int fourthSensor = 3; // fourth photocell sensor (connect to analog pin 3)
int firstLED = 2; // first LED pin (connect to digital pin 2)
int secondLED = 3; // second LED pin (connect to digital pin 3)
int thirdLED = 4; // third LED pin (connect to digital pin 4)
int fourthLED = 5; // fourth LED pin (connect to digital pin 5)
int inByte = 0; // incoming serial byte
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
pinMode(firstSensor, OUTPUT); // set the first LED pin to be an output
pinMode(secondSensor, OUTPUT); // set the second LED pin to be an output
pinMode(thirdSensor, OUTPUT); // set the third LED pin to be an output
pinMode(fourthSensor, OUTPUT); // set the fourth LED pin to be an output
}
void loop()
{
//turn on the four LEDs
digitalWrite(firstLED, HIGH); // turn on the first LED
digitalWrite(secondLED, HIGH); // turn on the second LED
digitalWrite(thirdLED, HIGH); // turn on the third LED
digitalWrite(fourthLED, HIGH); // turn on the fourth LED
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
// read first analog input
firstSensor = constrain(analogRead(0)/4,0,255);
// delay 10ms to let the ADC recover:
delay(10);
// read second analog input
secondSensor = constrain(analogRead(1)/4,0,255);
// delay 10ms to let the ADC recover:
delay(10);
// read third analog input
thirdSensor = constrain(analogRead(2)/4,0,255);
// delay 10ms to let the ADC recover:
delay(10);
// read fourth analog input
fourthSensor = constrain(analogRead(3)/4,0,255);
// send sensor values:
Serial.print(firstSensor, BYTE);
Serial.print(secondSensor, BYTE);
Serial.print(thirdSensor, BYTE);
Serial.print(fourthSensor, BYTE);
}
}
Extremely Stripped-down Processing Code
This is the very basic Processing code for our team to begin working with.
import processing.serial.*;
Serial port; // The serial port
int firstSensor; // Initialize the first sensor variable
int secondSensor; // Initialize the second sensor variable
int thirdSensor; // Initialize the third sensor variable
int fourthSensor; // Initialize the fourth sensor variable
int[] serialInArray = new int[4]; // An array for our results
int serialCount = 0; // A count of how many bytes we receive
boolean firstContact = false; // Whether we've heard from the microcontroller
void setup() {
// Print a list of the serial ports, for debugging purposes:
println(Serial.list());
port = new Serial(this, Serial.list()[2], 9600); // define the serial port
port.write(65); // Send a capital A to start the microcontroller sending
}
void draw() {
// 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 4 bytes:
if (serialCount > 3 ) {
firstSensor = serialInArray[0];
secondSensor = serialInArray[1];
thirdSensor = serialInArray[2];
fourthSensor = serialInArray[3];
// print the values to the serial monitor:
println(firstSensor + "\t" + secondSensor + "\t" + thirdSensor + "\t" + fourthSensor);
// Send a capital A to request new sensor readings:
port.write(65);
// Reset serialCount:
serialCount = 0;
}
}