int interval = 10; // retrieve feed every 60 seconds; int lastTime; // the last time we fetched the content
int LED_red = 0; int LED_green = 0; int LED_blue = 0;
int light = 0; // light level measured by the lamp
Serial port; color c; String cs;
String buffer = ""; // Accumulates characters coming from arduino
PFont font;
void setup() { size(640,480); frameRate(2); // we don't need fast updates
font = loadFont("HelveticaNeue-Bold-32.vlw"); fill(255); textFont(font, 32); // IMPORTANT NOTE: // The first serial port retrieved by Serial.list() // should be your arduino. If not, uncomment the next // line by deleting the // before it, and re-run the // sketch to see a list of serial ports. Then, change // the 0 in between [ and ] to the number of the port // that your arduino is connected to. //println(Serial.list()); String arduinoPort = Serial.list()[0]; port = new Serial(this, arduinoPort, 9600); // connect to arduino
// write the colour string to the screen text("sending", 10, 340); text(cs, 200,340);
text("light level", 10, 380); rect(200, 352,light/10.23,28); // this turns 1023 into 100
if (n <= 0) { lastTime = millis(); }
port.write(cs); // send data to arduino
if (port.available() > 0) { // check if there is data waiting int inByte = port.read(); // read one byte if (inByte != 10) { // if byte is not newline buffer = buffer + char(inByte); // just add it to the buffer } else {
// newline reached, let's process the data if (buffer.length() > 1) { // make sure there is enough data
// chop off the last character, it's a carriage return // (a carriage return is the character at the end of a // line of text) buffer = buffer.substring(0,buffer.length() -1);
// turn the buffer from string into an integer number light = int(buffer);
// clean the buffer for the next read cycle buffer = "";
// We're likely falling behind in taking readings // from arduino. So let's clear the backlog of // incoming sensor readings so the next reading is // up-to-date. port.clear(); } } }