nice to meet you...

I like a heap of things. Writing. Thinking. Photography. Film-making. Conspiring. Aspiring. Dreaming. Reading. Watching. Listening. Electronics.

Tuesday, October 2, 2012

Arduino & Processing get together!

Ok, so this is my first "electronics" post, if you know me personally you won't be surprised to that I'm a geek! And proud of it too :)

Anyway so I scoured the Internet for tutorials on how to do this and I found heaps but they done ages ago. Which meant that since Arduino updated the programming software some of the 'code' was redundant. Furthermore I had scour even more to figure out what had changed and how to change it!

I don't have any programming training, everything I know was either found on Google.co.nz; arduino.cc; processing.org; and this PDF http://tinyurl.com/ArduinoProcessingWorkshopPDF.


In that picture is everything working fine, but how did I get there?

Well I used the schematic for the potentiometer setup from here, I did use the arduino.cc sites one but the readings were all over the place and sporadic. So my guess is that the 'tie-down resistor' helps to stabilise things.

here is the pic from that site:
I only did the potentiometer part, not the black and red wires that run up the side of the breadboard.

What you will need:

  • Arduino
  • Breadboard
  • Potentiometer/ variable resistor
  • 1K resistor
  • Arduino and Processing programs on your computer


So once you are all setup and wired up you want to upload your 'code' to the Arduino. This is the code I'm using:

Arduino:


int sensorValue = 0;
int sensorPin = A0; // this is the pin to which we
// connect the potentiometer
void setup() {
Serial. begin(9600);
}
void loop() {
sensorValue = analogRead(sensorPin)/4; // the potentiometer’s range is from
// 0-1023 but we only need numbers from
// 0-255.

Serial.write(byte(sensorValue));
delay (100);
}


The bit I highlighted in yellow is what has changed from old code that is floating around on the web. They basically just reformatted it. Yep.

Now onto Processing...

I suggest you have a play around on Processing before doing this tutorial. But you can just do this as well, no one is stopping you. I'm still trying to get my head around the important parts of a program and the parts that are changeable so that instead of drawing lines (as we will be doing here) I can make each line 'represent' an Image and have an interactive slideshow happening... but I digress...

The processing code:


import processing.serial.*;
Serial port;
int sensorValue;
void setup() {
size(1000, 500);
//noStroke();
frameRate(10);
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
// choose the port that the Arduino is
// connected to, on a Macintosh choose
// the tty.usbserial port
}
void draw() {
if (0 < port.available()) {
sensorValue = port.read();
}
background(204);
for (int i = 0; i < sensorValue+1; i++) {
// this is the “for” loop, see pp.61
// in the book “Processing” by Reas and Fry
stroke(255-i, 0, i); // choose the color of the line based on the
// potentiometer’s sensorValueue
line (i*5, 100, (i*5)+10, 400); // draw the lines based on the
// potentiometer’s sensorValueue
}
println(sensorValue); // prints out sensorValueues from Arduino board in
// the console, just to see what Processing
// actually receives from the Arduino board
}

I'm not at the stage yet where I can explain to you what every bit of code means and why it is there, but I will get there someday soon! :D

If everything is connected properly you should be able to turn your potentiometer and a gradient of vertical lines from red to purple should move across your screen.

That's it I think!

Grace and Peace,
Andy

p.s. please comment if you have any questions or ideas!

p.p.s. also comment if you want to collaborate on Arduino/ processing related stuff, the possibilities are endless!

No comments:

Post a Comment