Skip to content

Instantly share code, notes, and snippets.

@dashalom
Created October 27, 2014 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dashalom/555b2e7348d3f6c9a11a to your computer and use it in GitHub Desktop.
Save dashalom/555b2e7348d3f6c9a11a to your computer and use it in GitHub Desktop.
Hydrate.me Processing with calibation and timer
import processing.serial.*;
import com.onformative.yahooweather.*;
YahooWeather weather;
int updateIntervallMillis = 30000;
Serial myPort; // The serial port
float sensor = 0;
float inc = 0;
float a= 0.0;
PImage skin;
PImage alarm;
PImage bottom;
PImage logo;
//Timer
int sel;
int startingTime;
int sel_warning;
int seconds=0;
int real_seconds=0;
int minutes=0;
int rectX=100;
int rectY=30;
int initial_seconds = 0;
int initial_real_seconds = 0;
int initial_minutes = 0;
int calibration_seconds = 30;
boolean click_next = false;
float prev_water_val = 0;
boolean timer1_is_set = false;
boolean timer2_is_set = false;
boolean timer3_is_set = false;
void setup () {
size(400, 800);
skin = loadImage("midtermUI_BOTTOM.png");
alarm = loadImage("midtermUI_1.png");
bottom = loadImage("bottom.png");
logo = loadImage("logo_splash.png");
textMode(SHAPE);
//the WOEID of New York
weather = new YahooWeather(this, 12761335, "c", updateIntervallMillis);
println(Serial.list());
String portname = "/dev/tty.usbmodemfa131";
myPort = new Serial(this, Serial.list()[5], 9600);
myPort.bufferUntil('\n');
}
void draw () {
background(255);
if(millis() < calibration_seconds*1000) {
// give 30 seconds to calibrate the sensor to the bottle
background(46, 219, 250);
image(logo,0,0);
int t = calibration_seconds - int(millis()/1000);
fill(255);
textAlign(CENTER);
textSize(25);
text("Step 1", width/2, 150);
textSize(16);
text("Place your empty bottle on the coaster", width/2, 180);
textSize(25);
text("Step 2", width/2, 300);
textSize(16);
text("Fill your bottle up and place on coaster", width/2, 330);
textSize(25);
text("Step 3", width/2, 450);
textSize(16);
text("Hurry, you only have 30 seconds", width/2, 480);
textSize(25);
text(nf(t,2) + " secs left", 200,700);
} else {
//water_wave
if (frameCount%10==0) {
inc = inc+90;
}
// display lines to fill bottle
for (int i=120; i<280; i++) {
a=map(i, 120, 280, 0, 360);
a=a+inc;
stroke(#76f2ff, 100);
line(i, height-140, i, (height-sensor)+10*sin(radians(a)));
stroke(#76f2ff, 80);
line(i, height-140, i, (height-sensor)+10*cos(radians(a)));
if (height-sensor>=560) {
background(255);
fill(46, 219, 250);
textSize(18);
text("hydrate me!", 200, 418);
}
}
//load bg_img
image(skin, 0, 0);
//weather
weather.update();
textAlign(CORNERS);
fill(#2dc1e2);
textSize(14);
text("Temperature: "+weather.getWindTemperature()+" C", 15, 115);
text("Humidity: "+weather.getHumidity()+" %", 15, 134);
textSize(12);
text(weather.getCityName()+" , "+weather.getRegionName(), 15, 152);
//Set timers
//btn_line
noStroke();
fill(255);
rect (133, 734, 2, 66);
rect (267, 734, 2, 66);
//10min start
if (sel == 1 && timer1_is_set) {
seconds = (millis() - startingTime) / 1000;
real_seconds=seconds%60;
minutes = seconds / 60;
image(bottom, 0, 734);
fill(255);
textSize(25);
textAlign(CENTER);
text( nf(minutes, 2) + " : " + nf(real_seconds, 2), 200, 777);
//after 10min
if (seconds >= 5) {
sel_warning=1; // warning state;
myPort.write("r");
println("WRITING R");
println("5 seconds have passed!");
fill(46, 219, 250);
rect(0, 0, 400, 800);
image(alarm, 0, 400);
fill(255);
textSize(14);
textAlign(CENTER);
text("Drink some water.\n Click on the screen once you're done", width/2, height-200);
}
}
//20min start
else if (sel==2 && timer2_is_set) {
seconds = (millis() - startingTime) / 1000;
real_seconds=seconds%60;
minutes = seconds / 60;
image(bottom, 0, 734);
fill(255);
textSize(25);
textAlign(CENTER);
text( nf(minutes, 2) + " : " + nf(real_seconds, 2), 200, 777);
//after 20min
if (seconds >= 10) {
sel_warning=1; // warning state;
println("10 seconds have passed!");
myPort.write("r");
fill(46, 219, 250);
rect(0, 0, 400, 800);
image(alarm, 0, 400);
fill(255);
textSize(14);
textAlign(CENTER);
text("Drink some water.\n Click on the screen once you're done", width/2, height-200);
}
}
//30min start
else if (sel==3 && timer3_is_set) { //30min start
seconds = (millis() - startingTime) / 1000;
real_seconds=seconds%60;
minutes = seconds / 60;
image(bottom, 0, 734);
fill(255);
textSize(25);
textAlign(CENTER);
text( nf(minutes, 2) + " : " + nf(real_seconds, 2), 200, 777);
//after 30min
if (seconds >= 15) {
sel_warning=1; // warning state;
println("15 seconds have passed!");
myPort.write("r");
println("WRITING R");
fill(46, 219, 250);
rect(0, 0, 400, 800);
image(alarm, 0, 400);
fill(255);
textSize(14);
textAlign(CENTER);
text("Drink some water.\n Click on the screen once you're done", width/2, height-200);
}
}
//not start
else {
sel_warning=0;
if(timer1_is_set) {
fill(#2dc1e2);
rect(0,734,133,height-734);
} else if(timer2_is_set) {
fill(#2dc1e2);
rect(134,734,133,height-734);
} else if(timer3_is_set){
fill(#2dc1e2);
rect(267,734,133,height-734);
}
textSize(18);
textAlign(CENTER);
//10min button
fill(255);
text("10min", 66, 774);
//20min button
fill(255);
text("20min", 199, 774);
fill(255);
text("30min", 334, 774);
}
}// end of click next
}
void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
if(inString.equals("hydrate")){
myPort.write("a");
println("got hydrate");
}else{
// convert to an int and map to the screen height:
float[] values = float(inString.split(","));
if(values.length > 2) {
float inByte = values[2];
float sens_low = values[0];
float sens_high = values[1];
//println(inByte);
//inByte = constrain(inByte,4,20);
inByte = int(map(inByte, sens_low, sens_high,0, 580));
inByte = constrain(inByte, 0 , 580);
inByte = lerp(prev_water_val, inByte, 0.1);
sensor = int(inByte);
println(sensor, sens_low, sens_high);
// if no bottle or empty bottle or very little value, make arduno lights blink
if(sensor < 100){
myPort.write("e");
println("WRITING E");
} else {
// if we are not on the warning screen
if(sel_warning != 1){
myPort.write("a");
println("WRITING A");
}
}
prev_water_val = inByte;
}
}
}
}
void mouseReleased() {
if (mouseY >=734 && mouseY <=height) {
if (mouseX >= 0 && mouseX <=133) {
sel=1; //10min select
timer1_is_set = true;
timer2_is_set = false;
timer3_is_set = false;
} else if (mouseX >= 134 && mouseX <=266) {
sel=2; //20min select
timer1_is_set = false;
timer2_is_set = true;
timer3_is_set = false;
} else if (mouseX >= 267 && mouseX <=width) {
sel=3; //30min select
timer1_is_set = false;
timer2_is_set = false;
timer3_is_set = true;
}
}
// if you click on select a timer, allow user to change timing
if(mouseY < 734 && mouseY > 694) {
timer1_is_set = false;
timer2_is_set = false;
timer3_is_set = false;
//seconds = 0;
startingTime = millis();
}
if (sel_warning==1) {
//sel=0;
// reset sel warning to make lights return to normal after reminder is done
sel_warning = 0;
startingTime = millis();
//seconds = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment