#include "FastLED.h"
#include <TMP36.h>
#define NUM_LEDS 8*5 + 16*3
int green = 11;
int red = 10;
int blue = 9;
int brightness = 0;
CRGB leds[8 * 5 + 16 * 3];
TMP36 tempSensor1(A0, 5.0);
TMP36 tempSensor2(A4, 5.0);
int switch1Val = 0;
int switch1Pre = 0;
int switch1Pin = 1;
int touchPin = 2;
int touchVal = 0;
int touchPre = 0;
int button1Pin = 4;
int fifo_index = 0;
const int fifo_max = 5;
float fifo[fifo_max] = {0, 0, 0, 0, 0};
float fifo_avg = 0;
int fifo_button[5] = {0, 0, 0, 0, 0};
int fifo_button_index = 0;
int fifo_button_max = 5;
int fifo_button_sum = 0;
int a0 = 0;
int state = 0;
int nState = 10;
bool isOn = false;
int start_counter = 0;
int state_0_counter = 0;
int state_1_counter = 0;
int state_2_counter = 0;
float temp1 = 0;
float temp2 = 0;
int ref0, ref1;
void setup() {
FastLED.addLeds<NEOPIXEL, 6>(leds, NUM_LEDS);
FastLED.setBrightness(255);
pinMode(blue, OUTPUT);
pinMode(button1Pin, INPUT);
Serial.begin(9600);
for (int i = 0; i < 5; i++) {
fifo[i] = 0;
}
for (int i = 0; i < 5; i++) {
fifo_button[i] = 0;
}
delay(2);
}
uint8_t brightness_red = 0;
uint8_t brightness_blue = 0;
uint8_t brightness_green = 0;
int threshold = 5;
bool led_on = false;
bool triggered = false;
bool button_read;
bool button_read_pre;
int count = 0;
int brightness_change = 0;
int brightness_change_amount = 20;
int seq_index = 0;
bool blink_on = false;
bool gotoNextState = false;
uint8_t hue;
void loop() {
count = (count + 1) % 2;
if ( count == 0) {
led_on = !led_on;
}
brightness += brightness_change;
if (brightness > 244) {
brightness = 244;
brightness_change = -brightness_change_amount;
} else if (brightness < 1) {
brightness = 1;
brightness_change = brightness_change_amount;
}
delay(2);
temp1 = tempSensor1.getTempF();
isOn = digitalRead(touchPin);
hue = map(temp1, 60., 110., 160, 255);
CRGB rgb;
hsv2rgb_spectrum( CHSV(hue, 255, brightness), rgb);
button_read = digitalRead(4);
if (button_read == false && button_read_pre == true) {
triggered = !triggered;
} button_read_pre = button_read;
if (state == 0) {
clear_all_neopixel();
FastLED.show();
analogWrite(red, rgb.r);
analogWrite(green, rgb.g);
analogWrite(blue, rgb.b);
if (isOn && triggered) {
state = 1;
}
}
if (state == 1) {
analogWrite(red, 0);
analogWrite(green, 0);
analogWrite(blue, 255);
draw_sequence_neopixel(CRGB::Blue);
FastLED.setBrightness(brightness);
FastLED.show();
if (isOn) {
state = 2;
} else {
state = 0;
}
}
else if (state == 2) {
analogWrite(red, rgb.r);
analogWrite(green, rgb.g);
analogWrite(blue, rgb.b);
set_all_neopixel(CHSV(hue, 255, brightness));
FastLED.show();
if (isOn && triggered) {
state = 3;
seq_index = 0;
} else if (!isOn) {
state = 0;
}
}
else if (state == 3) {
analogWrite(red, 0);
analogWrite(green, 0);
analogWrite(blue, 255);
FastLED.setBrightness(240);
leds[seq_index] = CRGB::Blue;
seq_index++;
if (seq_index >= (NUM_LEDS)) {
seq_index = 0;
gotoNextState = true;
}
FastLED.show();
if (isOn && gotoNextState) {
state = 4;
gotoNextState = false;
seq_index = 0;
} else if (isOn && triggered ) {
state = 6;
seq_index = 0;
gotoNextState = false;
} else if (!isOn) {
state = 0;
}
}
else if (state == 4) {
analogWrite(red, 255);
analogWrite(green, 0);
analogWrite(blue, 255);
FastLED.setBrightness(240);
leds[seq_index] = CRGB::Purple;
seq_index++;
if (seq_index >= (NUM_LEDS)) {
seq_index = 0;
gotoNextState = true;
}
FastLED.show();
if (isOn && gotoNextState) {
state = 5;
gotoNextState = false;
seq_index = 0;
} else if (isOn && triggered ) {
state = 6;
seq_index = 0;
gotoNextState = false;
} else if (!isOn) {
state = 0;
}
}
else if (state == 5) {
analogWrite(red, 255);
analogWrite(green, 0);
analogWrite(blue, 0);
FastLED.setBrightness(240);
leds[seq_index] = CRGB::Red;
seq_index++;
if (seq_index >= (NUM_LEDS)) {
seq_index = 0;
gotoNextState = true;
}
FastLED.show();
if (isOn && (triggered || gotoNextState)) {
state = 2;
seq_index = 0;
gotoNextState = false;
} else if (isOn && triggered ) {
state = 6;
seq_index = 0;
FastLED.setBrightness(240);
gotoNextState = false;
} else if (!isOn) {
state = 0;
}
}
else if (state == 6) {
if (seq_index < (NUM_LEDS)) {
analogWrite(red, 200);
analogWrite(green, 200);
analogWrite(blue, 200);
leds[seq_index] = CHSV(0,0,200);
seq_index++;
} else {
if (led_on) {
FastLED.setBrightness(200);
analogWrite(red, 200);
analogWrite(green, 200);
analogWrite(blue, 200);
} else {
FastLED.setBrightness(10);
analogWrite(red, 10);
analogWrite(green, 10);
analogWrite(blue, 10);
}
}
FastLED.show();
delay(100);
if (isOn && triggered) {
state = 2;
gotoNextState = false;
} else if (!isOn) {
state = 0;
}
}
triggered = false;
delay(40);
serial_print();
}
void checkOn() {
isOn = digitalRead(touchPin);
}
void serial_print() {
Serial.print("temp1: ");
Serial.print(temp1);
Serial.print("temp map: "); Serial.println(hue);
}
void draw_start_neopixel(CRGB color) {
draw_sequence_neopixel(color);
if (!isOn) return;
draw_blink_neopixel(color);
if (!isOn) return;
draw_blink_neopixel(color);
if (!isOn) return;
draw_blink_neopixel(color);
}
void draw_sequence_neopixel(CRGB color) {
FastLED.setBrightness(255);
for (int i = 0; i < (8 * 5 + 16 * 3); i++) {
isOn = digitalRead(touchPin);
if (!isOn) return;
leds[i] = color;
FastLED.show();
delay(25);
}
}
void draw_blink_neopixel(CRGB color) {
FastLED.setBrightness(255);
set_all_neopixel(CRGB::Black);
FastLED.show();
checkOn();
if (!isOn) return;
delay(200);
FastLED.setBrightness(0);
FastLED.show();
checkOn();
if (!isOn) return;
delay(400);
}
void clear_all_neopixel() {
clear_led8_neopixel();
clear_led16_neopixel();
}
void clear_led8_neopixel() {
for (int i = 0; i < (8 * 5); i++) {
leds[i] = CRGB::Black;
}
}
void clear_led16_neopixel() {
for (int i = 8 * 5; i < (8 * 5 + 16 * 3); i++) {
leds[i] = CRGB::Black;
}
}
void set_all_neopixel(CRGB c) {
set_led8_neopixel(c);
set_led16_neopixel(c);
}
void set_led8_neopixel(CRGB c) {
for (int i = 0; i < (8 * 5); i++) {
leds[i] = c;
}
}
void set_led16_neopixel(CRGB c) {
for (int i = 8 * 5; i < (8 * 5 + 16 * 3); i++) {
leds[i] = c;
}
}
Comments
Post a Comment