xxxxxxxxxx
396
/*
EDPX4310 Project 1: Scan and Spin Reward System
October 2022
akio.correll
Credits:
- p5Serial Connect credit: https://github.com/p5-serial/p5.serialcontrol/releases
- P5SerialConnect code credit: https://editor.p5js.org/digitalcoleman/sketches/XYj6SrDjP
- Color code credit: https://editor.p5js.org/vidianindhita/sketches/rJpmUmlj-
- Game start credit: https://stackoverflow.com/questions/66540760/how-do-i-make-a-p5js-drawing-canvas-only-start-after-pressing-button
- wheel credit: https://www.classes.cs.uchicago.edu/archive/2021/spring/11111-1/color-wheel/index.html
- text rotation credit: https://p5js.org/examples/typography-text-rotation.html
- wheel of fortune credit: https://editor.p5js.org/codingtrain/sketches/vTAoPZw0c
*/
let serial;
//change these variables
let goalCount = 3;
let redName = "A";
let greenName = "K";
let blueName = "Mom & Dad";
let dataR = -0; //not zero or one
let dataG = -0;
let dataB = -0;
let buttonG = -0;
let buttonR = -0;
let buttonY = -0;
let latestDataR = "waiting for data";
let latestDataG = "waiting for data";
let latestDataB = "waiting for data";
let latestDatabG = "waiting for data";
let latestDatabR = "waiting for data";
let latestDatabY = "waiting for data";
let startButton = false;
let showGame = false;
let cardCount = 0;
let countRed = 0;
let countGreen = 0;
let countBlue = 0;
let scanText = "Slide the card in the slot and push the green button";
const red = "#FE2712";
const orange = "#FB9902";
const yellow = "#FEFE33";
const green = "#66B032";
const blueGreen = "#347C98";
const blue = "#0247FE";
const purple = "#8601AF";
const redPurple = "#C21460";
const size = 400;
let angle = 6.1;
let angleV = 0;
let angleA = 0;
let holdA = 0;
let trigger = 0;
function setup() {
createCanvas(500, 500);
angleMode(RADIANS);
serial = new p5.SerialPort();
serial.list();
serial.open("/dev/tty.usbmodem14101"); //change to match your port name!!
serial.on("connected", serverConnected);
serial.on("list", gotList);
serial.on("data", gotData);
serial.on("error", gotError);
serial.on("open", gotOpen);
serial.on("close", gotClose);
}
function serverConnected() {
print("Connected to Server");
}
function gotList(thelist) {
print("List of Serial Ports:");
for (let i = 0; i < thelist.length; i++) {
print(i + " " + thelist[i]);
}
}
function gotOpen() {
print("Serial Port is Open");
}
function gotClose() {
print("Serial Port is Closed");
latestData = "Serial Port is Closed";
}
function gotError(theerror) {
print(theerror);
}
function gotData() {
//most important to change sensors.
let currentString = serial.readLine();
trim(currentString);
if (!currentString) return;
// split the string at the commas and convert the sections into integers:
let sensors = int(split(currentString, ","));
//console.log(currentString);
latestDataR = sensors[0];
latestDataG = sensors[1];
latestDataB = sensors[2];
latestDatabG = sensors[3];
latestDatabR = sensors[4];
latestDatabY = sensors[5];
}
function draw() {
if (startButton == true) {
if (serial.available() > 0) {
print("Card Count Init: " + cardCount);
if (scan() == true && (dataR > 110 || dataG > 110 || dataB > 110)) {
cardCount++;
if (dataB > 110 && dataB > dataR && dataB > dataG) {
countBlue++;
}
if (dataR > 110 && dataR > dataG && dataR > dataG) {
countRed++;
}
if (dataG > 110 && dataG > dataR && dataG > dataB) {
countGreen++;
}
}
background(0);
fill(255);
noStroke();
text(
scanText +
"\n\nCard Count: " +
cardCount +
"\nCards from " + redName + ": " +
countRed +
"\nCards from " + greenName + ": " +
countGreen +
"\nCards from " + blueName + ": " +
countBlue,
250,
250,
250
);
}
} else {
background(0);
stroke(255);
strokeWeight(4);
noStroke();
fill(255);
textSize(16);
text("click anywhere to start", 250, 250);
}
if (cardCount >= goalCount) {
message();
}
if (showGame == true) {
goal();
}
}
function mousePressed() {
if (startButton == false);
{
// line(250, 0, 250, 500); //helper lines
// line(0, 250, 500, 250); //helper lines
background(0);
startButton = true;
}
}
function scan() {
if (latestDatabG > 0) {
dataR = latestDataR;
dataG = latestDataG;
dataB = latestDataB;
print(dataR + ", " + dataG + ", " + dataB);
return true;
} else {
text(scanText, 250, 250, 250, 250);
return false;
}
}
function message() {
background(0);
text(
"Great job! You reached your goal of " +
goalCount +
" cards.\n\nPush the red button.",
250,
250,
250,
250
);
if (latestDatabR > 0) {
print("congrat message");
showGame = true;
trigger == 1;
return true;
}
}
function goal() {
background(0);
noStroke();
fill(255);;
text("Push yellow button to spin", 300, 18);
noStroke();
fill(red);
arc(250, 250, 450, 450, 0, QUARTER_PI);
fill(orange);
arc(250, 250, 450, 450, QUARTER_PI, HALF_PI);
fill(yellow);
arc(250, 250, 450, 450, HALF_PI, 0.75 * PI);
fill(green);
arc(250, 250, 450, 450, 0.75 * PI, PI);
fill(blueGreen);
arc(250, 250, 450, 450, PI, PI + QUARTER_PI);
fill(blue);
arc(250, 250, 450, 450, PI + QUARTER_PI, PI + HALF_PI);
fill(purple);
arc(250, 250, 450, 450, PI + HALF_PI, PI + HALF_PI + QUARTER_PI);
fill(redPurple);
arc(250, 250, 450, 450, PI + HALF_PI + QUARTER_PI, PI + PI);
//RED
push();
let angle1 = QUARTER_PI / 2;
translate(250, 250);
rotate(angle1);
// Draw the letter to the screen
fill(0);
textSize(17);
text("SPIN AGAIN", 70, 0);
//line(0, 0, 150, 0);
pop();
//ORANGE
push();
let angle2 = QUARTER_PI + QUARTER_PI / 2;
translate(250, 250);
rotate(angle2);
// Draw the letter to the screen
fill(0);
textSize(17);
text("MOVIE", 70, 0);
//line(0, 0, 150, 0);
pop();
//YELLOW
push();
let angle3 = HALF_PI + QUARTER_PI / 2;
translate(250, 250);
rotate(angle3);
// Draw the letter to the screen
fill(0);
textSize(17);
text("SWEET SNACK", 70, 0);
//line(0, 0, 150, 0);
pop();
//GREEN
push();
let angle4 = HALF_PI + QUARTER_PI / 2 + QUARTER_PI;
translate(250, 250);
rotate(angle4);
// Draw the letter to the screen
fill(0);
textSize(17);
text("YAYA & DAYA", 70, 0);
//line(0, 0, 150, 0);
pop();
//BLUE GREEN
push();
let angle5 = PI + QUARTER_PI / 2;
translate(250, 250);
rotate(angle5);
// Draw the letter to the screen
fill(0);
textSize(17);
text("FRIEND", 70, 0);
//line(0, 0, 150, 0);
pop();
//BLUE
push();
let angle6 = PI + QUARTER_PI / 2 + QUARTER_PI;
translate(250, 250);
rotate(angle6);
// Draw the letter to the screen
fill(0);
textSize(17);
text("MYSTERY", 70, 0);
//line(0, 0, 150, 0);
pop();
//PURPLE
push();
let angle7 = PI + HALF_PI + QUARTER_PI / 2;
translate(250, 250);
rotate(angle7);
// Draw the letter to the screen
fill(0);
textSize(17);
text("LUNCH TREAT", 70, 0);
//line(0, 0, 150, 0);
pop();
//RED PURPLE
push();
let angle8 = PI + HALF_PI + QUARTER_PI + QUARTER_PI / 2;
translate(250, 250);
rotate(angle8);
// Draw the letter to the screen
fill(0);
textSize(17);
text("ACTIVITY", 70, 0);
//line(0, 0, 150, 0);
pop();
translate(250, 250);
let i = 0;
for (let a = 0; a < TWO_PI; a += PI / 5) {
fill(245, 245, 220);
stroke(220);
strokeWeight(2);
let ang = angle % TWO_PI;
if (ang >= a && ang <= a + PI / 5) {
fill(255, 50);
}
//arc(0, 0, 400, 400, a, a + PI / 5, PIE);
i++;
spin();
}
noStroke();
fill(240, 99, 164); //pink
rectMode(CENTER);
rotate(angle);
//arrow
rect(0, 0, 256, 16);
triangle(128, -32, 128, 32, 172, 0);
rect(-128, 0, 32, 48);
//yellow dot
fill(252, 238, 33);
ellipse(0, 0, 8, 8);
angle += angleV;
// angleV += angleA;
angleV *= 0.98;
}
function spin() {
if (latestDatabY > 0) {
let newAngle = TWO_PI + createVector(mouseX - 200, mouseY - 200).heading();
angleV = -1 * (newAngle - holdA) * 0.05;
} else {
trigger = 0;
}
}