What about this assignment?
This week hopefully I started felt more comfortable with programming, and my plans are start with Processing just to get the concept of applications and interfaces and then choose another platform with more resources, like openframeworks for example, to explore a little bit.
Processing
Software used:So first I started having a look at Processing witch has a friendly interface, very similiar with Arduino IDE and lot of examples that could make your life easier. Our instructor Ferdi gave us a quick introduction to processing + input devices, also he explained basically how a serial communication should work using as example Neil’s Python and C codes.
After that I did some examples using Processing and some of my boards just to get used with this.
This is a nice tutorial to learn how to connect some outputs and inputs:
Processing + eletronics tutorial
At the end I made two examples, one using as input a step response board and other using as an output a hello.bus.45.bridge, both turning a LED on and off. See the result and commented code below:
This is the Arduino IDE code that I ran in hello.bus.bridge.45 board to make LED blink.
char val; // Data received from the serial port | |
int ledPin = 0; // Set the pin to digital I/O 4 | |
void setup() { | |
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT | |
Serial.begin(9600); // Start serial communication at 9600 bps | |
} | |
void loop() { | |
if (Serial.available()) { // If data is available to read, | |
val = Serial.read(); // read it and store it in val | |
} | |
if (val == 'H') { // If H was received | |
digitalWrite(ledPin, HIGH); // turn the LED on | |
} else { | |
digitalWrite(ledPin, LOW); // Otherwise turn it OFF | |
} | |
delay(100); // Wait 100 milliseconds for next reading | |
} |
Then this is the Processing code that talks with my board to make the LED blink:
import processing.serial.*; | |
Serial port; // Create object from Serial class | |
float x = 640; // Circle parameters | |
float y = 400; | |
float radius = 100; | |
void setup() { | |
size(1280, 800); | |
noStroke(); | |
frameRate(10); | |
// Open the port that the board is connected to and use the same speed (9600 bps) | |
port = new Serial(this, "/dev/tty.usbserial-A9024P2C", 9600); | |
} | |
void draw() { | |
background(255); | |
if ( dist( mouseX, mouseY, x, y ) < radius ) { // If mouse is over circle, | |
fill(0); // change color and | |
port.write('H'); // send an H to indicate mouse is over | |
} else { // If mouse is not over circle, | |
fill(204); // change color and | |
port.write('L'); // send an L otherwise | |
} | |
ellipse( x, y, 2*radius, 2*radius); | |
} |
And this is the Processing code to receive the values of my step response sensor and “turn the LED on and off” on the image.
import processing.serial.*; | |
Serial myPort; // Create object from Serial class | |
int val; // Data received from the serial port | |
int sensorData; // Data received from the serial port with 1,2,3,4 framing numbers filtered out | |
int low = 0; | |
int med = 0; | |
int high = 0; | |
float value1 =0; | |
float value2 =0; | |
int byte1 = 0; | |
int byte2 = 0; | |
int byte3 = 0; | |
int byte4 = 0; | |
int up_low = 0; | |
int up_high = 0; | |
int down_low = 0; | |
int down_high = 0; | |
float x = 640; // Circle parameters | |
float y = 400; | |
float radius = 100; | |
PImage img1; | |
PImage img2; | |
void setup() | |
{ | |
size(1280, 760); | |
// I know that the first port in the serial list on my mac | |
// is always my FTDI adaptor, so I open Serial.list()[0]. | |
// On Windows machines, this generally opens COM1. | |
// Open whatever port is the one you're using. | |
myPort = new Serial(this, "/dev/tty.usbserial-A9024P2C", 9600); | |
img1 = loadImage("ledoff.jpg"); // Take images that were imported to the sketch before | |
img2 = loadImage("ledon.jpg"); | |
} | |
void draw() | |
{ | |
while (myPort.available() > 0) { // If data is available | |
byte1 = byte2; | |
byte2 = byte3; | |
byte3 = byte4; | |
byte4 = up_low; | |
up_low = up_high; | |
up_high = down_low; | |
down_low = down_high; | |
down_high = myPort.read(); | |
if ((byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 == 4)){ // Filter out the framing numbers: 1,2,3,4 | |
float up_value = 256*up_high + up_low; | |
float down_value = 256*down_high + down_low; | |
value1 = (up_value + (1023 - down_value))/2.0; | |
println("VALUE1 IS " + value1); //print to the screen | |
} | |
if ( value1 >= 0 && value1 < 1700 ) { // If nothing touching step response sensor | |
image(img2, 0, 0); // show image with LED off | |
} else if ( value1 > 1700 ) { // If you touch step response sensor and increase values received, | |
image(img1, 0, 0); // show image with LED on | |
} | |
} | |
} |
What's next?
Since I saw some nice projects using Openframeworks I decided that I have to learn it! So first step was follow the installation tutorial, very well made:
http://www.openframeworks.cc/setup/xcode/
By now I’m still following some ready made examples and trying to understand C++… I hope that until the end I can update some extra application here with OF…
← week 13 / network and communication | week 15 / applications and implications → |
---|