Object - Digital I/O
A simple switch system for RGB LEDs using the neopixel and 3 switches
Components and supplies
3
Resistor 10k ohm
3
Slide Switch
1
Arduino UNO
1
Jumper wires (generic)
1
Breadboard (generic)
1
NeoPixel strip
Project description
Code
neopixel-rgb-switches.ino
arduino
1// Made by: Caleb Wright 2// 5-15-2018 3// Object - Maymester 4// Digital I/O 5 6// Schematic for circut: https://clbwright.files.wordpress.com/2018/05/img_9968-e1526449729541.jpg?w=680 7// Blog post on process: https://wp.me/p9Drf1-18 8 9// Include libraries and schtuff. 10#include <Adafruit_NeoPixel.h> 11#ifdef __AVR__ 12 #include <avr/power.h> 13#endif 14// Which pin are you feeding info into for the IALED? 15#define PIN 5 16// How many NeoPixels are attached to the Arduino? 17#define NUMPIXELS 5 18 19// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals. 20// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest 21// example for more information on possible values. 22Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); 23 24int delayval = 50; // set our delay value to 1/20th of a second, or 50 ms. 25int redVal = 25; // red starts as 25 (this is barely on, because I didn't love it when it was on full-chooch.) 26int greenVal = 25; // green starts at 25 27int blueVal = 25; // blue starts at 25 28 29void setup() { 30 pixels.begin(); // This initializes the NeoPixel library. 31 pinMode(11, INPUT); // Define pin 11 as an input 32 pinMode(10, INPUT); // Define pin 10 as an input 33 pinMode(9, INPUT); // Define pin 9 as an input 34 pinMode(6, INPUT); // Define pin 6 as an input 35 pinMode(5, OUTPUT); // Define pin 5 as an output 36 // below is a little loading "animation" that runs a little test of all the pixels on the first power-on 37 for(int i = 0; i<NUMPIXELS; i++){ // This for loop runs, and increments i each time it finishes the loop, until it hits whatever value is set for NUMPIXELS 38 pixels.setPixelColor(i, pixels.Color(25,0,0)); // Set the pixel at that index to 25 red, and nothing else 39 pixels.setPixelColor(i-1, pixels.Color(0,0,0)); // Set the pixel behind this to black 40 pixels.show(); // This sends the updated pixel color to the hardware. 41 delay(100); // Delay for a period of time (in milliseconds). 42 pixels.setPixelColor(NUMPIXELS-1, pixels.Color(0, 0, 0)); // now make sure the last pixel is off, because it will not turn off if we don't tell it to 43 pixels.show(); // show that 44 } 45 for(int i = 0; i<NUMPIXELS; i++){ 46 pixels.setPixelColor(i, pixels.Color(0,25,0)); // set the pixel at that index to 25 green, and nothing else 47 pixels.setPixelColor(i-1, pixels.Color(0,0,0)); // set whatever pixel is behind that index to black 48 pixels.show(); // This sends the updated pixel color to the hardware. 49 delay(100); // Delay for a period of time (in milliseconds). 50 pixels.setPixelColor(NUMPIXELS-1, pixels.Color(0, 0, 0)); // last pixel to black 51 pixels.show(); // update color 52 } 53 for(int i = 0; i<NUMPIXELS; i++){ 54 pixels.setPixelColor(i, pixels.Color(0,0,25)); // set the pixel at that index to 25 blue, and nothing else 55 pixels.setPixelColor(i-1, pixels.Color(0,0,0)); // set the pixel behind this to black 56 pixels.show(); // This sends the updated pixel color to the hardware. 57 delay(100); // Delay for a period of time (in milliseconds). 58 pixels.setPixelColor(NUMPIXELS-1, pixels.Color(0, 0, 0)); // last pixel to black 59 pixels.show(); // update color 60 } 61} 62 63void loop() { 64 if(digitalRead(11) == HIGH){ // If pin 11 is recieveing a voltage, set the red value to 25 65 redVal = 25; 66 } 67 if(digitalRead(10) == HIGH){ // If pin 10 is recieveing a voltage, set the green value to 25 68 greenVal = 25; 69 } 70 if(digitalRead(9) == HIGH){ // If pin 9 is recieveing a voltage, set the blue value to 25 71 blueVal = 25; 72 } 73 while(digitalRead(6) == LOW){ // while the button is not pressed, we run the turn on function. I might try making these if statements, and seeing how it works. 74 turnOn(); 75 } 76 while(digitalRead(6) == HIGH){ // While the button is pressed, we run the turn off function 77 turnOff(); 78 } 79 redVal = 0; // At the end of the loop, we set each pixel value to zero. Otherwise we would have to write if statements for each switch being off as well. 80 greenVal = 0; 81 blueVal = 0; 82} 83void turnOn(){ // This function is called turn on, and will turn each pixel on after a short delay between it an the last pixel. 84 for(int i=0;i<NUMPIXELS;i++){ // for loop, which runs until we have met the number of pixels in the strand. 85 pixels.setPixelColor(i, pixels.Color(redVal,greenVal,blueVal)); // Set the pixel color to whatever value is defined at each 86 pixels.show(); // Show that color 87 delay(delayval); // wait a bit before going to the next pixel 88 } 89} 90void turnOff(){ // This is a function called turnOff, which turns off the LEDs one at a time, giving it a more interesting effect. 91 for(int i=0; i<NUMPIXELS; i++){ // For loop, which runs for number of pixels in the strand 92 pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Sets the pixel color in question to black, or off 93 pixels.show(); // Shows that pixel 94 delay(delayval); // Waits a certain amount of time before running this loop on the next pixel 95 } 96} 97 98
Downloadable files
circuit diagram
circuit diagram
circuit diagram
circuit diagram
Comments
Only logged in users can leave comments