xxxxxxxxxx
109
//Declaring variables
let pressure = 0; // Initialize pressure sensor value received from Arduino
let mood = "Neutral"; // Initial mood value
let duckNeutral, duckHappy, duckSad; // Daisy's moods
let rainImage, toiletImage, rainbowImage, menuImage; // Background images
function preload() {
// Load Daisy Pics
duckNeutral = loadImage("duckneutral.png");
duckHappy = loadImage("duckhappy.png");
duckSad = loadImage("ducksad.png");
// Load background pics for each mood
rainImage = loadImage("rain.jpg");
toiletImage = loadImage("toilet.jpg");
rainbowImage = loadImage("sunshine.jpg");
menuImage = loadImage("phonto.png");
projectFont = loadFont("Wigglye.ttf");
}
function setup() {
createCanvas(windowWidth, windowHeight); // Full screen
textAlign(CENTER, CENTER); // Centered text
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
} //Responsiveness
function draw() {
// Update background and mood based on pressure value received from Arduino through serial communication handshake
if (pressure > 900) {
mood = "Happy!!";
background(rainbowImage); // Set background to sunshine and rainbows - if she's happy
} else if (pressure > 680) {
mood = "Neutral";
background(toiletImage); // Set background to toilet -if she's neutral
} else if (pressure > 3) {
mood = "Sad";
background(rainImage); // Set background to rain - if she's sad
} else {
background(menuImage); // Main background - sunshine and rainbows but with introductory text
}
// Display the right Daisy facial expression pic based on her mood from the strangle
let duckSize = 600; // Her size
let xCenter = width / 2 - duckSize / 2; // Center her horizontally
let yCenter = height / 2.3 - duckSize / 2; // Center her vertically
if (mood === "Happy!!") {
image(duckHappy, xCenter, yCenter, duckSize, duckSize); // if the pressure value defines the mood as happy, show her happy pic
} else if (mood === "Neutral") {
image(duckNeutral, xCenter, yCenter, duckSize, duckSize); // if the pressure value defines the mood as neutral, show her neurtal pic
} else if (mood === "Sad") {
image(duckSad, xCenter, yCenter, duckSize, duckSize); // if the pressure value defines the mood as sad, show her sad pic
}
// Mood meter moving bar
drawMoodMeter(mood);
// Display pressure value (for debugging)
fill(255);
textSize(16);
text(`Pressure: ${pressure}`, width / 2, height - 50);
}
function drawMoodMeter(mood) {
let meterWidth = 400; // Bar width
let meterHeight = 55; // Bar thickness
let x = width / 2 - meterWidth / 2; // Centering it
let y = height - 125; // Vertical position
// Background color of the mood meter
fill(200);
noStroke();
rect(x, y, meterWidth, meterHeight);
// Mood level (changes color from red to green)
let moodColor;
if (mood === "Happy!!") {
moodColor = color(112, 243, 158); // Green
} else if (mood === "Neutral") {
moodColor = color(251, 246, 113); // Yellow
} else if (mood === "Sad") {
moodColor = color(216, 64, 66); // Red
}
fill(moodColor);
rect(x, y, map(pressure, 0, 1023, 0, meterWidth), meterHeight); // Map pressure values to width of mood meter
// Mood text label
fill(0);
textSize(23);
textAlign(CENTER, CENTER);
text(`Mood: ${mood}`, x + meterWidth / 2, y + meterHeight / 2);
textFont(projectFont);
}
// Serial communication handshake
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
}
function readSerial(data){
pressure = int(data);
}