xxxxxxxxxx
97
let dir = 4;
let connect = true;
function preload(){
title = loadImage("title.png");
mov = loadImage("movement.png");
brk = loadImage("break.png");
play = loadImage("play.png");
arrow = loadImage("arroww.png");
choose = loadImage("choose.png");
}
function setup() {
createCanvas(640, 480);
textSize(18);
}
function draw() {
background(0);
fill(255);
textStyle(BOLD);
imageMode(CENTER);
image(title, width/2, height/8, 300, 50);
image(mov, width/4, height* 3/8, 160, 25);
image(arrow, width* 3/4, height * 3/8, 100, 65);
image(brk, width/5, height * 4.5/8, 90, 24);
fill(0);
stroke(255);
strokeWeight(4);
rectMode(CENTER);
rect(width* 3/4, height * 4.5/8, 140, 30, 5);
if (connect) {
if (!serialActive) {
image(choose, width/2, height * 6.5/8, 400, 20);
fill(0);
rect(width/3 + 10, height*6.5/8, 35, 35, 9);
fill(255);
strokeWeight(0);
textSize(28);
text("A", width/3 +0.5, height*6.5/8 + 9);
}
else {
text("Connected", 20, 50);
}
}
else {
rect(width/2, height * 6.5/8, 100, 40, 5);
image(play, width/2, height * 6.5/8, 80, 23);
}
}
function keyPressed() {
if (key == "a") {
// important to have in order to start the serial connection!!
setUpSerial();
}
else if (keyCode == UP_ARROW) {
dir = 0;
}
else if (keyCode == LEFT_ARROW) {
dir = 1;
}
else if (keyCode == RIGHT_ARROW) {
dir = 2;
}
else if (keyCode == DOWN_ARROW) {
dir = 3;
}
else if (key == " ") {
dir = 4;
}
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
if (data != "0") {
let sendToArduino = dir + "\n";
console.log(sendToArduino)
writeSerial(sendToArduino);
}
else {
dir = 4
let sendToArduino = dir + "\n";
console.log(sendToArduino)
writeSerial(sendToArduino);
}
}
//console.log(data);
}