xxxxxxxxxx
256
let currentColor, nextColor;
let gridColors = [];
let gridSize = 2; // Grid size
let ellipseSize = 100; // Size of the ellipses
let currentPage = -1; // Page tracker
let startImage;
let timer = 5;
let timerActive = false;
let ArrowLeft = 0, ArrowRight = 0, ArrowUp = 0, ArrowDown = 0;
let serial; // Serial port object
function preload() {
startImage = loadImage('Start.jpeg'); // Preload the start image
}
function setup() {
createCanvas(windowWidth, windowHeight);
noLoop();
currentColor = randomColor();
nextColor = randomColor();
gridColors = Array.from({length: gridSize * gridSize}, () => randomColor());
gridColors[int(random(gridSize * gridSize))] = currentColor;
setUpSerial(); // Initialize serial communication
}
function setupGrid() {
let shuffledImages = shuffle(images.slice()); // Shuffle a copy of the images array
gridImages = shuffledImages.slice(0, gridSize * gridSize); // Select the first gridSize*gridSize images
if (!gridImages.includes(selectedImage)) {
gridImages[0] = selectedImage; // Ensure the selected image is in the grid
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function draw() {
background(220);
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() {
fill(currentColor);
ellipse(width / 2, height / 2, 120, 120);
if (!timerActive) {
timerActive = true;
startTimer();
}
}
function drawGamePage() {
let offsetX = width / 2 - gridSize * 170 / 2 + 85 / 2;
let offsetY = height / 2 - gridSize * 170 / 2 + 85 / 2;
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
image(gridImages[i * gridSize + j], offsetX + j * 170, offsetY + i * 170, 85, 85);
}
}
}
function mouseClicked() {
if (currentPage == -1 && mouseX > width / 2 - 30 && mouseX < width / 2 + 30 && mouseY > height / 2 - 20 && mouseY < height / 2 + 20) {
currentPage = 0;
redraw();
}
}
function keyPressed() {
if (key === ' ') {
// Already set up in setup(), you might want to toggle or check connection here
} else if (currentPage === 0) {
currentPage++;
redraw();
} else if (currentPage > 0) {
// Handle other keys and game logic
}
if (key === 'f' || key === 'F') {
fullscreen(!fullscreen());
}
}
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));
}
function startTimer() {
let interval = setInterval(() => {
timer--;
if (timer <= 0) {
clearInterval(interval);
currentPage++;
timerActive = false;
redraw();
}
}, 1000);
}
function setUpSerial() {
//serial = new p5.SerialPort();
//serial.on('data', readSerial);
//serial.open('/dev/tty.usbmodem14101'); // Adjust to match your device or port
}
function readSerial() {
let data = serial.readLine();
if (data) {
let fromArduino = split(trim(data), ',');
if (fromArduino.length == 4) {
ArrowLeft = int(fromArduino[0]);
ArrowDown = int(fromArduino[1]);
ArrowRight = int(fromArduino[2]);
ArrowUp = int(fromArduino[3]);
}
}
}
function readSerial(data) {
// Process serial data here
////////////////////////////////////
//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 == 4) {
// 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
ArrowLeft = int(fromArduino[0]);
ArrowDown = int(fromArduino[1]);
ArrowRight = int(fromArduino[2]);
ArrowUp = int(fromArduino[3]);
}
//console.log ("left button is" + ArrowLeft);
console.log ("right button is" + ArrowRight);
//console.log ("up button is" + ArrowUp);
//console.log ("meow button is" + ArrowDown);
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = "\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);
}
*/