Tilt Switch RGB Strip Setup
by NateCreates01 in Circuits > Arduino
233 Views, 0 Favorites, 0 Comments
Tilt Switch RGB Strip Setup
Supplies
What do you need?
- 1x Microcontroller
- 1x USB cable
- 4x Jumper Wires
- 5x Female to Male jumper wires
- 1x 100 - 1000 Capacitor
- 1x 470 ohms resistor
- 1x Tilt ball switch
- 1x Addressable RGB led strip
Setting Up Your Components
Follow the layout in the sketch above to assemble your components.
Please keep your microcontroller disconnected from the power source while putting your components together.
Code
#include "FastLED.h"
int sensorPin = 4;
int sensorValue;
int lastTiltState = HIGH;
// the previous reading from the tilt sensor
// RainbowLeds
// Animated, infinate Rainbows.
// Nathaniel Chavez
// Borrowed from Mark Kriegsman
#if FASTLED_VERSION < 3001000
#error "Requires FastLED 3.1 or later; check github for latest code."
#endif
#define DATA_PIN 13
//#define CLK_PIN 4
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS 144
#define BRIGHTNESS 255
CRGB leds[NUM_LEDS];
void setup() {
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
delay(3000); // 3 second delay for recovery
// tell FastLED about the LED strip configuration
FastLED.addLeds(leds, NUM_LEDS)
.setCorrection(TypicalLEDStrip)
.setDither(BRIGHTNESS < 255);
// set master brightness control
FastLED.setBrightness(BRIGHTNESS); }
void loop(){
sensorValue = digitalRead(sensorPin);
// If the switch changed, due to noise or pressing:
{ pride();
}
FastLED.show();
}
// This function draws rainbows with an ever-changing,
// widely-varying set of parameters.
void pride()
{ static uint16_t sPseudotime = 0;
static uint16_t sLastMillis = 0;
static uint16_t sHue16 = 0;
uint8_t sat8 = beatsin88( 87, 220, 250);
uint8_t brightdepth = beatsin88( 341, 96, 224);
uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
uint8_t msmultiplier = beatsin88(147, 23, 60);
uint16_t hue16 = sHue16;//gHue * 256; uint16_t hueinc16 = beatsin88(113, 1, 3000);
uint16_t ms = millis();
uint16_t deltams = ms - sLastMillis ;
sLastMillis = ms;
sPseudotime += deltams * msmultiplier;
sHue16 += deltams * beatsin88( 400, 5,9);
uint16_t brightnesstheta16 = sPseudotime;
for( uint16_t i = 0 ; i < NUM_LEDS; i++) {
hue16 += hueinc16;
uint8_t hue8 = hue16 / 256;
brightnesstheta16 += brightnessthetainc16;
uint16_t b16 = sin16( brightnesstheta16 ) + 32768;
uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
bri8 += (255 - brightdepth);
CRGB newcolor = CHSV( hue8, sat8, bri8);
uint16_t pixelnumber = i;
pixelnumber = (NUM_LEDS-1) - pixelnumber;
nblend( leds[pixelnumber], newcolor, 64);
}
}
Tilt Switch Check
If you connected all of your components correctly, your RGB strip should now switch on and off when it is tilted.
Quick Switch Check Video
Above is a short video that will demonstrate how your tilt ball switch and RGB strip should be working together. If your setup is not working as in the example in the video, please go back up the page and re-wire your set up and check the code again.