Eh good question, I'm not very familiar with VVVV either for what I was asking here.
The only thing I know is that it passes values as a string.
Anyway, back to the code, I'll ask you another question.
As you can see from the code, I have inserted a blocking function so that when the button is pressed, it changes function.
However this works for handleSoil and handleTemperature but not for handleLight.
In fact, as soon as the handleLight function starts, even if I press the button, nothing changes.
The same happens when the handleTemp or handleSoil function starts.
I can switch the latter two with the button only at the beginning, while handleLight even pressing the button never stops even at the beginning.
#include <Adafruit_NeoPixel.h>
#include <RTClib.h>
#include <Wire.h>
#include <DHT.h>
#include <DHT_U.h>
#include <easyRun.h>
int sogliaLuce = 700;
int sogliaUmi = 600;
int sogliaTemp = 23;
// includere possibilità cambiare soglia vvvv
#include <LiquidCrystal_I2C.h>
DHT dht(11, DHT11);
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte modePin = 3;
button modeButton(modePin);
const uint32_t autoChangePeriod = 5000ul; // ogni 10s
enum : byte {LIGHT, SOIL, TEMPERATURE} mode = LIGHT;
#define LDR_PIN A0
#define USOIL A2
#define PIN 2 // input pin Neopixel
#define NUMPIXELS 24// numero di pixel nell'annelo led
#define secondsSinceMidnight(h, m,s) (3600ul * (h) + 60ul * (m) + (s))
int light = 0;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
uint32_t dayStart = secondsSinceMidnight(6, 0, 0); // 6:00:00 am
uint32_t dayEnd = secondsSinceMidnight(22, 0, 0); // 10:00:00 pm
void dieHere(const char * errorMessage) {
Serial.println(__FUNCTION__);
Serial.println(errorMessage);
while (true) yield();
}
void selectMode() {
Serial.println(__FUNCTION__);
static unsigned long lastChange = 0;
easyRun();
if (modeButton || (millis() - lastChange >= autoChangePeriod)) {
// passare ad un altra funzione
for (int i = 0; i < NUMPIXELS; i ++) pixels.setPixelColor (i, pixels.Color (0, 0, 0)); // turn LEDs off
pixels.show ();
lastChange = millis ();
switch (mode) {
case LIGHT: mode = SOIL; break;
case SOIL: mode = TEMPERATURE; break;
case TEMPERATURE: mode = LIGHT; break;
}
}
}
void handleLight() {
Serial.println(__FUNCTION__);
light = analogRead(LDR_PIN);
Serial.println(light);
pixels.begin();
lcd.init();
lcd.backlight();
if (Serial.available()) // comunicazione seriale in input
{
int value = Serial.parseInt(); // trasforma ASCII in INT
// Zero means timeout so ignore that value
if (value > 0 && value <= 1024)
sogliaLuce = value;
}
if (sogliaLuce < 500) {
for (int i = 0; i < NUMPIXELS; i++)
pixels.setPixelColor(i, pixels.Color(0, 255, 0));
pixels.show();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Luce OK");
delay (100);
} else {
for (int i = 0; i < NUMPIXELS; i++)
pixels.setPixelColor(i, pixels.Color(255, 0, 0));
pixels.show();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Luce no OK");
delay (100);
}
}
void handleSoil() {
int u = analogRead(USOIL); //sensore umidità
Serial.println(__FUNCTION__);
Serial.println(u);
if (Serial.available()) // comunicazione seriale in input
{
int value = Serial.parseInt(); // trasforma ASCII in INT
// Zero means timeout so ignore that value
if (value > 0 && value <= 1024)
sogliaUmi = value;
}
if (sogliaUmi <= 600) { //condizione per far partire neopixel con umidità suolo
for (int i = 0; i < NUMPIXELS; i++)
pixels.setPixelColor(i, pixels.Color(51, 153, 255));
pixels.show();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Acqua OK");
delay(100);
} else {
for (int i = 0; i < NUMPIXELS; i++)
pixels.setPixelColor(i, pixels.Color(255, 0, 0));
pixels.show();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Acqua no OK");
delay (100);
}
}
void handleTemperature() {
dht.begin();
int t = dht.readTemperature();
if (Serial.available()) // comunicazione seriale in input
{
int value = Serial.parseInt(); // trasforma ASCII in INT
// Zero means timeout so ignore that value
if (value > 0 && value <= 1024)
sogliaTemp = value;
}
Serial.println(__FUNCTION__);
Serial.println(t);
pixels.begin();
if (sogliaTemp >= 23 ) {
for (int i = 0; i < NUMPIXELS; i++)
pixels.setPixelColor(i, pixels.Color(153, 51, 255)); // violet
pixels.show();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp OK");
delay (100);
} else {
for (int i = 0; i < NUMPIXELS; i++)
pixels.setPixelColor(i, pixels.Color(255, 0, 0));
pixels.show();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp no OK");
delay (100);
}
}
void setup() {
Serial.begin(115200); Serial.println();
Serial.println(__FUNCTION__);
if (! rtc.begin()) dieHere("Couldn't find RTC");
if (! rtc.isrunning()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // careful will be the time when you compiled the sketch
}
void loop () {
Serial.println(__FUNCTION__);
selectMode ();
DateTime now = rtc.now ();
uint32_t sNow = secondsSinceMidnight (now.hour (), now.minute (), now.second ());
if ((sNow >= dayStart) && (sNow <= dayEnd)) {
switch (mode) {
case LIGHT: handleLight (); break;
case SOIL : handleSoil (); break;
case TEMPERATURE: handleTemperature (); break;
}
}
}