Replaced the LED code with a switch/case block. Maybe easier to understand.
Does the same, apart from some brightness corrections.
Leo..
#include "HX710.h"
HX710 sensor;
#include <RunningMedian.h>
RunningMedian samples = RunningMedian(25);
#include "MultiMap.h"
int in[] = {950, 1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350, 1400, 1450, 1500, 1550, 1600, 1650, 1700, 1750}; // calibrate
int out[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; // 17 steps
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, 6, NEO_GRB + NEO_KHZ800); // nb_LEDs, pin
int rawValue, level, ledsToLight;
unsigned long prevMillis, color;
void setup() {
Serial.begin(115200);
sensor.initialize(3, 4); // SCK, SDI
strip.begin();
strip.show(); // clear
// strip.setBrightness(64);
}
void loop() {
if (millis() - prevMillis > 250 && sensor.isReady()) {
prevMillis = millis();
sensor.readAndSelectNextData(HX710_DIFFERENTIAL_INPUT_10HZ);
rawValue = sensor.getLastDifferentialInput() >> 10; // snip
samples.add(rawValue);
level = samples.getAverage();
ledsToLight = multiMap(level, in, out, 17);
Serial.print("Median: ");
Serial.print(level);
Serial.print("\tLEDs: ");
Serial.println(ledsToLight);
}
switch (ledsToLight) {
case 0: // flash
color = strip.Color(0, 0, 0);
if ((millis() / 500) & 1) color = strip.Color(63, 0, 0);
break;
case 1 ... 2: // red
color = strip.Color(255, 0, 0);
break;
case 3 ... 5: // yellow
color = strip.Color(170, 85, 0);
break;
case 6 ... 16: // green
color = strip.Color(0, 255, 0);
break;
}
strip.clear();
strip.fill(color, 0, ledsToLight);
strip.show();
}