Fundamentals
of
Physical
Computing
Week 9 - Reinforcement on Topics
SVA IxD
2022.11.03
Today’s agenda:
Voltage divider circuit
Voltage divider circuit use cases
Variable resistance sensors
converting resistance into voltage (for arduino to read)
Arduino pin (V out)
R1
R2
Light Level | R1 (sensor) | R2 (fixed) | Portion (R2 / R1+R2) | V out (5V x Portion) |
dark | 10kΩ | 10kΩ | 0.5 | 2.5V |
dim | 6kΩ | 10kΩ | 0.625 | 3.125V |
bright | 1kΩ | 10kΩ | 0.9 | 4.5V |
Voltage divider circuit use cases
Potentiometers
One resistor divided into two, with R1 and R2 both changing as the divider slides, which changes their portion as well
Analog sensors that already output voltage
Serial Lab III
Bill of materials:
Microcontroller
Input
Output
Sensors of your choice
Sensor 1 - A0
Sensor 2 - A1
EXAMPLE CIRCUIT!!
Serial Lab III: Reading Sensors (and mapping if needed)
int sensorPin1 = A0;
int sensorVal1;
int sensorPin2 = A1;
int sensorVal2;
void setup() {
Serial.begin(9600);
}
void loop() {
sensorVal1 = analogRead(sensorPin1);
sensorVal2 = analogRead(sensorPin2);
// sensorVal1 = map(sensorVal1, ??, ??, 0, 1023);
// sensorVal2 = map(sensorVal2, ??, ??, 0, 1023);
Serial.print(sensorVal1);
Serial.print(", ");
Serial.println(sensorVal2);
}
Depends on your reading
Handshaking 🤝
Sometimes when the sender sends faster than the receiver can read, and the receiver program will slow down as the serial buffer fills up. You can manage this by implementing some form of flow control.
The simplest way do to this is using a call-and-response method, or handshaking, transmitting and receive a small set of messages to establish connection.
For example, the receiving program requests new data every time it finishes reading what it’s got, and the sender only sends data upon that request message.
Handshaking 🤝
STEP 1: Arduino opens Serial port
When port opens, send a message (‘hello’)
STEP 2: p5.js opens Serial port, establishes connection
Gets the message Arduino sent (‘hello’)
Sends a response message (‘x’)
STEP 3: Arduino
Gets the response p5.js sent (‘x’)
Runs code, sends serial data (‘val1, val2’)
STEP 4: p5.js
Gets incoming Serial data (‘val1, val2’), Reads till the end of one set
Sends a response message to request new set of data (‘x’)
Or any other message
Also can be any message (one byte will get one string of message)
Serial Lab III: Handshaking in Arduino
Modify setup() and below:
void setup() {
Serial.begin(9600);
while (Serial.available() <= 0) { // when serial just opened
Serial.println("hello"); // send a starting message
delay(300); // wait 300 millisecond
}
}
void loop() {
if (Serial.available() > 0) { // when there is a reading
// read the incoming byte from p5
int inByte = Serial.read();
// read sensor value
sensorVal1 = analogRead(sensorPin1);
sensorVal2 = analogRead(sensorPin2);
Serial.print(sensorVal1);
Serial.print(", ");
Serial.println(sensorVal2);
}
}
Serial Lab III: Handshaking in Arduino
Try typing something in your serial monitor!
Serial Lab III: Handshaking in p5
function portOpen() {
// send a byte to microcontroller:
serial.write('x');
}
function serialEvent() {
// code from before
if (inString.length > 0) {
if (inString !== 'hello') { // if getting hello, ignore, otherwise...
// code from before
}
serial.write('x'); // send a byte requesting more serial data
}
}
Serial Lab III: p5 sketch
Ball and line:
https://editor.p5js.org/sandpills/sketches/ohxqmh3_q
Moving Typography:
https://editor.p5js.org/sandpills/sketches/KlzfMj1A-
Project Assignment:
Option 1: Arduino to p5, an application that uses 3 inputs to emulate a mouse (mouseX, mouseY, clicking)
Option 2: p5 to Arduino, using mouseX and mouseY to control something (light, buzzer, ??)
^
we’ll provide an example for this as an optional lab if you’re interested in it, and feel free to come to office hours if you run into issues!