Sending Multiple Serial Data using Punctuation


const int buttonPin = 2; // digital input
void setup() {
// configure the serial connection:
Serial.begin(9600);
// configure the digital input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the first analog sensor:
int sensorValue = analogRead(A0);
// print the results:
Serial.print(sensorValue);
Serial.print(",");
// read the second analog sensor:
sensorValue = analogRead(A1);
// print the results:
Serial.print(sensorValue);
Serial.print(",");
// read the button:
sensorValue = digitalRead(buttonPin);
// print the results:
Serial.println(sensorValue);
}
i am confused if i don’t press the button, it keeps showing 0 or 1, if i press the button, it shows 1
Flow Control: Call and Response (Handshaking)
P5 CODE:
var serial; // variable to hold an instance of the serialport library
var portSelector; // a select menu for the port list
var locH = 0;
var locV = 0; // location of the circle
var circleColor = 255; // color of the circle
function setup() {
createCanvas(640, 480); // make canvas
smooth(); // antialias drawing lines
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
}
function draw() {
background(0); // black background
fill(circleColor); // fill depends on the button
ellipse(locH, locV, 50, 50); // draw the circle
}
// get the list of ports:
function printList(portList) {
// make a select menu and position it:
portSelector = createSelect();
portSelector.position(10, 10);
// portList is an array of serial port names
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
// console.log(i + " " + portList[i]);
// add item to the select menu:
portSelector.option(portList[i]);
}
// set a handler for when a port is selected from the menu:
portSelector.changed(openPort);
}
function openPort() {
var thisPort = portSelector.value();
if (thisPort != null) {
serial.open(thisPort);
}
}
function serverConnected() {
console.log('connected to server.');
}
function portOpen() {
console.log('the serial port opened.')
// send a byte to prompt the microcontroller to send:
serial.write('x');
}
function serialError(err) {
console.log('Something went wrong with the serial port. ' + err);
}
function portClose() {
console.log('The serial port closed.');
}
function serialEvent() {
// read a string from the serial port
// until you get carriage return and newline:
var inString = serial.readStringUntil("\r\n");
//check to see that there's actually a string there:
if (inString.length > 0) {
if (inString !== 'hello') { // if you get hello, ignore it
var sensors = split(inString, ','); // split the string on the commas
if (sensors.length > 2) { // if there are three elements
locH = map(sensors[0], 0, 1023, 0, width); // element 0 is the locH
locV = map(sensors[1], 0, 1023, 0, height); // element 1 is the locV
circleColor = 255 - (sensors[2] * 255); // element 2 is the button
}
}
serial.write('x'); // send a byte requesting more serial data
}
}
ARDUINO IDE CODE:
const int buttonPin = 2; // digital input
void setup() {
Serial.begin(9600);
while (Serial.available() <= 0) {
Serial.println("hello"); // send a starting message
delay(300); // wait 1/3 second
}
// configure the digital input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the first analog sensor:
int sensorValue = analogRead(A0);
// print the results:
Serial.print(sensorValue);
Serial.print(",");
// read the second analog sensor:
sensorValue = analogRead(A1);
// print the results:
Serial.print(sensorValue);
Serial.print(",");
// read the button:
sensorValue = digitalRead(buttonPin);
// print the results:
Serial.println(sensorValue);
if (Serial.available() > 0) {
// read the incoming byte:
int inByte = Serial.read();
// read the sensor:
sensorValue = analogRead(A0);
// print the results:
Serial.print(sensorValue);
Serial.print(",");
// read the sensor:
sensorValue = analogRead(A1);
// print the results:
Serial.print(sensorValue);
Serial.print(",");
// read the sensor:
sensorValue = digitalRead(buttonPin);
// print the results:
Serial.println(sensorValue);
}
}

when i pressed the button, the color could not be changed...and i was wondering maybe there was something wrong with the button, but i changed a new one, the color still didn’t change.
Project 2 Ideas
Shannon and i came up with several project ideas and we have been testing them this week.
the most recent one is about how noise influences living creatures. After moving to New York, I found it is so normal to hear the alarms of police car and ambulance, at first i found it so disturbing and annoying, and gradually i get used to it... we plan to make a sound visualization to show how noise influences us.
Particle - leisurely vs. impatient, when the volume gets louder the speed of the particles increases and hits the edge, when the volume gets smaller the particles move evenly on the screen
-Vigorous and calm contrast, the louder the sound the more active the particles, the smaller the sound the more calm the particles
OR
Flower-open and closed, the volume is low, the flower is open, high brightness, the volume is high, the flower is closed, the light is dark
Interaction: User selects the location/time of the sound to be played
1. same location but different time
2. Different locations at the same time
3. Different time different location (select location first then time)
Location: Central Park (Nature) Wall Street ()
Downtown Brooklyn (noisy) Brooklyn park (children)
Roosevelt Island (quiet)