Communicating Serially 🎙

Exercise 1:

    • P5.Js:

  • let serial; // variable to hold an instance of the serialport library
    let portName = "/dev/cu.usbmodem14101"; // fill in your serial port name here
    let xPos=0;
    let yPos=0;
    let onOff=0;
    
    function setup() {
      createCanvas(640, 480);
      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.on("connected", serverConnected); // callback for connecting to the server
      serial.on("open", portOpen); // callback for the port opening
      serial.on("data", serialEvent); // callback for when new data arrives
      serial.on("error", serialError); // callback for errors
      serial.on("close", portClose); // callback for the port closing
    
      serial.list(); // list the serial ports
      serial.open(portName); // open a serial port
    }
    
    function draw() {
      background(255);
      ellipse(xPos, height/2, 50, 50); // draw the circle
      if (mouseIsPressed){
        if(mouseX<=width/2)
          onOff=1;
        else
          onOff=2;
      }else{
        onOff=0;
      }
    }
    
    // get the list of ports:
    function printList(portList) {
      // portList is an array of serial port names
      for (let i = 0; i < portList.length; i++) {
        // Display the list the console:
        print(i + " " + portList[i]);
      }
    }
    
    function serverConnected() {
      print("connected to server.");
    }
    
    function portOpen() {
      print("the serial port opened.");
    }
    
    function serialEvent() {
      // read a string from the serial port
      // until you get carriage return and newline:
      let inString = serial.readLine();
     
      //check to see that there's actually a string there:
      if (inString.length > 0) {
        let sensors = split(inString, ","); // split the string on the commas
        if (sensors.length == 2) {
          // if there are three elements
          xPos = map(sensors[0], 0, 1023, 0, width); // element 0 is the locH
          yPos = map(sensors[1], 550, 250, 0, height); // element 1 is the locV
        }
      }
    
      serial.write(onOff);
    }
    
    function serialError(err) {
      print("Something went wrong with the serial port. " + err);
    }
    
    function portClose() {
      print("The serial port closed.");
    }
    
    /* ARDUINO CODE
    
    void setup() {
      Serial.begin(9600);
      pinMode(2, OUTPUT);
      pinMode(5, OUTPUT);
      while (Serial.available() <= 0) {
        Serial.println("0,0"); // send a starting message
        delay(300);              // wait 1/3 second
      }
    }
    void loop() {
      while (Serial.available() > 0) {
        // read the incoming byte:
       int inByte = Serial.read();
        switch (inByte) {
          case 0:
            digitalWrite(2, LOW);
            digitalWrite(5, LOW);
            break;
          case 1:
            digitalWrite(2, HIGH);
            break;
          case 2:
            digitalWrite(5, HIGH);
            break;
        }
        int sensorValue = analogRead(A0);
        Serial.print(sensorValue);
        Serial.print(",");
        sensorValue = analogRead(A1);
        Serial.print(sensorValue);
        Serial.println();
      }
    }
    
    */
    

     

    • Arduino:

      void setup() {
        Serial.begin(9600);
        pinMode(2, OUTPUT);
        pinMode(5, OUTPUT);
        while (Serial.available() <= 0) {
          Serial.println("0,0"); // send a starting message
          delay(300);              // wait 1/3 second
        }
      }
      void loop() {
        while (Serial.available() > 0) {
          // read the incoming byte:
         int inByte = Serial.read();
          switch (inByte) {
            case 0:
              digitalWrite(2, LOW);
              digitalWrite(5, LOW);
              break;
            case 1:
              digitalWrite(2, HIGH);
              break;
            case 2:
              digitalWrite(5, HIGH);
              break;
          }
          int sensorValue = analogRead(A0);
          Serial.print(sensorValue);
          Serial.print(",");
          sensorValue = analogRead(A1);
          Serial.print(sensorValue);
          Serial.println();
        }
      }

       

Exercise 2:

    • P5.js:

 let serial; // variable to hold an instance of the serialport library
let portName = "/dev/cu.usbmodem14101"; // fill in your serial port name here
let xPos=0;
let yPos=0;
let onOff=0;

function setup() {
  createCanvas(640, 480);
  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.on("connected", serverConnected); // callback for connecting to the server
  serial.on("open", portOpen); // callback for the port opening
  serial.on("data", serialEvent); // callback for when new data arrives
  serial.on("error", serialError); // callback for errors
  serial.on("close", portClose); // callback for the port closing

  serial.list(); // list the serial ports
  serial.open(portName); // open a serial port
}

function draw() {
  background(255);
  
  xPos = mouseX;
  mappedxPos = map(xPos, 0, 640, 0, 255);
  print(mappedxPos);

  ellipse(xPos, height/2, 50, 50); // draw the circle
  
}

// get the list of ports:
function printList(portList) {
  // portList is an array of serial port names
  for (let i = 0; i < portList.length; i++) {
    // Display the list the console:
    print(i + " " + portList[i]);
  }
}

function serverConnected() {
  print("connected to server.");
}

function portOpen() {
  print("the serial port opened.");
}

function serialEvent() {
  // read a string from the serial port
  // until you get carriage return and newline:
  let inString = serial.readLine();
 
  //check to see that there's actually a string there:
  if (inString.length > 0) {
    let sensors = split(inString, ","); // split the string on the commas
   /* if (sensors.length == 1) {
      // if there are three elements
      xPos = map(sensors[0], 0, 1023, 0, width); // element 0 is the locH
      // element 1 is the locV
    } */
  }

  serial.write(mappedxPos);
}

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

function portClose() {
  print("The serial port closed.");
}

/* ARDUINO CODE

void setup() {
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
  while (Serial.available() <= 0) {
    Serial.println("0,0"); // send a starting message
    delay(300);              // wait 1/3 second
  }
}
void loop() {
  while (Serial.available() > 0) {
    // read the incoming byte:
   int inByte = Serial.read();
    switch (inByte) {
      case 0:
        digitalWrite(2, LOW);
        digitalWrite(5, LOW);
        break;
      case 1:
        digitalWrite(2, HIGH);
        break;
      case 2:
        digitalWrite(5, HIGH);
        break;
    }
    int sensorValue = analogRead(A0);
    Serial.print(sensorValue);
    Serial.print(",");
    sensorValue = analogRead(A1);
    Serial.print(sensorValue);
    Serial.println();
  }
}

*/
    • Arduino:

void setup() {
  Serial.begin(9600);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  while (Serial.available() <= 0) {
    Serial.println("0,0"); // send a starting message
    delay(300);              // wait 1/3 second
  }
}
void loop() {
 
  while (Serial.available() > 0) {
    // read the incoming byte:
   int inByte = Serial.read();
       analogWrite(5, inByte);
       analogWrite(3, inByte);
    int sensorValue = analogRead(A0);
    Serial.print(sensorValue);
    Serial.println();
  }
}

 

Exercise 3:

 

void setup() {
  Serial.begin(9600);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  while (Serial.available() <= 0) {
    Serial.println("0,0"); // send a starting message
    delay(300);              // wait 1/3 second
  }
}
void loop() {
 
  while (Serial.available() > 0) {
    // read the incoming byte:
     int inByte = Serial.read();
  switch (inByte) {
      case 0:
        digitalWrite(3, HIGH);
        digitalWrite(5, HIGH);
        break;
      case 1:
        digitalWrite(3, LOW);
        digitalWrite(5, LOW);
        break;
  }
   int sensorValue = analogRead(A0);
    Serial.print(sensorValue);
    Serial.println();
}}

 

Leave a Reply