HI,
I am using an Arduino NANO33 IoT to make an automatic drinking fountain to water my cat.
The sketch is as follows:
#include "arduino_secrets.h"
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/80dfaa6f-73a8-4e8f-99ee-34f884e398ea
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
int erogazioni;
bool acqua;
bool motore;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
unsigned long previusMillis = 0;
const long interval = 30000; // Seconds of water dispensing
int SharpPin = A1; // pin connected to Sharp sensor
int acquaPin = 2; // water flow sensor pin
bool motorePin = 3; // pin motor
int valore = 0; // Sharp sensor analog variable
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
/*========== Azzera le erogazioni =================*/
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, Azzera);
pinMode(SharpPin, INPUT);
pinMode(acquaPin, INPUT_PULLUP);
pinMode(motorePin, OUTPUT);
digitalWrite(motorePin, HIGH); // motor stop
previusMillis = millis();
}
void Azzera(){
erogazioni = 0; // azzera contatore erogazioni acqua
Serial.println("Azzerato contatore erogazioni");
Serial.print(" ");
}
void loop() {
ArduinoCloud.update();
valore = analogRead(SharpPin);
//Serial.print("valore ");
//Serial.print(valore);
//Serial.println(" ");
if(valore >= 900){ // spresence detector ensore
if(millis() - previusMillis >= interval){
previusMillis = millis();
digitalWrite(motorePin, LOW); // active motor relay = LOW
}
if(acquaPin == LOW){ // water flow in the circuit
erogazioni++;
}
else{
digitalWrite(motorePin, HIGH); // motor stop
}
}
Serial.print("Val ");
Serial.print(valore);
Serial.print(" Erog ");
Serial.print(erogazioni);
Serial.print(" Motor ");
Serial.print(motorePin);
Serial.print(" Acqua ");
Serial.println(acqua);
}
/*
Since Erogazioni is READ_WRITE variable, onErogazioniChange() is
executed every time a new value is received from IoT Cloud.
*/
void onErogazioniChange() {
// Add your code here to act upon Erogazioni change
}
/*
Since Motore is READ_WRITE variable, onMotoreChange() is
executed every time a new value is received from IoT Cloud.
*/
void onMotoreChange() {
// Add your code here to act upon Motore change
}
/*
Since Acqua is READ_WRITE variable, onAcquaChange() is
executed every time a new value is received from IoT Cloud.
*/
void onAcquaChange() {
// Add your code here to act upon Acqua change
/*Serial.print(valore);
Serial.print("-");
Serial.print(erogazioni);
Serial.print("-");
Serial.println(motorePin);*/
}
Through the monitor I can see the variables in use, but it only changes the value of the variable "value" on pin A1 for me.
When the Sharp sensor reading exceeds 900 I do not see the variable "motorPin" change value on pin D3, where I have a led to simulate the motor.
Nor does the variable "waterPin" if I simulate water flowing by holding down a button on pin D2.
As a result, the variable "dispensing" does not increment.
I don't know where I am going wrong.