166 lines
3.9 KiB
C++
166 lines
3.9 KiB
C++
#include <Adafruit_NeoPixel.h>
|
|
#define LED_PIN 9 // orange
|
|
#define NUM_LEDS 7
|
|
#define CLBUTTON 10 // yellow
|
|
Adafruit_NeoPixel pixels(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
|
|
int colourchoice = 0;
|
|
int buttonState = 0; // current state of the button
|
|
int lastButtonState = 0; // previous state of the button
|
|
int startPressed = 0; // the moment the button was pressed
|
|
int endPressed = 0; // the moment the button was released
|
|
int holdTime = 0; // how long the button was hold
|
|
int idleTime = 0; // how long the button was idle
|
|
int currentcolour = 0;
|
|
int lastcolour = 0;
|
|
|
|
/////////// pot stuff
|
|
|
|
int analogValue;
|
|
int brightness;
|
|
|
|
// Define the number of samples to keep track of. The higher the number, the
|
|
// more the readings will be smoothed, but the slower the output will respond to
|
|
// the input. Using a constant rather than a normal variable lets us use this
|
|
// value to determine the size of the readings array.
|
|
const int numReadings = 10;
|
|
|
|
int readings[numReadings]; // the readings from the analog input
|
|
int readIndex = 0; // the index of the current reading
|
|
int total = 0; // the running total
|
|
int average = 0; // the average
|
|
|
|
void setup() {
|
|
// put your setup code here, to run once:
|
|
pinMode(CLBUTTON, INPUT_PULLUP);
|
|
pixels.begin();
|
|
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
|
|
readings[thisReading] = 0;
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
buttonState = digitalRead(CLBUTTON);
|
|
analogValue = analogRead(A0);
|
|
// subtract the last reading:
|
|
total = total - readings[readIndex];
|
|
// read from the sensor:
|
|
readings[readIndex] = analogValue;
|
|
// add the reading to the total:
|
|
total = total + readings[readIndex];
|
|
// advance to the next position in the array:
|
|
readIndex = readIndex + 1;
|
|
|
|
// if we're at the end of the array...
|
|
if (readIndex >= numReadings) {
|
|
// ...wrap around to the beginning:
|
|
readIndex = 0;
|
|
}
|
|
|
|
// calculate the average and set the brightness
|
|
average = total / numReadings;
|
|
brightness = map(analogValue, 0, 1023, 0, 255);
|
|
switch (colourchoice)
|
|
{
|
|
case 0:
|
|
setcolour(0,0,0);
|
|
break;
|
|
case 1:
|
|
setcolour(255,255,255);
|
|
break;
|
|
case 2:
|
|
setcolour(226,100,15);
|
|
break;
|
|
case 3:
|
|
fire();
|
|
break;
|
|
}
|
|
|
|
if (buttonState != lastButtonState)
|
|
{
|
|
updateState();
|
|
if(buttonState == LOW){
|
|
changecolour();
|
|
}
|
|
lastButtonState = buttonState;
|
|
delay(150);
|
|
}
|
|
else
|
|
{
|
|
updateCounter();
|
|
}
|
|
|
|
delay(50);
|
|
}
|
|
|
|
void fire()
|
|
{
|
|
int r = 226, g = 121, b = 35;
|
|
delay(150);
|
|
do
|
|
{
|
|
analogValue = analogRead(A0);
|
|
brightness = map(analogValue, 0, 1023, 0, 255);
|
|
for(int i=0; i<pixels.numPixels(); i++)
|
|
{
|
|
int flicker = random(0,55);
|
|
int r1 = r-flicker;
|
|
int g1 = g-flicker;
|
|
int b1 = b-flicker;
|
|
if(g1<0) g1=0;
|
|
if(r1<0) r1=0;
|
|
if(b1<0) b1=0;
|
|
pixels.setPixelColor(i,r1,g1,b1);
|
|
pixels.setBrightness(brightness);
|
|
pixels.show();
|
|
}
|
|
delay(random(10,113));
|
|
} while(digitalRead(CLBUTTON)==HIGH);
|
|
changecolour();
|
|
}
|
|
|
|
void changecolour()
|
|
{
|
|
if (colourchoice == 3)
|
|
{
|
|
colourchoice = 0;
|
|
}
|
|
else
|
|
{
|
|
colourchoice++;
|
|
}
|
|
}
|
|
|
|
void setcolour(int r, int g, int b)
|
|
{
|
|
for( int i = 0; i < NUM_LEDS; i++) {
|
|
pixels.setPixelColor(i, pixels.Color(r, g, b));
|
|
}
|
|
pixels.setBrightness(brightness);
|
|
pixels.show();
|
|
}
|
|
|
|
void updateState() {
|
|
// the button has been just pressed
|
|
if (buttonState == LOW) {
|
|
startPressed = millis();
|
|
idleTime = startPressed - endPressed;
|
|
|
|
// the button has been just released
|
|
} else {
|
|
endPressed = millis();
|
|
holdTime = endPressed - startPressed;
|
|
}
|
|
}
|
|
|
|
void updateCounter() {
|
|
// the button is still pressed
|
|
if (buttonState == LOW) {
|
|
holdTime = millis() - startPressed;
|
|
|
|
if (holdTime >= 3000) {
|
|
colourchoice = 0;
|
|
pixels.setBrightness(0);
|
|
pixels.show();
|
|
}
|
|
}
|
|
} |