xxxxxxxxxx
94
/*
name: Adina Maratkyzy
class: Intro to IM, Spring 2023
professor: Michael Shiloh
date:
project description:
*/
let gameState = 1; // can be 'start', 'playing', 'end'
let player1L_btn;
let player1R_btn;
let player1Select_btn;
let player2L_btn;
let player2R_btn;
let player2Select_btn;
let menu_btn;
let instructions_btn;
function setup() {
createCanvas(640, 480);
textSize(18);
}
function draw() {
background (130);
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
// text("Connected", 20, 30);
text('player 1', 20, 50);
text('left button = ' + str(player1L_btn), 20, 70);
text('right button = ' + str(player1L_btn), 20, 90);
text('select button = ' + str(player1L_btn), 20, 110);
text('player 2', 280, 50);
text('left button = ' + str(player1L_btn), 280, 70);
text('right button = ' + str(player1L_btn), 280, 90);
text('select button = ' + str(player1L_btn), 280, 110);
}
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
}
// This function will be called by the web-serial library
// with each new *line* of data. The serial library reads
// the data until the newline and then gives it to us through
// this callback function
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
// make sure there is actually a message
// split the message
let fromArduino = split(trim(data), ",");
// if the right length, then proceed
if (fromArduino.length == 8) {
player1L_btn = int(fromArduino[0]);
player1R_btn = int(fromArduino[1]);
player1Select_btn = int(fromArduino[2]);
player2L_btn = int(fromArduino[3]);
player2R_btn = int(fromArduino[4]);
player2Select_btn = int(fromArduino[5]);
menu_btn = int(fromArduino[6]);
instructions_btn = int(fromArduino[7]);
// only store values here
// do everything with those values in the main draw loop
// We take the string we get from Arduino and explicitly
// convert it to a number by using int()
// e.g. "103" becomes 103
// rVal = int(fromArduino[0]);
// alpha = int(fromArduino[1]);
}
else {
console.log ("no data");
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = gameState + "\n";
writeSerial(sendToArduino);
}
}