xxxxxxxxxx
146
// This is a combined version of the following sketchs.
// https://editor.p5js.org/aaronsherwood/sketches/I7iQrNCul
// https://editor.p5js.org/aaronsherwood/sketches/q2Pl77SWl
//
// Instead of using a keyboard, this version communicates with Arduino
// to receive inputs. In the circuit, a button, a potentiometer, and an LED
// are placed for a digital input, an analog output, and a digital output.
//
// Only lines of code that I wrote may be commented.
let hit = 0; // whether the ball hit the ground
let reset = 0; // whether Arduino sent a reset argument (a button press)
// Ball physics
let velocity;
let gravity;
let position;
let acceleration;
let wind; // wind direction is controlled by Arduino (potentiometer)
let drag = 0.99;
let mass = 50;
function setup() {
createCanvas(600, 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) { // if reset signal is sent and flagged (button press)
reset = 0; // clear the flag
// reset ball with some random mass
mass = random(15, 80);
position.x = width / 2;
position.y = -mass;
velocity.mult(0);
}
}
}
function applyForce(force) {
// Newton's 2nd law: F = M * A
// or A = F / M
let f = p5.Vector.div(force, mass);
acceleration.add(f);
}
function keyPressed() {
// Only SPACEBAR, which is used to setup serial remains.
// All other keyboard interaction are removed
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 = 11;
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(); // receives 1 argument, whether the ball hit the ground
if (Serial.read() == '\n') {
digitalWrite(ledOut, hit); // turn on LED if the ball is in contact with the ground (1 -> HIGH) turn off LED if not (0, -> LOW)
int sensor = digitalRead(buttonSwitch); // read button
delay(1);
int sensor2 = analogRead(potentiometer); // read potentiometer
delay(1);
Serial.print(sensor); // button
Serial.print(',');
if (sensor2 < 512) { // potentiometer; depending whether the value is over or below half, direction of the wind is set
Serial.println(1);
} else {
Serial.println(-1);
}
}
}
}
*/