Computational Media · Physical Computing

Connecting Arduino and P5.js

This week me and Steven Simon tried to connect Arduino and P5.js with the help of ITP, it was fun.

First we started with a  p5 sketch Steven made, which is an ipod contains a song file,  when the mouse hover on the white button, it will automatically play a song, which is pretty cool. We thought it would be interesting to make a stop and play button for the ipod using Arduino.

 

We downloaded the P5.serialcontrol, which is a tool that connect them up. One of the point of the Arduino coding  is that to set up the button state.  The codes are below:

 

 

int pauseButton = 2;

int playButton = 9;

int buttonState = 0;    // the setup routine runs once when you press reset:

void setup() {

// initialize serial communication at 9600 bits per second:

Serial.begin(9600); // make the pushbutton’s pin an input:

pinMode(pauseButton, INPUT);

pinMode(playButton,INPUT);

} // the loop routine runs over and over again forever:

void loop() {  // read the input pin:

int buttonStateOne = digitalRead(pauseButton);

int buttonStateTwo = digitalRead(playButton);  // print out the state of the button:

if (!buttonStateOne && ! buttonStateTwo){ //

Serial.println(buttonState);

delay(1);

} else if (buttonStateOne && !buttonStateTwo){

Serial.println(“Pause”); //

buttonState = 0;

delay(1); } else if (buttonStateTwo && !buttonStateOne){

Serial.println(“Play”); //

buttonState = 1; delay(1); } else if( buttonStateOne && buttonStateTwo){

Serial.println(“both”);

delay(1); } // delay in between reads for stability

}

img_5251

the Arduino Connection

Then with the given examples on the ICM website, we tried to make some chances in the original codes, here is the final version of the p5 codes, and it worked!!  Video

Something new I learned from this lab is that functions like serial(), and it also helped me better with understanding how’s the output from Arduino to the P5 codes. Our problems before is that in our older codes, we print out words like “play” or “stop” as well as “0” or “1”, that’s kind of messy, and should be avoided in the future.

2 thoughts on “Connecting Arduino and P5.js

Leave a Reply

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