Lab 04

I had a lot of trouble with this week's labs. I understand the principals of serial communication, however I had trouble working with the node serial port library.

First, some successes:

Here is a simple circuit I replicated from the labs schematics. It is simply a FSR, a potentiometer, and a button.

digi-out

Here is the code I wrote to read each sensor value:

int potPin = A0;
int fsrPin = A1;
int digiPin = 2;
int count = 0;
void setup() {
  // put your setup code here, to run once:
  pinMode(2,INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int fsr_val = analogRead(fsrPin);
  int pot_val = analogRead(potPin);
  int button_val = digitalRead(digiPin);
  int sensors[]={fsr_val,pot_val,button_val};
  
  if (Serial.available() > 0){
    int incoming = Serial.read();
    if (count < 3){
        Serial.println(sensors[count]) ;
        count ++;
    }else{
      count = 0;
    }
  }
}

When the time came to send data through my serial port into a program, I decided to go down the rabbit hole of creating my own node server.

I won't get in to too many details, but the node-serial library is broken - it seems like the only working serial port library lives within p5. I was stubborn, so I cloned the p5.serial library, and tried to make a work around that would allow me to read data through my serial port without p5.

Correction:: Some hero named voodootikigod published a new version yesterday. I am going to try again later.

After a lot of struggle, and no progress, I gave up. So I downloaded p5, and went to the docs to learn more about the library.

I got the serial input and serial output labs done pretty quickly once I stopped trying to work around p5. In fact, p5 is great, and I am going to spend a lot more time working with it.

Arduino:

const int ledPin = 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
   }
 }
}

p5:

var serial;          
var portName = '/dev/cu.usbmodem1411';
var inByte;
var outByte;
function setup() {
serial = new p5.SerialPort();       // make a new instance of the serialport library
  // set a callback function for the serialport list event     // callback for when new data arrives
  serial.on('error', serialError);    // callback for errors     // callback for the port closing
  serial.open(portName);              // open a serial port
  createCanvas(400,400);
}

function draw() {
 // black background, white text:
 background(0);
}

function keyPressed() {
 if (key ==='H' || key === 'L'){
    serial.write(key);
 }

}
function serialError(err) {
  println('Something went wrong with the serial port. ' + err);
}

finished product:

Written on October 13, 2015