11. Output devices¶
I was unable to finish the board I made for week 6, so I had to remake it. Working slightly differently than before, this time, instead of LED only being active whenever the button is held, the button was connected to a completely seperate pin, removing this requirement. This is actually unimportant to this weeks project however, as this code does not use the button at all.
# Programming in Arduino
to operate the neopixels using the Arduino IDE, the best step would be to use the Adafruit Neopixels Library. This would give me access to many functions that streamline the process of contolling the neopixel strip I was using
modifying existing code¶
the code I used to operate the Neopixels was based off the “simple” neopixel example. This example made more and more of the strip green as time went on. Besides changing the pin number to fit my board. I modified this code in a few ways. I changed the color to be random every iteration of the light travelling through the strip, along with a random speed and randomly deciding whether to travel from start to end or from end to start. The final result was this.
Code Example¶
int directionCount ;
int *directions;
void setup() {
pinMode(LEDPIN, OUTPUT);
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
// Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
// END of Trinket-specific code.
digitalWrite(LEDPIN, HIGH);
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
directionCount = 2;
directions = new int[directionCount];
}
int dir, dv, color_rand1, color_rand2, color_rand3 ;
void loop() {
auto randomColor = pixels.Color(random(0,255),random(0,255),random(0,255));
int delayVal = random(10, 100);
int dir =random(0,2) ;
spanStrip(randomColor,delayVal, dir);
}
void spanStrip(uint16_t color, int delayVal, int direction ){
int startVal, endVal, i_step;
if (direction == 1){
startVal = 0;
endVal = NUMPIXELS;
i_step = 1;
}
if (direction == 0){
startVal = NUMPIXELS;
endVal = 0;
i_step = -1;
}
for(int i=startVal; i != endVal; i+= i_step) { // For each pixel...
pixels.clear(); // Set all pixel colors to 'off'
pixels.setPixelColor(i, color);
pixels.show(); // Send the updated pixel colors to the hardware.
delay(delayVal); // Pause before next pass through loop
}
}