xxxxxxxxxx
223
let currentColor, nextColor;
let gridColors = [];
let gridSize = 2;
let ellipseSize = 100;
let currentPage = -1; // Start with the start page
let startImage;
function preload() {
startImage = loadImage('Start.jpeg'); // Load the start page background image
}
function setup() {
createCanvas(600, 600);
noLoop();
currentColor = randomColor(); // Start with a random color
nextColor = randomColor(); // Prepare next memorization color
}
function draw() {
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("Connected", 20, 30);
// Print the current values
text('rVal = ' + str(rVal), 20, 50);
text('alpha = ' + str(alpha), 20, 70);
}
if (currentPage === -1) {
drawStartPage();
} else if (currentPage === 0) {
drawFirstPage();
} else {
drawGamePage();
}
}
function drawStartPage() {
background(startImage);
fill(0);
rect(width / 2 - 30, height / 2 - 20, 60, 40);
fill(255);
textAlign(CENTER, CENTER);
text('Start', width / 2, height / 2);
}
function drawFirstPage() {
background(220);
fill(currentColor);
ellipse(width / 2, height / 2, 120, 120);
fill(0);
rect(width - 80, 20, 60, 40);
fill(255);
textAlign(CENTER, CENTER);
text('Next', width - 50, 40);
}
function drawGamePage() {
background(220);
fill(nextColor);
ellipse(width / 2, 100, 120, 120);
let padding = 230;
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
fill(gridColors[i * gridSize + j]);
ellipse(padding + j * (ellipseSize + 50), padding + i * (ellipseSize + 50), ellipseSize, ellipseSize);
}
}
}
function mouseClicked() {
if (currentPage === -1) {
if (mouseX > width / 2 - 30 && mouseX < width / 2 + 30 && mouseY > height / 2 - 20 && mouseY < height / 2 + 20) {
currentPage = 0;
redraw();
}
} else if (currentPage === 0) {
if (mouseX > width - 80 && mouseY > 20 && mouseX < width - 20 && mouseY < 60) {
gridColors = Array.from({length: gridSize * gridSize}, () => randomColor());
gridColors[int(random(gridSize * gridSize))] = currentColor;
currentPage++;
redraw();
}
} else {
let padding = 230;
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
let x = padding + j * (ellipseSize + 50);
let y = padding + i * (ellipseSize + 50);
if (dist(mouseX, mouseY, x, y) < ellipseSize / 2) {
if (gridColors[i * gridSize + j] === currentColor) {
prepareNextLevel();
}
return;
}
}
}
}
}
function prepareNextLevel() {
currentColor = nextColor;
nextColor = randomColor();
gridColors = Array.from({length: gridSize * gridSize}, () => randomColor());
gridColors[int(random(gridSize * gridSize))] = currentColor;
redraw();
}
function randomColor() {
return color(random(255), random(255), random(255));
}
// 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 == 2) {
// 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]);
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = left + "," + right + "\n";
writeSerial(sendToArduino);
}
}
//Arduino Code
/*
// Week 11.2 Example of bidirectional serial communication
// Inputs:
// - A0 - sensor connected as voltage divider (e.g. potentiometer or light sensor)
// - A1 - sensor connected as voltage divider
//
// Outputs:
// - 2 - LED
// - 5 - LED
int leftLedPin = 2;
int rightLedPin = 5;
void setup() {
// Start serial communication so we can send data
// over the USB connection to our p5js sketch
Serial.begin(9600);
// We'll use the builtin LED as a status output.
// We can't use the serial monitor since the serial connection is
// used to communicate to p5js and only one application on the computer
// can use a serial port at once.
pinMode(LED_BUILTIN, OUTPUT);
// Outputs on these pins
pinMode(leftLedPin, OUTPUT);
pinMode(rightLedPin, OUTPUT);
// Blink them so we can check the wiring
digitalWrite(leftLedPin, HIGH);
digitalWrite(rightLedPin, HIGH);
delay(200);
digitalWrite(leftLedPin, LOW);
digitalWrite(rightLedPin, LOW);
// start the handshake
while (Serial.available() <= 0) {
digitalWrite(LED_BUILTIN, HIGH); // on/blink while waiting for serial data
Serial.println("0,0"); // send a starting message
delay(300); // wait 1/3 second
digitalWrite(LED_BUILTIN, LOW);
delay(50);
}
}
void loop() {
// wait for data from p5 before doing something
while (Serial.available()) {
digitalWrite(LED_BUILTIN, HIGH); // led on while receiving data
int left = Serial.parseInt();
int right = Serial.parseInt();
if (Serial.read() == '\n') {
digitalWrite(leftLedPin, left);
digitalWrite(rightLedPin, right);
int sensor = analogRead(A0);
delay(5);
int sensor2 = analogRead(A1);
delay(5);
Serial.print(sensor);
Serial.print(',');
Serial.println(sensor2);
}
}
digitalWrite(LED_BUILTIN, LOW);
}
*/