Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED...
Transcript of Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED...
![Page 1: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/1.jpg)
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 1
Which LED(s) turn on?
![Page 2: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/2.jpg)
Lab 3bProgramming the LED cube
ENGR 40M
Chuan-Zheng Lee
Stanford University
12 May 2017
![Page 3: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/3.jpg)
Overview
• Goal: To write display(), which takes a 4×4×4 pattern[][][] array and displays it on the LED cube.
• Challenges:
• Electrically, the LED “cube” actually an 8×8 grid.
• We’d rather not think about time-division multiplexing.
• How can we write a function that allows to abstract these hardware details away from our “main” software?
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 3
![Page 4: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/4.jpg)
Arrays in C
An array is a sequential collection of elements of the same data type
e.g. int myArr[7];
declares space for seven ints:
These are stored next to each other in memory.
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 4
myArr[0] myArr[1] myArr[2] myArr[3] myArr[4] myArr[5] myArr[6]
![Page 5: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/5.jpg)
Arrays in C
• You can access array elements with []:
int fourthElement = myArray[3];myArray[4] = -29;
• You can iterate over array elements:
int sum = 0;for (int i = 0; i < 7; i++) {
sum = sum + myArray[i];}
• Remember, indices start at zero
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 5
![Page 6: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/6.jpg)
Quiz
If we have the array declaration
unsigned byte x[10];
what are valid ways to iterate over the array?
a) for (int i=0; i < 10; i++)Serial.println(x[i]);
b) for (int i=0; i <= 10; i++)Serial.println(x[i]);
c) for (int i=10; i > 10; i--)Serial.println(x[i]);
d) for (int i=9; i >= 0; i--)Serial.println(x[i]);
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 6
![Page 7: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/7.jpg)
Warning
The compiler will not tell you what went wrong!
unsigned byte x[10];
for (int i = 1; i <= 10; i++)Serial.println(x[i]);
It will just happily read whatever is in memory afterthe array, whether it makes sense to or not. If it doesn’t make sense, your program may crash without warning or error message.
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 7
![Page 8: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/8.jpg)
Arrays can be multidimensional
e.g. int cube[4][4][4];
myArr[3][0][0] myArr[3][0][1] myArr[3][0][2] myArr[3][0][3]
myArr[3][1][0] myArr[3][1][1] myArr[3][1][2] myArr[3][1][3]
myArr[3][2][0] myArr[3][2][1] myArr[3][2][2] myArr[3][2][3]
myArr[3][3][0] myArr[3][3][1] myArr[3][3][2] myArr[3][3][3]
myArr[2][0][0] myArr[2][0][1] myArr[2][0][2] myArr[2][0][3]
myArr[2][1][0] myArr[2][1][1] myArr[2][1][2] myArr[2][1][3]
myArr[2][2][0] myArr[2][2][1] myArr[2][2][2] myArr[2][2][3]
myArr[2][3][0] myArr[2][3][1] myArr[2][3][2] myArr[2][3][3]
myArr[1][0][0] myArr[1][0][1] myArr[1][0][2] myArr[1][0][3]
myArr[1][1][0] myArr[1][1][1] myArr[1][1][2] myArr[1][1][3]
myArr[1][2][0] myArr[1][2][1] myArr[1][2][2] myArr[1][2][3]
myArr[1][3][0] myArr[1][3][1] myArr[1][3][2] myArr[1][3][3]
Multidimensional arrays in C
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 8
myArr[0][0][0] myArr[0][0][1] myArr[0][0][2] myArr[0][0][3]
myArr[0][1][0] myArr[0][1][1] myArr[0][1][2] myArr[0][1][3]
myArr[0][2][0] myArr[0][2][1] myArr[0][2][2] myArr[0][2][3]
myArr[0][3][0] myArr[0][3][1] myArr[0][3][2] myArr[0][3][3]
![Page 9: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/9.jpg)
Multidimensional arrays in C
• You access elements with multiple []:
cube[2][0][1] = 1;
• You iterate using nested loops:
int sum = 0;for (int i = 0; i < 4; i++) {for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {sum = sum + cube[i][j][k];
}}
}
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 9
![Page 10: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/10.jpg)
Decomposition
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 10
loop()
display()
getLEDState()
The main Arduino loop
Does one pass through the LEDs
(time-division multiplexing)
Looks up the LED state
associated with an
anode/cathode pair
calls
which calls
![Page 11: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/11.jpg)
loop() delegates to display()
void loop() {
byte pattern[4][4][4];
// (assemble the LEDs pattern)
display(pattern);
}
void display(byte pattern[4][4][4]) {
for (byte aNum = 0; aNum < 8; aNum++) {
for (byte cNum = 0; cNum < 8; cNum++) {
// LED multiplexing code
}
}
}
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 11
![Page 12: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/12.jpg)
display() delegates to getLEDState()
void display(byte pattern[4][4][4]) {
for (byte aNum = 0; aNum < 8; aNum++) {
for (byte cNum = 0; cNum < 8; cNum++) {
byte value = getLEDState(pattern, aNum, cNum);
// LED multiplexing code
}
}
}
byte getLEDState(byte pattern[4][4][4], byte aNum, byte cNum) {
// map from (𝑎, 𝑐) to (𝑥, 𝑦, 𝑧)
return pattern[z][y][x];
}
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 12
![Page 13: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/13.jpg)
Review: Pin multiplexing
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 13
an
od
e (
+)
wir
es
cathode (−) wires
……
![Page 14: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/14.jpg)
Review: Providing enough current
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 14
from
Arduino
Use a PMOS to provide enough power to the LEDs
![Page 15: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/15.jpg)
Implementing display()
void display(byte pattern[4][4][4]) {
for (byte aNum = 0; aNum < 8; aNum++) {
for (byte cNum = 0; cNum < 8; cNum++) {
byte value = getLEDState(pattern, aNum, cNum);
// Activate the cathode (-) wire (column) if value > 0,
// deactivate it otherwise
}
// Activate the anode (+) wire (row)
// Wait a bit
// Deactivate the anode (+) wire (row)
}
}
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 15
![Page 16: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/16.jpg)
display() delegates to getLEDState()
void display(byte pattern[4][4][4]) {
for (byte aNum = 0; aNum < 8; aNum++) {
for (byte cNum = 0; cNum < 8; cNum++) {
byte value = getLEDState(pattern, aNum, cNum);
// LED multiplexing code
}
}
}
byte getLEDState(byte pattern[4][4][4], byte aNum, byte cNum) {
// map from (𝑎, 𝑐) to (𝑥, 𝑦, 𝑧)
return pattern[z][y][x];
}
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 16
![Page 17: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/17.jpg)
Mapping between 2-D and 3-D
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 17
![Page 18: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/18.jpg)
Mapping between 2-D and 3-D
We want a function that maps
from anode/cathode pairs𝑎, 𝑐
(e.g. “D6”)
to 3D coordinates𝑥, 𝑦, 𝑧
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 18
![Page 19: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/19.jpg)
Mapping between 2-D and 3-D
Anodes Cathodes
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 19
![Page 20: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/20.jpg)
Making the mapping function easier
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 20
You can reorder the anodes/cathodes however you like.
Would a different ordering make the relationship simpler?
![Page 21: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/21.jpg)
Mapping between 2-D and 3-D
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 21
![Page 22: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/22.jpg)
Mapping between 2-D and 3-D
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 22
![Page 23: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/23.jpg)
What do bitwise operations do?
• Bitwise operations apply to each bit in the binary representation of a number individually
Examples (in binary):
00110101 | 01100011 == 01110111
00110101 & 01100011 == 00100001
00110101 00110101
| 01100011 & 01100011
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 23
![Page 24: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/24.jpg)
Implementing getLEDState()
void getLEDState(byte pattern[4][4][4], byte aNum, byte cNum) {
x = ???;
y = ???;
z = ???;
return pattern[z][y][x];
}
(Your function can be more than four lines, depending on how you implement it.)
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 24
![Page 25: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/25.jpg)
Decomposition
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 25
loop()
display()
getLEDState()
The main Arduino loop
Does one pass through the LEDs
(time-division multiplexing)
Looks up the LED state
associated with an
anode/cathode pair
calls
which calls
![Page 26: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/26.jpg)
A note on coding style
• Indent your code correctly(Seriously, pay attention to this!)
• Don’t just keep adding braces until your code compiles
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 26
![Page 27: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/27.jpg)
A note on coding style
Why indentation matters:
for(int i = 0; i < 5; i++){
for(int j = 0; j < 2; j++){
if(i == 0 && j == 0)
print("Hello!\n");
}
print("Hello!\n");
}
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 27
![Page 28: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/28.jpg)
A few pointers
• Give variables sensible namesbyte whichOne;byte currentPatternIndex;
• Use constants for things that shouldn’t changeconst byte TEST_PIN = 3;
• Watch out for = and ==if (x = 3) { // always true
print("winning!");}
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 28
![Page 29: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/29.jpg)
A note on coding style
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 29
https://xkcd.com/1513/
![Page 30: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/30.jpg)
Planning your breadboard
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 30
![Page 31: Which LED(s) turn on? - Stanford University...the LED cube. •Challenges: •Electrically, the LED “cube” actually an 8×8 grid. •We’d rather not think about time-division](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6e7a5ed7b9dc553774f2ff/html5/thumbnails/31.jpg)
What makes a good breadboard layout?
• Easy to follow
• Wires are relatively direct (and color-coded)
No spaghetti-like crossovers
• Resistors don’t touch each other
• Your cube wiring should also be neat
• Try to keep fairly compact, to allow for things to
add next week!
May 12, 2017 ENGR 40M Spring 2017 — C.Z. Lee, J. Plummer, R. Howe 31