xxxxxxxxxx
165
//change LED birghtness using mouse
let rVal = 0;
let alpha = 255;
let left = 0;
let right = 0;
let LED_value;
// the bouncing ball code
let velocity;
let gravity;
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);
textSize(18);
}
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) {
LED_value = 255;
velocity.y *= -0.9; // A little dampening when hitting the bottom
position.y = height-mass/2;
}
else{
LED_value = 0;
}
print(LED_value);
// one value from Arduino controls the background's red color
// background(map(rVal, 0, 1023, 0, 255), 255, 255);
// the other value controls the text's transparency value
// fill(255, 0, 255, map(alpha, 0, 1023, 0, 255));
if (!serialActive) {
fill("black");
text("Press Space Bar to select Serial Port", 20, 30);
} else {
fill("black");
text("Connected", 20, 30);
text("press left or right to change wind direction, and press k to drop another ball",20, 60)
}
fill("white");
// click on one side of the screen, one LED will light up
// click on the other side, the other LED will light up
if (mouseIsPressed) {
if (mouseX <= width / 2) {
left = 1;
} else {
right = 1;
}
} else {
left = right = 0;
}
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
if (keyCode==LEFT_ARROW){
wind.x=-1;
}
if (keyCode==RIGHT_ARROW){
wind.x=1;
}
if (key=='k'){
mass=random(15,80);
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 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
rVal = fromArduino[0];
alpha = fromArduino[1];
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = LED_value + "\n";
writeSerial(sendToArduino);
}
}
//Arudino Code
/*
void setup() {
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(5, OUTPUT);
// start the handshake
while (Serial.available() <= 0) {
Serial.println("0,0"); // send a starting message
delay(300); // wait 1/3 second
}
}
void loop() {
// wait for data from p5 before doing something
while (Serial.available()) {
int left = Serial.parseInt();
// int right = Serial.parseInt();
if (Serial.read() == '\n') {
analogWrite(2, left);
// digitalWrite(5, right);
int sensor = 3;
delay(1);
int sensor2 =3;
delay(1);
Serial.print(sensor);
Serial.print(',');
Serial.println(sensor2);
}
}
}
*/