W6 Lab

In formatting multiple serial data with punctuation, I used two potentiometers attached to A0 and A1 and a switch attached to digital pin 2. I got my pot readings however I was having some trouble reading the switch. I thought it was the problem of my code but I think it is the way I set up my circuit. I first setup my switch like this: connecting a resistor from 5V to switch and then another line from switch to ground.  When I pressed the switch, the digital reading doesn't change, it stays at 0. I tested with an LED the switch was able to turn the LED on and off  successfully it just wouldn't change the reading at digital read.

Attempt 1 (fail)

Attempt 1 (fail)

img_7931.jpg

so I reversed the resistor and the dark wire to have resistor going to ground from switch, and it worked successfully and was giving reading in the correct format as: pot1, pot2, switchState


Code:

const int switchPin = 2;

void setup() {

Serial.begin(9600);

pinMode(switchPin, INPUT);

}

void loop() {

int pot1 = analogRead(A0);

int pot2 = analogRead(A1);

int switchState = digitalRead(switchPin);

Serial.print(pot1);

Serial.print('\t');

Serial.print(pot2);

Serial.print('\t');

Serial.println(switchState);

}

Flow Control: Call and Response (Handshaking):

Code:

const int switchPin = 2;

void setup() {

Serial.begin(9600);

pinMode(switchPin, INPUT);

//sends out a message until it gets a byte of data from the remote computer

if (Serial.available() <= 0) {

//open serial port wait for "hello"

Serial.println("hello");

// send a starting message/Send a byte to request data

delay(300); // wait 1/3 second }

}

void loop() {

if (Serial.available() > 0) {

// Wait for one set of data

int inByte = Serial.read(); // Send a byte to request new data

int pot1 = analogRead(A0);

int pot2 = analogRead(A1);

int switchState = digitalRead(switchPin);

Serial.print(pot1);

Serial.print('\t');

Serial.print(pot2);

Serial.print('\t');

Serial.println(switchState);

}

}

In this handshaking code, however, I was still a little confused at the order of how each time one set of data is transmitted and received between the Arduino and computer (int inByte = Serial.read()) 

Discover & share this Animated GIF with everyone you know. GIPHY is how you search, share, discover, and create GIFs.

Draw dots with potentiometer:

I sent my potentiometer reading from analogRead(A0) as a numeric string (0-1023) to my p5 sketch using println(), and my p5 sketch receives the data as a string value with serial.readLine() and converted it to a number to store in inData and had inData mapped to 0-500, which corresponds to the number of dots on the canvas. As I turn the knob up and down, number of dots increases and decreases.

Arduino code:

int potValue;

void setup() {

Serial.begin(9600);

}

void loop() {

potValue = analogRead(A0);

Serial.println(potValue); //send as a numeric string (0-1023) using println

delay(1);

}

P5 code: https://editor.p5js.org/ada10086/sketches/Bycb6z39m

For application, I previewed two-way serial communication, and took one of my animation sketches from ICM class and have it controlled by Arduino.

In my original p5 sketch, I have two sliders one controlling the scaling speed and the other controlling the rotating speed of the squares, and also a button changing the color of the squares randomly. Each controlled by mouse dragging or clicking. So I thought it might be a good sketch to apply to serial communication as I can replace mouse interaction with potentiometers and button on my Arduino.

My circuit

My circuit

Arduino code:

//serial input application

//sending multiple serial data with punctuation

//two potentiometers and one button controlling p5 animation

int pot1;

int pot2;

int buttonState;

void setup() {

Serial.begin(9600);

pinMode(2,INPUT); }

void loop() {

pot1 = analogRead(A0);

Serial.print(pot1);

Serial.print(",");

pot2 = analogRead(A1);

Serial.print(pot2);

Serial.print(",");

buttonState = digitalRead(2);

Serial.println(buttonState); }

my p5 code: https://editor.p5js.org/ada10086/sketches/r1oeUHTc7

My problem for this application is that my sketch jiggles a lot. when I apply change to one potentiometer to change scaling speed, for example, the other which controls the rotating speed would jiggles a little, and when I press the button, both parameters would jiggle, compared to accurate reaction of change in mouse actions in my original sketch. I ran println in my arduino serial monitor and noticed that there is a lot of jiggling in my sensor readings. I am not sure though if it is due to my hardware connection or problem in my serial communication.