Problem with pin numbering of the NANO33 IoT

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.

how could you get a pin number being LOW ?

acquaPin is 2.

you are missing a digitalRead()

I saw the mistake I made, I will correct it right away.
Thank you for your help.

EzioGi

I made the correction as suggested, but now I still have the problem even though I corrected the timing with millis(),
which is now like this:

if(millis() - previusMillis <= interval){

Pin3 (Motor) always remains HIGH.
Also if water flows the variable "dispensing" only increments by 1 but several times.

EzioGi

can you post the new code ?

This is new code:

  #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 = 10000; // 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(digitalRead(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);*/
  }
  

it probably does not compile. seems you have some code outside functions

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.