Lab 6: Serial Communication

Date: March 17 2017

Instructor: Professor Mikesell

Author: Andrew Huang

Partner: Jacob

Materials used: Breadboard, buzzer, wires, Arduino, 10k resistor

Processing to Arduino

Our first part of the part of the project was to get the buzzer to go off when the mouse is pressed in processing. This code was somewhat trivial, since we already had the code from yesterday where we made the LED light up from clicking the square in Processing and Arduino. Modifying it to buzz was simply a matter of using the tone() function. After that, we decided to proceed onto the Arduino to processing.

Arduino to Processing

The second part of the lab involved us using the serial port to turn the square darker when the button was pressed. This part of the lab was more tricky for us, due to the fact that I omitted the crucial while loop that told me whether or not the port was available. Due to that fact, we got really weird values, such as negative ones; values that we originally did not write to the serial port. From that, we realized that our code somewhat mirrored our first exercises, and after help from an instructor, we figured the code out and got the button to work properly.

Conclusion:

This lab provided good intuition on how to use Serial to use processing to talk to Arduino, and vise versa. It also told me crucially, that the ports have to be available for either of them to read their values, otherwise, the values that come out are not coherent.

//Buzzer Code: 

//Processing:

import processing.serial.*;
Serial myPort;
int val;

void setup(){
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[2], 9600);
}

void draw(){
  if (mousePressed){
    myPort.write('H');
  } else {
    myPort.write('L');
  }
}

//Arduino:

int val;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  

}

void loop() {
  // put your main code here, to run repeatedly:
  while (Serial.available()){
    val = Serial.read();
  }
  if (val =='H'){
    //digitalWrite(13, HIGH);
    tone(13, 1200, 20);
  } else if (val == 'L'){
    //digitalWrite(13, LOW);
  }
  delay(10);
}

//_________________________________

//Button Code:

//Processing:

import processing.serial.*;
Serial myPort;
int val, value;

void setup(){
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[2], 9600);
}

void draw(){
  if ( myPort.available() > 0) {
  value = myPort.read();
  println(value);
  }
  rect(0, 1, 50, 50);
  if (value == 1){
    fill(0, 0, 0);
  } else {
    fill(255, 255, 255);
  }
}

//Arduino:

int val;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(13, INPUT);
  

}

void loop() {
  // put your main code here, to run repeatedly:
//  while (Serial.available()){
//    val = Serial.read();
//  }
  int read = digitalRead(13);
  if (read == 1){
    //digitalWrite(13, HIGH);
    //tone(13, 1200, 20);
    Serial.write(1);
  } else {
    //digitalWrite(13, LOW);
    Serial.write(0);
  }
  delay(100);
}

Leave a Reply