PComp Lab: Serial Output From P5.js

In this lab we are using P5 to control Arduino serially.
We start by programming our board to change LED brightness
if you press “H” for high or “L”for low.  It looks like this, in Arduino code:.
 
constintledPin = 5; // the pin that the LED is attached to
int incomingByte;     // a variable to read incoming serial data into
void setup() {
 Serial.begin(9600);             // initialize serial communication
 pinMode(ledPin, OUTPUT);        // initialize the LED pin as an output
}
void loop() {
 if (Serial.available() > 0) {   // see if there's incoming serial data
   incomingByte = Serial.read(); // read it
   if (incomingByte == 'H') {    // if it's a capital H (ASCII 72),
     digitalWrite(ledPin, HIGH); // turn on the LED
   }
   if (incomingByte == 'L') {    // if it's an L (ASCII 76)
     digitalWrite(ledPin, LOW);  // turn off the LED
   }
 }
}
In P5.js, We can use H and L to control the intensity of the LED by writing
function keyPressed() {
 if (key ==='H' || key ==='L') { // if the user presses H or L
 serial.write(key);              // send it out the serial port
 }
}
Here’s the result:

Categories: itp, lab, pcomp

Tagged as: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s