// INCLUDES
// #include <Adafruit_NeoPixel.h>
// Interfacing to programmable LED strips, see https://fastled.io/
#include <FastLED.h>
// DEFINES
// water level pin
#define WATER_LEVEL_PIN A1
// input pin Neopixel is attached to
#define DATA_PIN 8
// number of digits
#define NUMDIGITS 3
// number of stripes in one digit
#define NUMSTRIPES_PRE_DIGIT 7
// NUMPIXELS PRE STRIPE
#define NUMPIXELS_PRE_STRIPE 10
// NUMPIXELS PRE DOT
#define NUMPIXELS_PRE_DOT 7
// How many LEDs are used in each digit
#define NUM_LEDS_PER_DIGIT NUMSTRIPES_PRE_DIGIT*NUMPIXELS_PRE_STRIPE
// total number of NUM_LEDS
#define NUM_LEDS NUMDIGITS*NUMSTRIPES_PRE_DIGIT*NUMPIXELS_PRE_STRIPE + NUMPIXELS_PRE_DOT
// GLOBALS
// The array of RGB values assigned to each LED in the strip
CRGB leds[NUM_LEDS];
// 0000000000
// 1111111111
// const uint32_t digits[10] = {
// 0b0000000000000000000000000000000000000000000000000000000000000000000000, // 0 ABCDEFG 0000000
// 0b0000000000111111111100000000000000000000000000000000000000001111111111, // 1 ABCDEFG 0100001
// 0b1111111111111111111111111111110000000000111111111111111111110000000000, // 2 ABCDEFG 1110110
// 0b1111111111111111111111111111110000000000000000000011111111111111111111, // 3 ABCDEFG 1110011
// 0b1111111111111111111100000000001111111111000000000000000000001111111111, // 4 ABCDEFG 1101001
// 0b1111111111000000000011111111111111111111000000000011111111111111111111, // 5 ABCDEFG 1011011
// 0b1111111111000000000011111111111111111111111111111111111111111111111111, // 6 ABCDEFG 1011111
// 0b0000000000111111111111111111110000000000000000000000000000001111111111, // 7 ABCDEFG 0110001
// 0b1111111111111111111111111111111111111111111111111111111111111111111111, // 8 ABCDEFG 1111111
// 0b1111111111111111111111111111111111111111000000000011111111111111111111, // 9 ABCDEFG 1111011
// };
const uint8_t digits[10] = {
0b1111110, // 0 ABCDEFG 0111111
0b1000010, // 1 ABCDEFG 0100001
0b0110111, // 2 ABCDEFG 1110110
0b1100111, // 3 ABCDEFG 1110011
0b1001011, // 4 ABCDEFG 1101001
0b1101101, // 5 ABCDEFG 1011011
0b1111101, // 6 ABCDEFG 1011111
0b1000110, // 7 ABCDEFG 0110001
0b1111111, // 8 ABCDEFG 1111111
0b1101111, // 9 ABCDEFG 1111011
};
// FUNCTIONS
// Set the values in the IED strip corresponding to a particular display/value
void setDigit(int display, int val, CHSV colour){
for(int i=0; i<NUM_LEDS_PER_DIGIT; i++){
colour.v = bitRead(digits[val], i) * 255;
if(display == 2){
leds[display*NUM_LEDS_PER_DIGIT + NUMPIXELS_PRE_DOT + i] = colour;
}else{
leds[display*NUM_LEDS_PER_DIGIT + i] = colour;
}
}
}
// FUNCTIONS
// Set the dot in the IED strip, always light.
void setDot(CHSV colour){
colour.v = 255;
for(int i=0; i<NUMPIXELS_PRE_DOT; i++){
leds[2*NUM_LEDS_PER_DIGIT + i] = colour;
}
}
// This function runs once when the program first starts
void setup () {
Serial.begin(115200);
Serial.println("Begin!");
pinMode(WATER_LEVEL_PIN, INPUT);
// Initialise the IED strip
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
}
float water_level = 0;
void loop() {
water_level = analogRead(WATER_LEVEL_PIN);
water_level = water_level / 1024.0 * 99.9;
Serial.println(water_level);
int num_0 = int(water_level * 10) % 1000 / 100;
int num_1 = int(water_level * 10) % 100 / 10;
int num_2 = int(water_level * 10) % 10;
// Serial.println(num_0 * 100 + num_1*10 + num_2);
// setDigit(0, num_0, CHSV(40, 255, 255));
// setDigit(1, num_1, CHSV(40, 255, 255));
// setDot(CHSV(40, 255, 255));
// setDigit(2, num_2, CHSV(40, 255, 255));
CHSV colour = CHSV(100, 255, 255);
// num_0
for(int i=0; i<NUMSTRIPES_PRE_DIGIT; i++){
colour.v = bitRead(digits[num_0], i) * 255;
for(int j=0; j<NUMPIXELS_PRE_STRIPE; j++){
leds[i*10 + j] = colour;
}
}
// num_1
for(int i=0; i<NUMSTRIPES_PRE_DIGIT; i++){
colour.v = bitRead(digits[num_1], i) * 255;
for(int j=0; j<NUMPIXELS_PRE_STRIPE; j++){
leds[NUM_LEDS_PER_DIGIT + i*10 + j] = colour;
}
}
// Dot
for(int i=0; i<NUMPIXELS_PRE_DOT; i++){
colour.v = 255;
leds[NUM_LEDS_PER_DIGIT * 2 + i] = colour;
}
// num_2
for(int i=0; i<NUMSTRIPES_PRE_DIGIT; i++){
colour.v = bitRead(digits[num_2], i) * 255;
for(int j=0; j<NUMPIXELS_PRE_STRIPE; j++){
leds[NUM_LEDS_PER_DIGIT*2 + NUMPIXELS_PRE_DOT+ i*10 + j] = colour;
}
}
FastLED.show();
delay(200);
}