1 of 16

Fundamentals

of

Physical

Computing

Week 9 - Reinforcement on Topics

SVA IxD

2022.11.03

2 of 16

Today’s agenda:

  • Review unclear concepts and questions
  • Serial Communications Lab, part III
  • Quick demonstration of LED strip

3 of 16

Voltage divider circuit

  • One voltage source and 2 resistors connected in series
  • Turns a larger voltage (Vin) into a smaller one (Vout)
  • The output voltage is directly proportional to the input voltage and the ratio of R1 and R2.

4 of 16

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

5 of 16

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

6 of 16

Analog sensors that already output voltage

7 of 16

Serial Lab III

Bill of materials:

  • Redboard / Arduino Uno board
  • Breadboard
  • jumper wires
  • 2 analog inputs of your choice (photocell, FSR, flex sensor, potentiometer, or anything else) + necessary resistors

Microcontroller

Input

Output

Sensors of your choice

8 of 16

Sensor 1 - A0

Sensor 2 - A1

EXAMPLE CIRCUIT!!

9 of 16

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

10 of 16

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.

11 of 16

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)

12 of 16

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);

}

}

13 of 16

Serial Lab III: Handshaking in Arduino

Try typing something in your serial monitor!

14 of 16

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

}

}

15 of 16

Serial Lab III: p5 sketch

16 of 16

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!