// https://wokwi.com/projects/369196030361510913
// https://forum.arduino.cc/t/turn-on-peristaltic-pump-based-on-ph-reading/1143658
# include <Adafruit_NeoPixel.h>
# define PIN A3 // the pin
# define NPIXELS 25
Adafruit_NeoPixel temperatureBar(NPIXELS, PIN, NEO_GRB + NEO_KHZ800);
const byte coolingControl = 6;
const byte heatingControl = 7;
unsigned long now;
# define RATE 20 // every N millisocends
# define LOW_COOL 55 // turn off cooling
# define HIGH_COOL 73 // turn on cooling
# define LOW_HEAT 47 // turn on heating
# define HIGH_HEAT 65 // turn off heating
int temperature;
void setup()
{
Serial.begin(115200);
Serial.println("\ntoo hot, too cold...\n");
temperatureBar.begin();
pinMode(coolingControl, OUTPUT);
pinMode(heatingControl, OUTPUT);
}
void loop()
{
static unsigned long lastTime;
now = millis();
if (now - lastTime < RATE) return;
lastTime = now;
temperature = map(analogRead(A0), 0, 1023, 20, 100);
static float averageTemp;
// averageTemp = 0.9 * averageTemp + 0.1 * temperature;
averageTemp = 0.7 * averageTemp + 0.3 * temperature;
temperature = averageTemp;
drawTemperatureBar();
if (temperature > HIGH_COOL) digitalWrite(coolingControl, HIGH);
if (temperature < LOW_COOL) digitalWrite(coolingControl, LOW);
if (temperature < LOW_HEAT) digitalWrite(heatingControl, HIGH);
if (temperature > HIGH_HEAT) digitalWrite(heatingControl, LOW);
if (digitalRead(heatingControl) && digitalRead(coolingControl)) {
Serial.println("heating and cooling logic error"); Serial.flush();
while (1);
}
}
void drawTemperatureBar()
{
temperatureBar.clear();
for (int tt = 20; tt <= 100; tt++) {
unsigned long color = 0;
if (tt >= LOW_COOL && tt <= HIGH_COOL) color |= 0x000080;
if (tt >= LOW_HEAT && tt <= HIGH_HEAT) color |= 0x800000;
temperatureBar.setPixelColor(map(tt, 20, 100, 0, NPIXELS), color | 0x101010);
}
temperatureBar.setPixelColor(map(temperature, 20, 100, 0, NPIXELS), 0xffffff);
temperatureBar.show();
}