Professional Documents
Culture Documents
Light)the)Pizza!)
Kelly)Hsiao)s1521373)Juliette)Hoedemakers)s1423592)
)
)
Code))Processing)
)
)
/* Light a Pizza: Using Arduino and a photocell light sensor to
animate
Written by Kelly Hsiao and Juliette Hoedemakers on 24/01/2015
using Processing 2.2.1
Source code derived from :
http://processing.org/learning/topics/sequential.html
http://processing.org/discourse/beta/num_1267080062.html
Code based off:
http://arduinobasics.blogspot.nl/search/label/PhotoCell
Pictures captured from:
https://www.youtube.com/watch?v=17UMGlDHzX0
======================================================= */
//import serial information from arduino
import processing.serial.*;
Serial myPort;
String sensorReading="";
// Create the array that will hold the images
PImage[] movieImage = new PImage[374];
//delare variable(images) for the reset buttons
PImage btn;
PImage btnC;
//font for the text that will appear beside the reset buttons
PFont pizza;
//frame variable control which image is displayed
int frame = 1;
//some number and counter variables declared for later use
int j, i=1;
int k;
int count=0;
/* Setup the size of the window. Initialise serial communication with
Arduino
and pre-load the images to be displayed later on. This is done
only once. */
void setup(){
size(604,340);
//put the images in a string to display later
String imageName = "pizza_00000.jpg";
//set up the reset button that shows up in the end of the sequence
void resetBtn(){
image(btn,538,15);
//if mouse hovers over the button it changes and words appear beside
it
if(mouseX>538 && mouseX<588 && mouseY>15 && mouseY<65){
image(btnC,538,15);
pizza = loadFont("My-handwriting-22.vlw");
textFont(pizza);
text("Bake another pizza!", 324,48);
fill(255);
//if the reset button is pressed the program resets and you can make
another pizza
if(mousePressed)
reset();
}
}
//calls the data from arduino via serial port
void serialEvent (Serial myPort){
//read a byte from the seial port
sensorReading = myPort.readStringUntil('\n');
if(sensorReading != null){
sensorReading=trim(sensorReading);
if (sensorReading.length()<2){
frame = 0;
}else{
frame += integerFromChar(sensorReading.charAt(1));
}
}
}
)
)
Code))Arduino)
)
)
/*Light a Pizza: Using Arduino and photocell light sensor to animate
Written by Kelly Hsiao and Juliette Hoedemakers on 24/01/2015 using
Arduino 1.0.6
Code based off:
http://arduinobasics.blogspot.nl/search/label/PhotoCell */
//delare variables
//the photocell is inserted in pin 0
int photocellPin = 0;
//variables for light level values for later use
int lightLevel;
int cookLightLevel;
int oldLightLevel;
//initializing, only runs once
void setup(){
Serial.begin(9600);
//Only record down information when the received value on the sensor
is lower than 14
//Which is when the sensed light is really bright
if(analogRead(photocellPin)<14){
lightLevel = analogRead(photocellPin);}
oldLightLevel=lightLevel;
}
//execute over and over again
void loop(){
if(analogRead(photocellPin)<14){
lightLevel = analogRead(photocellPin);}
delay(10);
//Map the light level to produce a result between 1 and 374
cookLightLevel = map(lightLevel, 1, 14, 1, 374);
cookLightLevel = constrain(cookLightLevel, 1, 374);
//Only send a new value to the Serial Port if the cookLightLevel
changes
if(oldLightLevel == cookLightLevel){
//Do nothing if the old value and the new value are the same
}else{
//Update the oldLightLevel value for the next round
oldLightLevel=cookLightLevel;
//Send the adjusted Light level result to Serial port
Serial.println(cookLightLevel);
}
Fritzing)
)
)