Skip to content

Instantly share code, notes, and snippets.

@smanning29
Created September 24, 2019 16:55
Show Gist options
  • Save smanning29/8110f6fdc0a4e9665f931447d7155a3a to your computer and use it in GitHub Desktop.
Save smanning29/8110f6fdc0a4e9665f931447d7155a3a to your computer and use it in GitHub Desktop.
Arduino codes for Object Lab 2
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define LED_PIN 7
// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 5
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
int buttonPin = 9;
int switchPin = 8;
void setup() {
//define digital pins as inputs or outputs
pinMode(buttonPin, INPUT);
pinMode(switchPin, INPUT);
Serial.begin(9600);
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
}
void loop() {
// Find states of button and/or switch
int stateButton = digitalRead(buttonPin); //read the state of the button
int stateSwitch = digitalRead(switchPin);
if(stateButton == 1) { //if is pressed
rainbow(5);
}
else if(stateSwitch == 1) { //if is switched on
colorWipe(strip.Color(255, 0, 0), 50); // Red
colorWipe(strip.Color( 0, 255, 0), 50); // Green
colorWipe(strip.Color( 0, 0, 255), 50); // Blue
}
else { //if not switched
colorWipe(strip.Color( 0, 0, 0), 0); // Off
}
}
//LED Strip functions from Adafruit library strandtest
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow(int wait) {
// Hue of first pixel runs 5 complete loops through the color wheel.
// Color wheel has a range of 65536 but it's OK if we roll over, so
// just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
// means we'll make 5*65536/256 = 1280 passes through this outer loop:
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
// Offset pixel hue by an amount to make one full revolution of the
// color wheel (range of 65536) along the length of the strip
// (strip.numPixels() steps):
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
// strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
// optionally add saturation and value (brightness) (each 0 to 255).
// Here we're using just the single-argument hue variant. The result
// is passed through strip.gamma32() to provide 'truer' colors
// before assigning to each pixel:
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
}
strip.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
void theaterChase(uint32_t color, int wait) {
for(int a=0; a<10; a++) { // Repeat 10 times...
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
strip.clear(); // Set all pixels in RAM to 0 (off)
// 'c' counts up from 'b' to end of strip in steps of 3...
for(int c=b; c<strip.numPixels(); c += 3) {
strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
}
strip.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
}
int LEDPin = 7;
int buttonPin = 8;
void setup() {
// put your setup code here, to run once:
pinMode(LEDPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
//check state of button
int stateButton = digitalRead(buttonPin);
// if button pressed, turn LED on
if(stateButton == 1){
digitalWrite(LEDPin, HIGH);
}
//otherwise LED off
else{
digitalWrite(LEDPin, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment