16. Interface and application programming


Group assignment

Link to the group assignment


Individual assignment


I decided to make an interface to graph the light intenisty changes. So I loaded the code i wrote in week 11 to the board. The initial aim for this assignment was to test the photosensor for my final project. Because beside the time released unlocking for The RE_LEASH, i also wanted the lock to be triggered by darkness and unlock.

Then I used arduino serial monitor to register the range of values from the sensor reading. It seems to be 0 - 1000. So I assumed it would be from 0 to 1024.

Then, I used Processing to create a GUI. Processing is easy to use, and because the interface is very similar to Arduino IDE, I would have an advantage there.

Arduino and Processing can both communicate through serial, so, it should be easy to send information from the board to Processing. I started with an example code, and got a black screen.

So I went back to the code and started changing it. Similar to arduino, Serial must be declared and started, also some variables. There is also a void method.

import processing.serial.*;
Serial thisPort; // Create object from Serial class
int xPos = 0; // starting position for graphing
int xPos = 0; // starting position for graphing

void setup()
{
    size(800,700); //make our canvas 800 x 700 pixels
    println(Serial.list()); //show available ports
    thisPort = new Serial(this, Seria.list()[0], 9600);
    thisPort.bufferUntil('\n'); //set the port to store incoming data
    background(0); //set background color for the window
}

I changed some things like the window (canvas) size. The Serial.list() line displays all available ports, so the user can find the right one for the serial communication and set it with Serial.list()[0]. I also changed the background color of the interface.

When i changed the colors, these where the several the outputs that i got.

void serialEvent(Serial thisPort)

This is the routine that is triggered when serial is activated

String inString = thisPort.readStringUntil('\n'); 

This line of code store the data it reads from the serial

inByte = float(inString); //convert serial data to a flat type number
println(inByte); //show value
inByte = map(inByte, 0, 40, 0, height); //re-maps a number from one range to another

This lines covert the data to a float number, then shows the value for debugging and re-maps the value to the range 0-Height which is the size of the canvas

Video

This video shows the graphical output of the lightsensor.

Download

Download board

Download Lightsensor-Processingcode