#include <Wire.h>
//#include <HX710B.h>
#include <HX711.h>
//#define SCK_PIN 3
//#define SDI_PIN 4
//HX710B air_press(SCK_PIN, SDI_PIN);
HX711 scale;
uint8_t dataPin = 6;
uint8_t clockPin = 7;
#include <Adafruit_NeoPixel.h>
#include <RunningMedian.h>
//HX710B sensor smoothing
RunningMedian pressure = RunningMedian(4); // Higher the number, the more filtering it will have
#define PIN A0 // // WS2812 chip - Data In
#define Pixel_Number 16
Adafruit_NeoPixel strip = Adafruit_NeoPixel(Pixel_Number, PIN, NEO_GRB + NEO_KHZ800);
void updateStrip(int ledsToLight, uint32_t colorOn, uint32_t colorOff){
for(int x = 0; x < Pixel_Number; x++){
if(x < ledsToLight) {
strip.setPixelColor(x, colorOn);
} else {
strip.setPixelColor(x, colorOff);
}
}
strip.show();
}
struct Level {
int ledsToLight;
uint32_t color;
};
#define max_colors 3
Level colors[max_colors] = {
{3, strip.Color(255,0,0)},
{5, strip.Color(255,128,0)},
{16, strip.Color(0,255,0)},
};
uint32_t getColor(int ledsToLight){
uint32_t ret = strip.Color(0,0,255);
for(int x = 0; x < max_colors; x++){
if (ledsToLight <= colors[x].ledsToLight){
ret = colors[x].color;
break;
}
}
return ret;
}
void setup()
{
strip.begin();
Wire.begin();
Serial.begin(9600);
if ( !air_press.init() )
{
Serial.println(F("HX710B not Found !"));
while(1);
}
}
uint32_t time_update = 0;
void loop()
{
uint32_t rollOver = millis();
if( rollOver < time_update )
time_update = rollOver;
if( millis() - time_update >= 2000UL )
{
uint32_t data_raw = 0;
if ( air_press.read(&data_raw, 1000UL) != HX711_OK )
Serial.println(F("something error !"));
else
{
Serial.print(F("Data raw of ADC is : "));
Serial.println((unsigned long) data_raw);
}
time_update = millis();
}
int range = (digitalRead(4));
digitalRead (range);
range = pressure.getMedian();
range = range;
Serial.print("Pressure: ");
Serial.print(range);
Serial.print("HG");
int ledsToLight = 0;
int brpressure = ( range );
if(brpressure < 60 ) {ledsToLight = 16;}
else if(brpressure > 60 && brpressure < 72 ) {ledsToLight = 15;}
else if(brpressure > 72 && brpressure < 84 ) {ledsToLight = 14;}
else if(brpressure > 84 && brpressure < 96 ) {ledsToLight = 13;}
else if(brpressure > 96 && brpressure < 108 ) {ledsToLight = 12;}
else if(brpressure > 108 && brpressure < 120 ) {ledsToLight = 11;}
else if(brpressure > 120 && brpressure < 132 ) {ledsToLight = 10;}
else if(brpressure > 132 && brpressure < 144 ) {ledsToLight = 9;}
else if(brpressure > 144 && brpressure < 156 ) {ledsToLight = 8;}
else if(brpressure > 156 && brpressure < 168 ) {ledsToLight = 7;}
else if(brpressure > 168 && brpressure < 180 ) {ledsToLight = 6;}
else if(brpressure > 180 && brpressure < 192 ) {ledsToLight = 5;}
else if(brpressure > 192 && brpressure < 204 ) {ledsToLight = 4;}
else if(brpressure > 204 && brpressure < 216 ) {ledsToLight = 3;}
else if(brpressure > 216 && brpressure < 228 ) {ledsToLight = 2;}
else if(brpressure > 228 && brpressure < 240 ) {ledsToLight = 1;}
else if(brpressure > 240 ) {ledsToLight = 0;}
if (ledsToLight == 0) {
doFlash();
} else {
auto colorOff = strip.Color(0,0,0);
auto colorOn = getColor(ledsToLight);
updateStrip(ledsToLight, colorOn, colorOff);
}
}
void doFlash(){
auto color = strip.Color(0,0,0);
if((millis() / 540) & 1 != 0){
color = strip.Color(255,0,0);
}
for (int x = 0; x < Pixel_Number; x++){
strip.setPixelColor(x, color);
}
strip.show();
}