pComp Week 6 CEREAL (nomnom) input to p5.js


2017-10-14 16.48.26 from Marco Wylie on Vimeo. (sorry for the sideways video)

void setup() {
void loop() {
int analogValue = analogRead(A0)/4;

So in this video I’ve used a serial connection between my Arduino and my laptop, and with the code in the Arduino software I can see the binary values that the potentiometer gives out which is why it looks crazy on the screen. I also noticed that the board lights up “TX” which means it’s transmitting information, not receiving.

After that I downloaded Processing and was able to read the data there:

I couldn’t seem to get any farther than that though because I was getting a string of errors and then my computer shut down. 🙁 I moved onto the next lab in the effort of time.

Below, I tried using an example from tigoe’s examples to see if I have my potentiometer serially hooked up to p5.js but the browser kept freezing.

No matter what I tried that tab kept freezing so I moved onto another lab “serial input to p5js ide” and ran into the following error after copying the code:

I thought I found part of the problem — I didn’t upload the p5.serialserver.js file to my sketch, but I’m still running into the same error! Soooo frustrated….

OK! After an hour or so of mind-blowing frustration I realized I uploaded the wrong #&*@ file…I had uploaded p5.serialserver.js instead of p5.serialport.js — to my dismay, I’m still getting errors: (see below)

After commenting out all those functions I was able to get a sensor reading.

Serial Sensor p5 reading from Marco Wylie on Vimeo.

Here’s a link to the p5.js sketch.

When trying to go a step further and add a graph to my sketch I encountered this error:

There seems to be a problem with my background but I can’t figure out what the issue is. I tried restarting with a fresh tab but now I’m not seeing any results or errors…

Here’s the code:
var serial; // variable to hold an instance of the serialport library
var portName = '/dev/cu.usbmodem1421'; // fill in your serial port name here
var inData; // for incoming serial data
var xPos = 0; // x position of the graph
function setup() {
createCanvas(400, 300);
background(0x08, 0x16, 0x40);
serial = new p5.SerialPort(); // make a new instance of the serialport library
serial.on('list', printList); // set a callback function for the serialport list event
serial.list(); // list the serial ports
// get the list of ports:
function printList(portList) {
// portList is an array of serial port names
for (var i = 0; i < portList.length; i++) { // Display the list the console: //println(i + " " + portList[i]); } } function graphData(newData) { // map the range of the input to the window height: var yPos = map(newData, 0, 255, 0, height); // draw the line in a pretty color: stroke(0xA8, 0xD9, 0xA7); line(xPos, height, xPos, height - yPos); // at the edge of the screen, go back to the beginning: if (xPos >= width) {
xPos = 0;
// clear the screen by resetting the background:
background(0x08, 0x16, 0x40);
} else {
// increment the horizontal position for the next reading:
function draw() {

Candy Machine

For this weeks assignment I made a candy machine that uses a potentiometer and a servo that dispenses the candy using a wheel attached to the servo.

2017-10-07 18.35.59 from Marco Wylie on Vimeo.

Here is the code:

Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
myservo.attach(9); // attaches the servo on pin 9 to the servo object
void loop()
val = analogRead(potpin); // reads the value of the
// potentiometer (value between
// 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with
// the servo (value between 0 and
// 180)
myservo.write(val); // sets the servo position according
// to the scaled value
delay(15); // waits for the servo to get there

It took a little finagling but eventually I was able to tape down the ramp to the cardboard in a way that would allow the wheel not to get caught when it spun. Once I had that in place I hot glued everything together. The only problem I ran into was similar to Oren’s problem last week. My potentiometer, when all the way to the left or right position would start to act out on it’s own. I contacted him to see if he had a solution but he said he didn’t and I was unable to rectify the problem myself.

The final result (with a few kinks):

2017-10-10 23.13.10 from Marco Wylie on Vimeo.