Yes, of course. The idea of my code is to control remotely the waterpump by means the Arduino IoT Cloud. However I noticed that sometimes can happen (but I don't understand why) the Arduino board resets itself: so it turns off and on in few seconds. However, everytime it happens it performs the setup() function in my code and thus also the line (the full code is attached down):
Carrier.Begin();
Inside this function there is the initialisation of all the sensors and objects, including also the Relays. I attach here the class: Arduino_MKRIoTCarrier/Arduino_MKRIoTCarrier.cpp at master · arduino-libraries/Arduino_MKRIoTCarrier · GitHub. In particular you can see at lines 74 and 75 that there is:
Relay1.Begin();
Relay2.Begin();
and going in the library: Arduino_MKRIoTCarrier/Arduino_MKRIoTCarrier_Relay.cpp at master · arduino-libraries/Arduino_MKRIoTCarrier · GitHub
there is:
void MKRIoTCarrier_Relay::begin(){
pinMode(_pin ,OUTPUT);
close();
}
Ok. That's my problem. Every time the Arduino resets it self (for some obscure reason) the Relay pin is put on OUTPUT (= 0x1, I guess it means ON) and the waterpump gives water for that few second of reset time. And it's a problem for a smart garden, although are only few seconds the water pump gives water without my control.
So my idea was to change the value of that OUTPUT or the MKRIoTCarrier_Relay::begin() function, maybe with:
void MKRIoTCarrier_Relay::begin(){
pinMode(_pin ,LOW);
close();
}
Anyway, I attach here also my code:
#include <Arduino_MKRIoTCarrier.h>
#include "thingProperties.h"
MKRIoTCarrier carrier;
String waterPumpState;
String lightState;
int moistPin = A5;
int wateringTimeInSeconds=5000;
int t = 0;
int dry = 1023;
int wet = 0;
uint32_t lightsOn = carrier.leds.Color(82, 118, 115);
uint32_t lightsOff = carrier.leds.Color(0, 0, 0);
//_______
void setup() {
// // Initialize serial and wait for port to open:
Serial.begin(9600);
//while (!Serial);
// Defined in thingProperties.h
initProperties();
//Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
//Wait to get cloud connection to init the carrier
while (ArduinoCloud.connected() != 1) {
ArduinoCloud.update();
delay(500);
}
CARRIER_CASE = true;
carrier.begin();
carrier.display.setRotation(0);
delay(500);
carrier.Relay1.close();
waterPumpState = "PUMP: OFF";
updateScreen();
delay(1500);
}
//_______
void loop() {
ArduinoCloud.update();
//Update touch buttons
carrier.Buttons.update();
//function to print out values
if (carrier.Buttons.onTouchDown(TOUCH0)) {
printInfo();
}
if (carrier.Buttons.onTouchDown(TOUCH1)) {
printInfoRelays();
}
//read temperature and humidity
temperature = carrier.Env.readTemperature();
humidity = carrier.Env.readHumidity();
//read raw moisture value
int raw_moisture = analogRead(moistPin);
//map raw moisture to a scale of 0 - 100
moisture = map(raw_moisture, wet, dry, 100, 0);
//read ambient light
while (!carrier.Light.colorAvailable()) {
delay(5);
}
int none;
carrier.Light.readColor(none, none, none, light);
if (waterpump == false){
t = 0;
carrier.Relay1.close();
}
if (waterpump == true) {
carrier.Relay1.open();
if (t == 0){
waterPumpState = "PUMP: ON";
updateScreen();
}
t = t + 1000;
}
if (t >= wateringTimeInSeconds){
waterpump = false;
waterPumpState = "PUMP: OFF";
updateScreen();
}
delay(1000);
}
//_______
void onArtificialLightChange() {
if (light_state == true) {
carrier.leds.fill(lightsOn, 0, 5);
carrier.leds.show();
lightState = "LIGHTS: ON";
} else {
carrier.leds.fill(lightsOff, 0, 5);
carrier.leds.show();
lightState = "LIGHTS: OFF";
}
updateScreen();
}
//Update displayed Info
void printInfo(){
carrier.display.fillScreen(ST77XX_BLACK);
carrier.display.setTextColor(ST77XX_WHITE);
carrier.display.setTextSize(2);
carrier.display.setCursor(40, 60);
carrier.display.print("Water: ");
carrier.display.print(moisture);
carrier.display.println("%");
carrier.display.setCursor(40, 80);
carrier.display.print("T: ");
carrier.display.print(temperature);
carrier.display.println("C");
carrier.display.setCursor(40, 100);
carrier.display.print("Hum: ");
carrier.display.print(humidity);
carrier.display.println("%");
}
//_______
void printInfoRelays(){
carrier.display.fillScreen(ST77XX_BLACK);
carrier.display.setTextColor(ST77XX_WHITE);
carrier.display.setTextSize(3);
carrier.display.setCursor(40, 50);
carrier.display.print(waterPumpState);
carrier.display.setCursor(40, 90);
carrier.display.print(lightState);
}
Thanks again and sorry for my inexperience
Lorenzo