Arduino 102
Creative Technology Residency
Last Class
Class Outline
Functions
For Loops
Block of code that repeats instruction over a range of values
Example: fading an LED, accelerating a motor
for (initialization; condition; increment) {
// executable code
}
Example: fading RGB from OFF to red
for(int r = 0; r <= 255; r ++){
writeRGB(r, 0, 0);
}
Nested For Loops
We can put for() loops inside each other
Useful for iterating over multiple varibales/counters
for (initialization; condition; increment) {
// executable code
for (initialization; condition; increment) {
// executable code
}
}
Example: RGB transitions
Smooth Colour Wheel Transitions
A more elegant approach to RGB transitions is the use of a colour wheel
Need to fade in on one colour while fading out on another
While Loops
A loop that executes continuously until the conditional state turns FALSE
Danger of getting stuck in an infinite loop
for() loop | while() loop |
for(int r = 0; r <= 255; r ++){ writeRGB(r, 0, 0); } | int r = 0; while(r <= 255){ writeRGB(r, 0, 0); r ++; } |
LED Problems of Scale
Solution? NeoPixels!
How to Wire NeoPixels
Installing Libraries & Programming NeoPixels
Soldering LED strips
What are libraries
Provide extended features specific to individual modules beyond what the standard Arduino environment provides.
An example of a pre-installed library is the Servo library
What is a servo?
A Simpler NeoPixel Program
strip.setPixelColor(index, R, G, B);
Example code:
strip.setPixelColor(4, 255, 0, 0);
strip.setPixelColor(7, 255, 0, 255);
index 4
index 7
Another NeoPixel Program
strip.fill(colour, first_index, led_count);
Example code:
strip.fill(red, 3, 6);
strip.fill(green, 9, 3);
index 0
index 3
turn 6 LEDs red, then stop
turn 3 LEDs green, then stop
index 9
Alternative NeoPixel Form Factors
What is a Servo?
map() function
map() function is used for rescaling a range of values
Exercise:
Exercise: Create a project from the ground up
When starting a new project you may need to use a component you’ve never seen before? How do you learn how to use it?
Building an automated plant watering system:
Materials
Open Discussion: Planning Month 2 Project(s)
Exercises to Practice