Afra Binjerais – Serial Communication

With Amal and Stefania

Initial connection and wiring from the schematic:

Exercise 1:

This was probably the easiest exercise, we just had to edit the code that was in the Week 12 Lecture Notes.

P5.js Code:

let potentiometer = 0;

function setup() {
  createCanvas(640, 480);
  textSize(18);
}

function draw() {
  background(255);
 
  if (!serialActive) {
    text("Press Space Bar to select Serial Port", 20, 30);
  } else {
    text("Connected", 20, 30);
    text('Potentiometer = ' + potentiometer, 20, 70);
  }

 
  let ellipseX = map(potentiometer, 0, 1023, 0, width);
  fill(0);
  ellipse(ellipseX, height / 2, 50, 50);

function keyPressed() {
  if (key === ' ') {
    setUpSerial();
  }
}

function readSerial(data) {
  if (data != null) {
    let trimmedData = trim(data);
    if (!isNaN(trimmedData)) {
      potentiometer = int(trimmedData);
    }
  }
}

Arduino Code: 

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

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(200);
  digitalWrite(LED_BUILTIN, LOW);
}

void loop() {
  int sensorValue = analogRead(A1);

  Serial.println(sensorValue);

  delay(10);
}

According to the other exercises, we tried multiple times with the code, and tried manipulating it to fit the requirments but we kept getting errors

After multiple trials we gave up and decided to dedicate all our time for our final project, however here is the code that was in progress with multiple errors:

the P5 code was the one giving us errors:

let serial;  
let rVal = 0;
let alpha = 255;
let left = 0;
let right = 0;
let slider;

function setup() {
  createCanvas(640, 480);
  textSize(18);
  
  function setup() {
  console.log('Setting up...');

  serial = new p5.SerialPort();
  serial.on('data', readSerial);
  serial.on('open', onSerialOpen);
  serial.on('error', onSerialError);
  serial.open('COM3');  // Update this to your port
}


  slider = createSlider(0, 255, 127);
  slider.position(10, 10);
  slider.style('width', '380px');

  // Initialize the serial port
  serial = new p5.SerialPort();
  serial.open('COM3'); // change this to your Arduino port
  serial.on('data', readSerial); // function to call when new data arrives
  serial.on('open', onSerialOpen); // function to call when the serial port opens
  serial.on('error', onSerialError); // function to call on error
}

function draw() {
  background(map(rVal, 0, 1023, 0, 255), 255, 255);
  fill(255, 0, 255, map(alpha, 0, 1023, 0, 255));

  if (!serial.isOpen()) {
    text("Press Space Bar to select Serial Port", 20, 30);
  } else {
    text("Connected", 20, 30);
    text('rVal = ' + str(rVal), 20, 50);
    text('alpha = ' + str(alpha), 20, 70);

    if (slider.value() > 127) {
      left = 1;
      right = 0;
    } else {
      left = 0;
      right = 1;
    }

    let sendToArduino = left + "," + right + "\n";
    serial.write(sendToArduino);
  }
}

function keyPressed() {
  if (key === ' ') {
    serial.open('/dev/tty.usbmodem1411'); // open a serial port
  }
}

function readSerial() {
  let data = serial.readLine(); // read the incoming string
  trim(data); // trim off trailing whitespace
  if (!data) return;
  let fromArduino = split(data, ',');
  if (fromArduino.length >= 2) {
    rVal = int(fromArduino[0]);
    alpha = int(fromArduino[1]);
  }
}

function onSerialOpen() {
  console.log('Serial Port Opened');
}

function onSerialError(err) {
  console.log('Serial Port Error: ' + err);
}

 

 

 

Leave a Reply