Morgan's Website

  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Home
  • Group Project Research
  • Group Project Read
  • Finished Group Research Project
  • Midterm Project: Proposal
  • Midterm Term Project: Make, Present, Report
  • Final Project Essay
  • Study Buddy – Morgan Somchanhmavong – Professor Rudi
  • Actuators and Mechanisms
  • Animated Poster
  • Arduino Basics
  • Digital Fabrication
  • Electronics and Soldering
  • Final Project Essay
  • Finished Group Research Project
  • Group Project Read
  • Group Project Research
  • Image and Video
  • Midterm Project: Proposal
  • Midterm Term Project: Make, Present, Report
  • Neopixel Music Visualization
  • Preparatory Research and Analysis
  • Processing Basics
  • Serial Communication
  • Study Buddy – Morgan Somchanhmavong – Professor Rudi
  • Three Project Proposal
  • Workout

Serial Communication

November 22, 2022 by Morgan Somchanhmavong Leave a Comment

For this recitation, the first few steps included building a circuit with two potentiometers and writing an Arduino sketch that reads their values and sends them serially. I didn’t have trouble building the circuit as it was a similar process to what we’ve done in previous recitations many times. I tested the connections by opening the serial monitor in Arduino and turning the potentiometers to see if the values were reactive to the potentiometers’ motion, meaning that the potentiometers were working. 

 After knowing I had a working circuit, I wrote a processing sketch drawing a circle and read the two analog values from Arduino.  The sketch modifies the circle’s x and y values based on the input values from Arduino from the potentiometers connected to the circuit. 


 The circle moved up and down based on the potentiometer values. I had trouble creating lines from the movement of the circle. It was supposed to emulate an etch-a-sketch and create a drawing from the movements of plastic knobs requiring similar motion as potentiometers. 

Code:

Arduino Code:

#include "SerialRecord.h"
// Change this number to send a different number of values
SerialRecord writer(2);
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
writer[0] = millis() % 1024;
writer[1] = sensorValue;
writer.send();
// This delay slows down the loop, so that it runs less frequently. This can
// make it easier to debug the sketch, because new values are printed at a
// slower rate.
delay(10);
}
 
Processing Code:

import processing.serial.*;
import osteele.processing.SerialRecord.*;

Serial serialPort;
SerialRecord serialRecord;

void setup() {
size(500, 500);

String serialPortName = SerialUtils.findArduinoPort();
serialPort = new Serial(this, serialPortName, 9600);

// If the Arduino sketch sends a different number of values, modify the number
// `2` on the next line to match the number of values that it sends.
serialRecord = new SerialRecord(this, serialPort, 2);
}

void draw() {
background(0);

serialRecord.read();
int value1 = serialRecord.values[0];
int value2 = serialRecord.values[1];

float x = map(value1, 0, 1024, 0, width);
float y = map(value2, 0, 1024, 0, height);
circle(x, y, 20);
}

The recitation was fantastic in using the physical circuits to create visual drawings on our computers. I never thought of the parallel between the etch-a-sketch and the potentiometers’ motion. 
 
Task #2
My partner’s name is Shelly, and we worked together on task #2, for we had to make a ball bounce from the left to the right side of the screen. We also tried to use two servo motors to create a sort of hand-slapping-like motion to create a more interactive visual. However, we were only able to get one servo to work. We built the new circuit off the old one, connecting the servos to the existing breadboard.

 
We worked well together by dividing up the work. I focused more on the physical building of the circuit while Shelly did most of the coding. We would help each other out when we ran into problems. 
 
Arduino Code:
#include "SerialRecord.h"
#include <Servo.h>
Servo myservo;
Servo myservo2;
int step;
int pos;
// Change this number to the number of values you want to receive
SerialRecord reader(2);
void setup() {
// Servos
myservo.attach(9);
myservo2.attach(8);
myservo.write(0);
myservo2.write(0);
Serial.begin(9600);
}
void loop() {
if (reader.read()) { //Rudi helped and suggested
if (reader[0] == 1) {
for (pos = 0; pos < 120; pos += 3) {
myservo.write(pos);
delay(3);
}
for (pos = 120; pos > 0; pos -= 3) {
myservo.write(pos);
delay(10);
}
}
if (reader[1] == 1) {
for (pos = 0; pos < 120; pos += 3) {
myservo2.write(pos);
delay(3);
}
for (pos = 120; pos > 0; pos -= 3) {
myservo2.write(pos);
delay(10);
}
}
}
}
 
Processing Code:

import processing.serial.*;
import osteele.processing.SerialRecord.*;

Serial serialPort;
SerialRecord serialRecord;
int step=2;
int x=0;
void setup() {
fullScreen();
size(500, 500);
background(0);
noCursor();

String serialPortName = SerialUtils.findArduinoPort(2);
serialPort = new Serial(this, serialPortName, 9600);
serialRecord = new SerialRecord(this, serialPort, 2);
serialRecord.values[0] = 0;
serialRecord.values[1] = 0;

serialRecord.send();
}

void draw() {
background(0);

//float x = map(value1, 0, 1024, 0, width);
x+=step;
//if (x>=90|| x<=0){
//step=-1*step;
//}
float r=radians(x);
float move=sin(r)*width/2+width/2;

circle(move, height/2, 80);
serialRecord.values[0] = 0;
serialRecord.values[1] = 0;
if (x%360==180+step) {
serialRecord.values[0] = 1;
//serialRecord.send();
} else if (move==0) {
serialRecord.values[1] = 1;
//serialRecord.send();
}
serialRecord.send();
}

We found this task quite challenging. Mainly the coding aspect of getting the servos to do precisely what we wanted, such as range of motion and timing, was challenging. The initial task also gave me coding difficulties. I’ll need to practice and improve my coding knowledge to be more proficient during the recitations. 

Filed Under: Uncategorized

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Study Buddy – Morgan Somchanhmavong – Professor Rudi
  • Image and Video
  • Digital Fabrication
  • Final Project Essay
  • Serial Communication

Recent Comments

No comments to show.

Copyright © 2025 · Agency Pro on Genesis Framework · WordPress · Log in