It had been quite some time since the old arduino has been at it again… For our class Rest of You. We were asked to connect to Processing via Arduino. I first started by doing a very simple lab. The code for this first lab is pasted below. Essentially I was able to control the intensity of an LED with a potentiometer.
/*
Newer versions of Arduino like Arduino UNO may affect uploading.
If you get this error:
avrdude: stk500_recv(): programmer is not responding
You must specify the Board type under the Tools Menu
You must also specify the Serial Port which is also under the Tools Menu
Here is the link to where I got this information:
http://arduino.cc/en/Guide/Troubleshooting#upload
*/
const int ledPin = 9; // LED Pin
int analogValue = 0; // Pot Value
int brightness = 0; // PWM pin that the LED is on
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// declare the led pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
analogValue = analogRead(A0); // read the pot value
brightness = analogValue /4; //divide by 4 to fit in a byte
analogWrite(ledPin, brightness); // PWM the LED with the brightness value
Serial.println(brightness); // print the brightness value back to the serial monitor
}
Afterwards I hooked up a few photocells and read three analog inputs. These three inputs were then mapped to an RGB scale. At first I was getting a very muddled color but after some fusing around by reviewing the numbers that were being sent to the serial monitor I adjusted the mapped numbers. I used Cornflake to read the serial numbers from all three inputs clearly. This is very nice and helps us differentiate the values for all three inputs quickly. It is in Beta, but so far, I have had no problems with it.
Hardwares used for this lab included 3 photocells as our analog sensors, an Arduino, a breadboard, some hook-up wire, and because I didn’t have a lot of options I used whatever resistors I had that matched for consistency.
Code:
Arduino Sketch
const int redPin = A0; // this sensor controls the red color
const int greenPin = A1; // this sensor controls the green color
const int bluePin = A2; // this sensor controls the blue color
void setup()
{
Serial.begin(9600); // Serial is used to communicate
// begin sets the data rate in bits per second(baud)
}
void loop()
{
// these print data to the serial port for all input values
Serial.print(analogRead(redPin));
Serial.print(“,”);
Serial.print(analogRead(greenPin));
Serial.print(“,”);
Serial.println(analogRead(bluePin));
}
Processing Sketch
// This example code is in the public domain.
// http://arduino.cc/en/Tutorial/VirtualColorMixer
import processing.serial.*; // this imports the serial data coming from an arduino
float redValue = 0; // red value
float greenValue = 0; // green value
float blueValue = 0; // blue value
Serial myPort;
void setup() {
size(800, 800);
background(255);
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you’re using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don’t generate a serialEvent() unless you get a newline character:
myPort.bufferUntil(‘\n’);
}
void draw() {
// set the background color with the color values. This is what is drawn/What you see.
background(redValue, greenValue, blueValue);
}
void serialEvent(Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil(‘\n’);
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// split the string on the commas and convert the
// resulting substrings into an integer array:
float[] colors = float(split(inString, “,”));
// if the array has at least three elements, you know
// you got the whole thing. Put the numbers in the
// color variables:
if (colors.length >=3) {
// map them to the range 0–255:
redValue = map(colors[0], 0, 20, 0, 255);
greenValue = map(colors[1], 0, 20, 0, 255);
blueValue = map(colors[2], 0, 20, 0, 255);
}
}
}


