You are on page 1of 5

Physical)Computing)End)Assignment)

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";

//initialise serial communication with arduino


myPort = new Serial(this, Serial.list()[2], 9600);
myPort.bufferUntil('\n');
//pre-load all the images
for(int i=0;i<373;i++){
j=i+1;
if(j<=9) imageName = "pizza_0000" + j + ".jpg";
if(10<=j && j<100) imageName = "pizza_000" + j + ".jpg";
if(100<=j) imageName = "pizza_00" + j + ".jpg";
movieImage[i] = loadImage(imageName);
}
//load the image for the reset buttons for later use
btn = loadImage("returnW.png");
btnC = loadImage("returnBW.png");
}

// The draw function controls the animation sequence.


void draw(){
//k is a counter variable that stores the number of frame to display
k = frame +1;
/*draws the relevant image to the window
if k reaches 371 the image should stop at frame 372
since it is the last image in the sequence
the reset button also shows up*/
if(k>372){
image(movieImage[372],0,0,width,height);
resetBtn();
/*as long k havent reached 371 it means
we have not yet reached the end of our photo sequence
the value stored in k can keep increasing
in order to display the rest of the images*/
}else{
image(movieImage[k],0,0,width,height);
}
}

//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));
}
}
}

/* This function used to convert the character received from the


serial port (Arduino), and converts it to a number */
int integerFromChar(char myChar) {
if (myChar < '0' || myChar > '9') {
return -1;
}else{
return myChar - '0';
}
}
//resets the frame number and set the displayed image back to the
first one
void reset(){
frame = 0;
image(movieImage[0],0,0,width,height);
}

)
)

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)
)
)

You might also like