Sooyeon Jeong

Interface and Application Programming

For the interface and application programming, I used Processing to interface with the Arduino IDE. I followed the tutorial from the Sparkfun website and it was pretty simple. For the application, I made a simple button with a text that changes between “Night Light” and “Day Light” when the rectangular button is clicked.

When the button indicates the word “Night Light”, the LED on the board acts like a night light, i.e. turning on when the surrounding environment is dark and turning off when the environment is bright. When the button indicates the word “Day Light”, the behavior becomes opposite, i.e. turning on when the environment is bright and turning off when it is dark. Whenever the button is clicked, the Processing application sends '0' or '1' to the Arduino IDE, in which determines the behavior for the LED. I've worked with Processing in the past so it felt pretty comfortable and straightforward.

Below is the code for the Processing application:

import processing.serial.*;

Serial myPort; // Create object from Serial class

String val; // Data received from the serial port

int rectX, rectY, rectSize = 90; // Diameter of rect

color rectColor, rectHighlight;

boolean rectOver = false, nightLight = true;



void setup() {

String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port

myPort = new Serial(this, portName, 9600);

size(200, 200);

rectColor = color(0);

rectHighlight = color(51);

textSize(32);

}



void draw() {

clear();

update(mouseX, mouseY);

if (rectOver) {

fill(rectHighlight);

} else {

fill(rectColor);

}

stroke(255);

rect(10, 10, 180, 100);

if (nightLight) {

fill(100, 100, 100);

text("Night Light", 15, 70);

} else {

fill(100, 100, 100);

text("Day Light", 15, 70);

}

if ( myPort.available() > 0) { // If data is available,

val = myPort.readStringUntil('\n'); // read it and store it in val

}

}



void update(int x, int y) {

if (overRect(10, 10, 180, 100) ) {

rectOver = true;

}

}



void mousePressed() {

if (rectOver) {

if (nightLight) {

myPort.write('1');

nightLight = false;

} else {

myPort.write('0');

nightLight = true;

}

}

}



boolean overRect(int x, int y, int width, int height) {

if (mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) {

return true;

} else {

return false;

}

}



Below is the code for the Arduino IDE:

#include <SoftwareSerial.h>

int sensorPin = 3; // select the input pin for the potentiometer

int ledPin = 7; // select the pin for the LED

int sensorValue = 0; // variable to store the value coming from the sensor



boolean nightLight = true;



char val;



SoftwareSerial serial(0,1);



void setup() {

// declare the ledPin as an OUTPUT:

pinMode(ledPin, OUTPUT);

pinMode(sensorPin, INPUT);

serial.begin(9600);

}



void loop() {

if (serial.available()) { // If data is available to read,

val = serial.read(); // read it and store it in val

}

if (val == '1') { // If 1 was received

nightLight = false;

} else {

nightLight = true;

}

// read the value from the sensor:s

sensorValue = analogRead(sensorPin);



serial.println(sensorValue);

if (sensorValue >= 1000) {

if (nightLight) {

digitalWrite(ledPin, HIGH);

} else {

digitalWrite(ledPin, LOW);

}

} else {

if (nightLight) {

digitalWrite(ledPin, LOW);

} else {

digitalWrite(ledPin, HIGH);

}

}



delay(100);

}



In this video, you can see the behavior of the LED changing based on the button click event.