Physical Computing and Robotics - Week 5-1
Physical Computing and Robotics - Week 5-1
(ITS310)
Week 5, Lecture 9
Array - Serial Communication - Temperature Sensor
Lecture Outline
➢ Using Arrays
➢ Challenges
➢ Serial Communication
➢ Temperature Sensor
➢ Shift Register
Using Arrays:
● Arrays are created in the C language with the following formula:
○ int myArray[] = {2, 5, 6, 10};
○ The above creates an array of 4 integer values called myArray with the
given values.
● Remember:
○ The power of arrays comes from using loops.
○ Use a variable to introduce a delay.
Array in Action
int LED_PIN[] = {2, 3, 4, 5, 6, 7, 8, 9}; // LED pins
int LED_COUNT= 8; // number of LEDs
void setup() {
for (int i = 0; i < LED_COUNT; i++) { // Counting up to 8 to consider the elements in the array
pinMode(LED_PIN[i], OUTPUT); // Setting each LED pin on the arduino to OUTPUT
}
}
void loop()
for (int i = 0; i < LED_COUNT; i++) { // Counting up to 8 to consider the elements in the array
digitalWrite(LED_PIN[i], HIGH); //Turning on each LED after the other in sequence
delay(200);
}
for (int i = 0; i < LED_COUNT; i++) {
digitalWrite(LED_PIN[i], LOW); //Maintaining the sequence, each LED is turning off.
delay(200);
}
}
Circuit Connections (LEDs)
Multiple LEDs Practice
● LED Chase Effect Challenges:
● Challenge 1:
○ Get the LEDs at both ends of the strip to start as on, then move towards each
other, appear to bounce off each other, and then move back to the end.
● Challenge 2: Homework
○ Make a bouncing ball effect by turning the LEDs so they are vertical, then
make an LED start at the bottom, then bounce up to the top LED, then back
to the bottom, then only up to the 9th LED, then back down, then up the 8th
LED, and so on to stimulate a bouncing ball losing momentum after each
bounce.
Input/Output - Digital/Analog - Functions
A Business/Capstone Idea → Infinite LED Dance Floor
Serial Communications
● Once a sketch is uploaded to Arduino, it keeps running as long as the board is
powered.
○ Sometimes, it is necessary to communicate with your machine and your Arduino
sketch while running.
● One way to send data over a serial link to your Arduino is through the Arduino Serial
Monitor using Serial Communications.
○ Serial communication is the process of sending data one bit at a time across a
communication link.
● Which can be activated by clicking its icon on the Arduino IDE.
○ Or access the window from Tools → Serial Monitor
● The Serial Monitor has an input and an output window, where you can type and see
the data that is being communicated.
Serial Communications
Setup for Serial Communications:
● Serial communications with Arduino requires the followings:
● Serial.begin(baudRate) tells the Arduino to start serial communications.
○ The baud rate is the number of pulses that your port can send per
second.
○ 9600 is recommended for communicating with Arduino.
○ 9600 pulses means 9600 bits per second → 1200 bytes per second
● The above function need to run when your board starts.
○ Therefore, you add them to setup() function of your sketch.
Setup for Serial Communications: This might look familiar
(the board) Writing to the Serial Monitor:
● One very good use of the Serial Monitor is debugging.
○ When you have to check for values of certain variables while your program is running.
● You can print something out to the serial monitor using Serial.print().
○ Prints consequent calls on the same line (horizontal).
○ For example: value 1 value 2 value 3 value 4
● You can print on different lines (vertically) using Serial.println().
○ For example:
■ Value 1
■ Value 2
■ Value 3
● One limitation of print and println is that they cannot take more than one value to print at a
time.
○ Therefore, you need to use multiple calls if you like to print multiple values.
● The above two functions usually go into your loop() function.
(the board) Writing to the Serial Monitor:
● Example 1 (Checking Analog Values):
○ Connect a potentiometer to your Arduino board and print out the values you read from the
potentiometer, in volts format.
Solution:
void setup(){
Serial.begin(9600);
}
void loop(){
int state = analogRead(A0);
Serial.println(state * 0.0048);
delay(100);
}
(the board) Writing to the Serial Monitor:
● Example 2 (Checking Digital Values):
○ Connect a push button to your Arduino board and print out the states you read from the push
button.
Solution:
void setup(){
Serial.begin(9600);
pinMode(13, INPUT);
}
void loop(){
int state = digitalRead(13);
Serial.println(state);
delay(100);
}
(the board) Reading from the Serial Monitor:
● Serial.available() returns the number of characters that are in the serial pipeline.
○ You can use this function to see if anything has been sent or not.
○ If (Serial.available() > 0){}
● You cannot read the entire content of the serial pipeline.
○ You have to read it character by character (one byte at a time).
● Therefore,
○ You have to use a built-in Arduino function to read the entire characters
before a delimiter character is encountered.
○ Every time, you read one character using Serial.read()
○ Every time, you read a string of characters using Serial.readStringUntil(‘\n’)
(the board) Reading from the Serial Monitor:
● Use a typical hardware setup to control an action through serial
communications instead of timing: a simple LED circuit or a buzzer.
○ For this purpose, you need to send messages, such as a string for turning
the light on and another for turning the light off.
○ You can use other methods to read string characters as input from the Serial
Monitor, such as using arrays and while loops and save them in a buffer.
Physical Computing and Robotics
(ITS310)