xxxxxxxxxx
124
//incomplete
let position;
let acceleration;
let wind;
let drag = 0.99;
let mass = 50;
function setup() {
createCanvas(640, 360);
noFill();
position = createVector(width / 2, 0);
velocity = createVector(0, 0);
acceleration = createVector(0, 0);
gravity = createVector(0, 0.5 * mass);
wind = createVector(0, 0);
}
function draw() {
background(255);
applyForce(wind);
applyForce(gravity);
velocity.add(acceleration);
velocity.mult(drag);
position.add(velocity);
acceleration.mult(0);
ellipse(position.x, position.y, mass, mass);
if (position.y > height - mass / 2) {
velocity.y *= -0.9; // A little dampening when hitting the bottom
position.y = height - mass / 2;
hit = 1;
} else {
hit = 0;
}
if (!serialActive) {
console.log("Press Space Bar to select Serial Port");
} else {
// console.log("Connected");
if (reset == 1) {
reset = 0;
mass = random(15, 80);
position.x = width / 2;
position.y = -mass;
velocity.mult(0);
}
}
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
}
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
reset = fromArduino[0];
wind.x = fromArduino[1];
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = hit + "\n";
writeSerial(sendToArduino);
}
}
//Arudino Code
/*
int buttonSwitch = A2;
int potentiometer = A0;
int ledOut = 12;
void setup() {
Serial.begin(9600);
pinMode(12, OUTPUT);
digitalWrite(ledOut, LOW); // in the case of reconnection while p5 is running
// start the handshake
while (Serial.available() <= 0) {
Serial.println("-1,-1"); // send a starting message
delay(300); // wait 1/3 second
}
}
void loop() {
// wait for data from p5 before doing something
while (Serial.available()) {
int hit = Serial.parseInt();
if (hit) {
digitalWrite(ledOut, HIGH);
} else {
digitalWrite(ledOut, LOW);
}
if (Serial.read() == '\n') {
digitalWrite(ledOut, hit);
int sensor = digitalRead(buttonSwitch);
delay(1);
int sensor2 = analogRead(potentiometer);
delay(1);
Serial.print(sensor);
Serial.print(',');
if (sensor2 < 512) {
Serial.println(1);
} else {
Serial.println(-1);
}
}
}
}
*/