So far we’ve been relying on functions that were built into Arduino or ons that were added via libraries. Now we are going to create some of our own! Try out this code:
// This is a modified version of the NeoPixel Ring simple sketch (c) 2013 Shae Erisson // released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library // It was created for students at Divine Child High School #include <Adafruit_NeoPixel.h> #define PIN 6 #define NUMPIXELS 12 Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); int delayval = 500; void setup() { pixels.begin(); } void loop() { blue(); delay(delayval); off(); delay(delayval); } void off() { for(int i = 0; i < NUMPIXELS; i++){ pixels.setPixelColor(i, pixels.Color(0,0,0)); } pixels.show(); } void blue() { for(int i = 0; i < NUMPIXELS; i++){ pixels.setPixelColor(i, pixels.Color(0,0,150)); } pixels.show(); }
As you should know, all Arduino sketches must include void setup() and void loop(). However, you can include more functions as well. In this sketch I’ve created two new functions. The void means, “Just run the function but don’t return anything.” If you’re not quite sure what that means it’s ok, just be sure to include the void when you declare your function for now. You can name your function however you’d like. However, as with variable naming, just be sure you don’t use a word Arduino already “knows”.
You can create your functions wherever you like, as long as they are not created inside of another function. I tend to put all my functions below my loop. In your loop you simply call the function as you would any Arduino function. In my sketch I just need to type in blue(); The code will go to the void blue() function, run the code that is there and then go bak to the void loop and pick up where it left off.
You can also add arguments to your functions. You might do this so that you can reuse a function and have it do different things. look at this code.
// This is a modified version of the NeoPixel Ring simple sketch (c) 2013 Shae Erisson // released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library // It was created for students at Divine Child High School #include <Adafruit_NeoPixel.h> #define PIN 6 #define NUMPIXELS 12 Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); int delayval = 500; // delay for half a second void setup() { pixels.begin(); // This initializes the NeoPixel library. } void loop() { redWipe(100); blue(); delay(delayval); redWipe(20); blue(); delay(delayval); } void redWipe(int t) { for(int i = 0; i < NUMPIXELS; i++){ // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255 pixels.setPixelColor(i, pixels.Color(150,0,0)); pixels.show(); delay(t); } } void blue() { for(int i = 0; i < NUMPIXELS; i++){ // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255 pixels.setPixelColor(i, pixels.Color(0,0,150)); } pixels.show(); //update the color }
In void loop we call redWipe(100); and redWipe(20); You should notice that one of the wipes is faster than the other. Both wipes use the same function.
void redWipe(int t)
The argument in redWipe() is assigned to the variable t, which is used with delay() in the function. So, here we’ve created a function with a single argument which is a number representing the speed of the wipe. It is also possible to create functions with multiple arguments. You can send in as many variables as you’d like to get the result you want.
void loop() { redWipe(100, 500); blue(); delay(delayval); redWipe(20, 1000); blue(); delay(delayval); } void redWipe(int t, int d) { for(int i = 0; i < NUMPIXELS; i++){ // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255 pixels.setPixelColor(i, pixels.Color(150,0,0)); pixels.show(); delay(t); } delay(d); }
Time to Play
- Create a fading function for different colors
- Use a potentiometer to change the wipe speed
- Create a wipe that sets a random color on each pixel.
- Create some other cool functions
- Write a sketch that uses several of your functions to make a cool light show.
Want to write a function with color as an argument? It turns out to be pretty easy. We just need to use a special variable type. Color content more information than we can store in an int. For this we need uint32_t, which is a special kind of integer that will store a lot more information.
void loop() { setRingColor(pixels.Color(150,0,150)); delay(delayval); setRingColor(pixels.Color(50,150,150)); delay(delayval); } void setRingColor(uint32_t c) { for(int i = 0; i < NUMPIXELS; i++){ pixels.setPixelColor(i, c); } pixels.show(); }
- Make a color wipe function
- Use a potentiometer and/or photoresists to change the color