|
| 1 | +# Episode 6 - LED Matrix |
| 2 | +This example will allow you to modify your LED matrix with different arrays, and be able to turn it on/off with two buttons. |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +## Ingredients |
| 7 | +- Arduino UNO |
| 8 | +- LED Matrix (in this example a 16x16) |
| 9 | +- 2 buttons |
| 10 | +- Jumper wires |
| 11 | + |
| 12 | + |
| 13 | +## Wiring |
| 14 | +##### Connect the wires and components according to the fritzing below. |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +## Code |
| 20 | + |
| 21 | +```sh |
| 22 | +/* |
| 23 | + NeoMatrix with two buttons |
| 24 | + Control a NeoPixel LED matrix using two buttons. |
| 25 | + The LED data is stored in arrays, allowing the generation of |
| 26 | + possible "screens" using external software |
| 27 | + (c) 2018 Karl, Josefine, and David for Arduino |
| 28 | + this code is GPLv3 |
| 29 | + based on code by Shae Erisson (c) 2013 |
| 30 | +*/ |
| 31 | + |
| 32 | +#include <Adafruit_NeoPixel.h> |
| 33 | +#include "ledData.h" |
| 34 | + |
| 35 | +#ifdef __AVR__ |
| 36 | +#include <avr/power.h> |
| 37 | +#endif |
| 38 | + |
| 39 | +// NeoPixel string is connected to pin 6 |
| 40 | +#define PIN 6 |
| 41 | + |
| 42 | +// size of the LED matrix: 16x16 |
| 43 | +#define NUMPIXELS 256 |
| 44 | + |
| 45 | +// create the NeoPixel object |
| 46 | +Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); |
| 47 | + |
| 48 | +// variables to control the buttons |
| 49 | +// by default they have to be "HIGH" |
| 50 | +// because of using the internal pullup |
| 51 | +int buttonPin1 = 2; |
| 52 | +int buttonPin2 = 3; |
| 53 | +int buttonState1 = HIGH; |
| 54 | +int buttonState2 = HIGH; |
| 55 | +int buttonStateOld1 = HIGH; |
| 56 | +int buttonStateOld2 = HIGH; |
| 57 | + |
| 58 | +// see the other tab for checking |
| 59 | +// the arrays storing the LED data |
| 60 | + |
| 61 | +void setup() { |
| 62 | + // init the NeoPixel matrix |
| 63 | + pixels.begin(); |
| 64 | + |
| 65 | + // configure the buttons with internal pullup resistors |
| 66 | + pinMode(buttonPin1, INPUT_PULLUP); |
| 67 | + pinMode(buttonPin2, INPUT_PULLUP); |
| 68 | +} |
| 69 | + |
| 70 | +void loop() { |
| 71 | + // check the status of the buttons |
| 72 | + buttonState1 = digitalRead(buttonPin1); |
| 73 | + buttonState2 = digitalRead(buttonPin2); |
| 74 | + |
| 75 | + // if button 1 is pressed, load the image |
| 76 | + if (buttonState1 == LOW && buttonStateOld1 == HIGH) |
| 77 | + showImage(); |
| 78 | + |
| 79 | + // if button 2 is pressed, clear the image |
| 80 | + if (buttonState2 == LOW && buttonStateOld2 == HIGH) |
| 81 | + clearImage(); |
| 82 | + |
| 83 | + // update the button states |
| 84 | + buttonStateOld1 = buttonState1; |
| 85 | + buttonStateOld2 = buttonState2; |
| 86 | +} |
| 87 | + |
| 88 | +// send the color information to the screen taking the data |
| 89 | +// from the arrays declared at the beginning of the program |
| 90 | +void showImage() { |
| 91 | + for (int i = 0; i < NUMPIXELS; i++) |
| 92 | + pixels.setPixelColor(i, pixels.Color(red[i], green[i], blue[i])); |
| 93 | + |
| 94 | + pixels.show(); |
| 95 | +} |
| 96 | + |
| 97 | +// send a bunch of zeroes to the string |
| 98 | +void clearImage() { |
| 99 | + for (int i = 0; i < NUMPIXELS; i++) |
| 100 | + pixels.setPixelColor(i, pixels.Color(0, 0, 0)); |
| 101 | + |
| 102 | + pixels.show(); |
| 103 | +} |
| 104 | +``` |
| 105 | +## *IMPORTANT: You will also have to download the ledData.h file |
| 106 | +## Start using |
| 107 | +
|
| 108 | +After wiring and uploading the code, we can now start making our own 'pictures' on the LED matrix. What we need to modify is the ledData.h file, which contains 3 different arrays: red, green & blue. Each array has 256 numbers. For example, if we want to modify pixel number 35, we go to number 35 in the array, and write a value between 1-256 to set the brightness of it. |
| 109 | +
|
| 110 | +In combination with this code, we can also use a tool call PixelCrafter, a Processing powered application that where we can draw a picture with different colours, and then export it in the form of arrays. |
| 111 | +
|
| 112 | +
|
| 113 | +## Outcome |
| 114 | +
|
| 115 | +In this example we have gone through how an LED matrix works, and how arrays are used. We have also showed how we can call upon different functions and how we use an external file for data. |
| 116 | +
|
| 117 | +
|
| 118 | +
|
| 119 | +
|
0 commit comments