circuit for analog input © 2011 . All rights reserved.

Cracking Open the Old Arduino — Do you miss me?

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 con­nect to Pro­cess­ing via Arduino. I first started by doing a very sim­ple lab. The code for this first lab is pasted below.  Essen­tially I was able to con­trol the inten­sity of an LED with a potentiometer.

/*

 Newer ver­sions of Arduino like Arduino UNO may affect upload­ing.
 If you get this error:
 avr­dude: stk500_recv(): pro­gram­mer is not responding

 You must spec­ify the Board type under the Tools Menu
 You must also spec­ify the Ser­ial Port which is also under the Tools Menu
 Here is the link to where I got this infor­ma­tion:
 http://arduino.cc/en/Guide/Troubleshooting#upload

 */

const int led­Pin = 9;       // LED Pin
int analog­Value = 0;        // Pot Value
int bright­ness = 0;         // PWM pin that the LED is on

void setup() {
  // ini­tial­ize ser­ial com­mu­ni­ca­tions at 9600 bps:
  Serial.begin(9600);
  // declare the led pin as an out­put:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  analog­Value = analogRead(A0);      // read the pot value
  bright­ness = analog­Value /4;       //divide by 4 to fit in a byte
  analogWrite(ledPin, bright­ness);   // PWM the LED with the bright­ness value
  Serial.println(brightness);        // print the bright­ness value back to the ser­ial mon­i­tor
}

After­wards I hooked up a few pho­to­cells and read three ana­log inputs. These three inputs were then mapped to an RGB scale. At first I was get­ting a very mud­dled color but after some fus­ing around by review­ing the num­bers that were being sent to the ser­ial mon­i­tor I adjusted the mapped num­bers. I used Corn­flake to read the ser­ial num­bers from all three inputs clearly. This is very nice and helps us dif­fer­en­ti­ate the val­ues for all three inputs quickly. It is in Beta, but so far, I have had no prob­lems with it.

Hard­wares used for this lab included 3 pho­to­cells as our ana­log sen­sors, an Arduino, a bread­board, some hook-up wire, and because I didn’t have a lot of options I used what­ever resis­tors I had that matched for consistency.

circuit of analog input

plan view of circuit of analog input

Code:

Arduino Sketch

const int red­Pin = A0;      // this sen­sor con­trols the red color
const int green­Pin = A1;    // this sen­sor con­trols the green color
const int blue­Pin = A2;     // this sen­sor con­trols the blue color

void setup()
{
  Serial.begin(9600); // Ser­ial is used to com­mu­ni­cate
                      // begin sets the data rate in bits per second(baud)
}

void loop()
{
  // these print data to the ser­ial port for all input val­ues
  Serial.print(analogRead(redPin));
  Serial.print(“,”);
  Serial.print(analogRead(greenPin));
  Serial.print(“,”);
  Serial.println(analogRead(bluePin));
}

Pro­cess­ing Sketch

// This exam­ple code is in the pub­lic domain.
// http://arduino.cc/en/Tutorial/VirtualColorMixer

import processing.serial.*; // this imports the ser­ial data com­ing from an arduino

float red­Value = 0;        // red value
float green­Value = 0;      // green value
float blue­Value = 0;       // blue value

Ser­ial myPort;

void setup() {
size(800, 800);
background(255);

// List all the avail­able ser­ial ports
println(Serial.list());
// I know that the first port in the ser­ial list on my mac
// is always my  Arduino, so I open Serial.list()[0].
// Open what­ever port is the one you’re using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don’t gen­er­ate a seri­alEvent() unless you get a new­line char­ac­ter:
myPort.bufferUntil(‘\n’);
}

void draw() {
// set the back­ground color with the color val­ues. This is what is drawn/What you see.
background(redValue, green­Value, blue­Value);
}

void serialEvent(Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil(‘\n’);

if (inString != null) {
// trim off any white­space:
inString = trim(inString);
// split the string on the com­mas and con­vert the
// result­ing sub­strings into an inte­ger array:
float[] col­ors = float(split(inString, “,”));
// if the array has at least three ele­ments, you know
// you got the whole thing.  Put the num­bers in the
// color vari­ables:
if (colors.length >=3) {
// map them to the range 0–255:
red­Value = map(colors[0], 0, 20, 0, 255);
green­Value = map(colors[1], 0, 20, 0, 255);
blue­Value = map(colors[2], 0, 20, 0, 255);
}
}
}