April, Tuesday 16th 2013
Assigment:
Write an application that interfaces with an input &/or output device
So this week I had to make Week10 and Week12 all together, since I didn't make my sensor on Week10 (input devices).
I made the Hello.Reflect board and the objective I wanted to achieve was to make a graphic interface to interpret the values read by the light sensor.
This board uses a surface mounted Phototransistor and a LED that I won't be using for this assigment.
I wanted to use Processing to make the graphic interface, but first I used the Arduino software to program my board through my FabISP.
At first I used Neil's -C firmware to flash the microcontroller and then run the Python program he made as to test the sensor worked.
Here you can find a nice tutorial how to do this:
http://academy.cba.mit.edu/content/tutorials/11_Input_Devices/Input_Device_Examples_C.html
I had no problem flashing the board through the FabISP, but I had some problems running the Python program, for some reason I couldn't read the port my board was connected to. The board is connected through a FTDI cable.
I finally realised that I didn't had the FTDI drivers installed, so after installing them the Python program ran.
You can find the drivers here: http://www.ftdichip.com/Drivers/VCP.htm
To list the ports in Mac run this on Terminal: ls /dev/tty.* or ls /dev/cu.*
Here are some screenshots of the process:
Only Bluetooth Ports listed (before installing the FDTI divers)
USBserial listed (after installing the FDTI divers)
Python program running
After testing the Python readings I went to try and program my board with Arduino.
The basic AnalogReadSerial Arduino example will do the job, but you have to modify two things:
- Add the SoftwareSerial Library
- Modify the value in the SensorPin depending in the position of your sensor connected to the microcontroller
#include <SoftwareSerial.h>
SoftwareSerial mySerial(1, 2); // RX, TX
int sensorPin = 2; // analog input pin to hook the sensor to
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
mySerial.begin(9600); // initialize serial communications
}
void loop() {
sensorValue = analogRead(sensorPin); // read the value from the sensor
mySerial.println(sensorValue); // print value to Serial Monitor
delay(0); // short delay so we can actually see the numbers
}
Arduino program
Arduino sensor readings
After succesfully having the arduino program and the readings working OK, I started with the Processing Program.
Here is the code after debuggin and lots of help from Guillem here in FabLab Barcelona:
import processing.serial.*;
Serial myPort; // The serial port
float xPos = 0; // horizontal position of the graph
String sensorReading = "";
void setup () {
size(800, 600); // window size
// List all the available serial ports
println(Serial.list());
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
background(#EDA430);
}
void draw () {
// nothing happens in draw. It all happens in SerialEvent()
}
void serialEvent (Serial myPort) {
sensorReading = myPort.readStringUntil('\n');
if (sensorReading != null) {
sensorReading = trim(sensorReading);
int sensorReadingValue = int(sensorReading);
float sensorReadingValueClean = map(sensorReadingValue, 0, 1023, 0, 100);
println(sensorReadingValueClean);
float yPos = height - sensorReadingValueClean;
// draw the line in a pretty color:
stroke(#A8D9A7);
line(xPos, height, xPos, height - sensorReadingValueClean);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
// clear the screen by resetting the background:
background(#081640);
}
else {
// increment the horizontal position for the next reading:
xPos++;
}
}
}
Processing Code
And the final result is this: