Serial Communication Blog Post/Bonus Servo Post

So for my lab I wanted to take the first step towards my mid-term by adding a sensor value to a baseball sketch that I had made**. Here is sketch pre serial-communication addition (the code for both sketches is featured at the bottom also turn your sound either off or really low: if you’re reading this at 4:00 am the last thing I want is for this loud crack of the bat to wake up Noodles):

As you can see, a ball is pitched from the mound and then the bat is swung using mouseY. When the bat makes contact with the ball, you record a hit and the crowd goes wild. For this assignment, I decided to replace mouseY with a potentiometer as I felt it would add a nice level of realism to the game.

Going further with this in the days to come, I’d like to replace the potentiometer with an accelerometer that is attached to an actual (mini) bat but I’m a bit concerned. I talked with Matt Romein about trying to make it so that when the bat is swung in real life, the bat is swung in the p5 sketch. He had mentioned that may be very difficult and would be best to make it so that when the accelerometer reaches a certain velocity, the bat is swung. I would then have to make sure there is an incredibly minimal delay between the velocity being reached and the bat being swung. Hopefully you’ll be reading about my solution to this project in the mid-term blog!
** Don’t want you to think I made Mona do a baseball project or that I have a one track mind. I totally just posed the idea to her as nothing more than that: an idea and she thought it would be a good way to showcase physical computing. I suppose I’m getting a bit insecure about that so I just wanted to mention it.
P.S Here is a link to my final Fab project which involved some physical computing.
Code for Sketch 1:

var x = 320;
var y = 235;
var speed = 3;
var speedb = 0;

function preload() {
img = loadImage('battersbox.jpg');
mySound = loadSound('Cheer.mp3');
image2 = loadImage('Bat.jpg');
image3 = loadImage('baseball.jpg');
}

function setup() {
createCanvas(600, 600);
img.loadPixels();
image2.loadPixels();
image3.loadPixels();

}

function draw() {
background(220);
image(img, 0, 0, 650, 500);
baseball();
pitch();
returnball();
bat();
swing();
}

function baseball() {
fill(255);
ellipse(x, y, 25, 25);
image(image3,x-15,y-10,25,25);
}

function pitch() {
y = y + speed;
x = x + speedb;
if (y >= height) {
y = 235;
}

}

function returnball() {
if (x > 600 || x < 0 || y > 600 || y < 0) {
baseball();
x = 320;
y = 235;
speed = 3;
speedb = 0;
}
}

function bat() {
translate(135, 400);
rotate(mouseY / 600.0);
fill(100, 100, 100);
rect(40, 30, 160, 20);
print(mouseY);
image(image2, 40, 30, 160, 20);
}

function swing() {
d = dist(x, y, x, 430);
if (d < 1 && mouseY/100 < 0.25) {
mySound.play();
speed = speed * random(-4, -1);
speedb = speedb + random(-10, 5);
}
}

 

Code for Potentiometer Sketch
In p5

var x = 320;
var y = 235;
var speed = 3;
var speedb = 0;
var serial;
var sensorValue = 0;

function preload() {
img = loadImage('battersbox.jpg');
mySound = loadSound('Cheer.mp3');
image2 = loadImage('Bat.jpg');
image3 = loadImage('baseball.jpg');
serial = new p5.SerialPort(); // make a new instance of serialport library
serial.on('list', printList); // callback function for serialport list event
serial.on('data', serialEvent);// callback for new data coming in
serial.list(); // list the serial ports
serial.open("/dev/cu.usbmodem1411"); // open a port
}

function setup() {
createCanvas(600, 500);
img.loadPixels();
image2.loadPixels();
image3.loadPixels();

}

function draw() {
background(220);
image(img, 0, 0, 650, 500);
baseball();
pitch();
returnball();
bat();
swing();
}

function baseball() {
fill(255);
ellipse(x, y, 25, 25);
image(image3,x-15,y-10,25,25);
}

function pitch() {
y = y + speed;
x = x + speedb;
if (y >= height) {
y = 235;
}

}

function returnball() {
if (x > 600 || x < 0 || y > 600 || y < 0) {
baseball();
x = 320;
y = 235;
speed = 3;
speedb = 0;
}
}

function bat() {
translate(135, 400);
rotate(sensorValue / 600.0);
fill(100, 100, 100);
rect(40, 30, 160, 20);
print(mouseY);
image(image2, 40, 30, 160, 20);
}

function swing() {
d = dist(x, y, x, 430);
if (d < 1 && sensorValue/100 < 0.25) {
mySound.play();
speed = speed * random(-4, -1);
speedb = speedb + random(-10, 5);
}
}

function printList(portList) {
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
println(i + " " + portList[i]);
}
}

function serialEvent() {
var inString = serial.readLine();
if (inString.length > 0) {
inString = inString.trim();
sensorValue = Number(inString/4);
}
}

for Arduino:

void setup() {
Serial.begin(9600);
}

void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(1);

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *