Part My sketch is not updating a String Variable "status" as expected

At this point I do not have all my hardware, so i am just testing the limit switch inputs to pins 1 and 2 and the resulting output to the serial monitor.

Both Variables “Close_Limit” and “Open_Limit” work as expected, I can see the values change from 1 to 0 on the serial monitor as i jumper each pin to grd.

The evaluation section between lines 141 to 153 work as intended and the variable “lost” is updated 0 to 1 when i leave both pins floating, the “status” variable is also updated to UNKNOWN by the steps on line 135 to 137 as expected.

The steps from 131 to 133 also work as expected and the “status” variable is updated to CLOSED.

However, the steps from 127 to 129 do not update the “status” variable. it just stays reporting the last condition, either CLOSED or UNKNOWN. I have even tried replacing steps 127 to 129 with the steps 131 to 133 and changing the values to match the OPENED criteria. I have also tried changing the order of the two evaluations for OPENED and CLOSED.

I have checked and double checked , tried many different things such as changing the variable type etc. I am stuck. see screenshot of serial monitor. thank you in advance for taking the time to look at it.

/*******************************************************************************
 * DESCRIPTION:
 This sketch is written to control a DC Gearmotor that swings a Flat panel into position and back against the wall. 

 The DC Gearmotor is Controlled by a Cytron DC motor Controller which requires speed input via a pulse Width Modulated signal 
 on the PWM terminal and Direction via a binary input on the DIR terminal. These inputs to the motor controller will
 originate from the Seeed Studio XIAO SAMD21 Board. Note the I/O pins are only rated for 3.3V
 
 Final speed of the motor will be determined during commisionaing and control by this sketch using the MaxSpeed varible.(0-255)

 The commands requiredfor the motor controller were dirived from the Example scipt and library created by:
          AUTHOR   : Kong Wai Weng
          COMPANY  : Cytron Technologies Sdn Bhd
          WEBSITE  : www.cytron.io
          EMAIL    : support@cytron.io

  Control inputs will be recieved from NINA via an ASCOM serial driver named FlatManCoverDriver implementing ICoverCalibratorV1. (created using Chat GBT) 

  The NINA interface will control both this mechanism and the Flat Man flat panel by Altinak. NINA will have Configure COM ports and baud rates in a setup dialog,
   Test buttons for both subsystems.

  The ASCOM commands from NINA to Arduino will be OPEN, CLOSE and STATUS. When STATUS is requested this Arduino sketch will respond OPENED, CLOSED, or UNKNOWN
 
 * CONNECTIONS:
 
  Arduino D1  - Grd thru a limit switch to input for stopping in the COVER_ON direction
  Arduino D2  - Grd thru a limit switch to input for stopping in the COVER_OFF direction
  Arduino D3  - Motor Driver PWM Input
  Arduino D4  - Motor Driver DIR Input
  Arduino GND - Motor Driver GND
  5V pwr and Grd and serial communication via USB
 
************************************************************************************************************************************************/

 #include "CytronMotorDriver.h"


// Configure the motor driver.
CytronMD motor(PWM_DIR, 3, 4);  // PWM = Pin 3, DIR = Pin 4.

//This section sets up the varibles needed for serial communication
String serialin; //declares a string type variable called "serialin" to store incoming serial data from the serial line
String status; //declares a string type variable called "status" to store the cover position status


//This section sets up other global varibles
int Speed = 0; //creates a integer varible "Speed" and assigns the initial value of 0
int MaxSpeed = 128; //creates an interger varible "MaxSpeed" and assigns a value (0-255), adjust to get desired travel speed
unsigned long end_time;//  declares an unsigned, long type varible named 'end_time' a unsigned long varible is 32 bits (0 to 4,294,967,295) (intial Value to be assigned in a few lines)
bool lost = false; //declares a boolean varible called 'lost' and assigns the initial value as FALSE. This will be set to TRUE if the actuator is not reporting position after 'end time'
unsigned long ActuatorTravelTime = 2100; //change to suit your rquirments , set it to just longer than expected open or close time (in milliseconds) to determine if Actuator is lost depending on Actuator travel time.Stat

// The setup routine runs once when you press reset.
void setup() {

  // this section defines the input pins
 pinMode(1, INPUT_PULLUP);  // assigns pin 1 as INPUT with an internal pullup resistor
 pinMode(2, INPUT_PULLUP);  // assigns pin 2 as INPUT with an internal pullup resistor


 end_time=millis()+ActuatorTravelTime; //Actuator lost reset timer 'end_time' = current time +ActuatorTravelTime seconds

  //Begin Serial Comunication(configured for 57600baud)
  Serial1.begin(9600);
    while (!Serial) {  
     ;// Wait for serial port to connect. Required for native USB!
    }
  //Serial.flush();

  // Make sure the RX, TX, and built-in LEDs don't turn on, they are very bright!
  // Even though the board is inside an enclosure, the light can be seen shining through the small opening for the USB connector! Unfortunately, it is not possible to turn off the power LED (green) in code...
  pinMode(PIN_LED_TXL, INPUT);
  pinMode(PIN_LED_RXL, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);

}


// The loop routine runs over and over again forever.
void loop() {

 int Open_Limit =digitalRead(1); //creates an integer varible "Open_Limit" and reads pin 1 status into that varible (HIGH if limit switch is open(off), LOW if limit switch is closed(on))
 int Close_Limit =digitalRead(2); //creates an integer varible "Close_Limit" and reads pin 2 status into that varible (HIGH if limit switch is open(off), LOW if limit switch is closed(on))

 //=======================================================================================================

 //look for and act on commands on the serial interface  

 while (Serial.available()>0) {  //will loop through these commands until there is no data remaining in the serial recieve buffer
    
   serialin = Serial.readStringUntil('#'); //Read Serial data and store the results in the 'serialin' string variable until it recieves an '#' 
 
   if (serialin == "CLOSE") { //if the command 'CLOSE' is recieved
     // this section puts the COVER ON(Close) if the CLOSE command is recieved and the associated Limit Switch is open
     while (Close_Limit = HIGH) {
       Speed = MaxSpeed; //set speed varible to max speed
       motor.setSpeed(Speed);  // Run forward at Maxspeed
      // status = "MOVING"; //updates the status varible
      } //end of while loop
     Speed = 0; // sets speed varible back to 0
     motor.setSpeed(Speed);  // Stop motor when limit opens
     } //end of if 'CLOSE'

   if (serialin == "OPEN"){ //if the command 'OPEN' is recieved
   // this section takes the COVER Off(Open) if the OPEN command is recieved and the associated Limit Switch is open
      while (Open_Limit = HIGH) {
       Speed = 0 - MaxSpeed; //set speed varible to negative maxspeed
       motor.setSpeed(Speed);  // Run reverse at Maxspeed
      // status = "MOVING"; //updates the status varible
      } //end of while loop
     Speed = 0; // sets speed varible back to 0
     motor.setSpeed(Speed);  // Stop motor when limit opens
    } //end of if 'OPEN' 
    
   // Send 'Status' via the serial interface
   if (serialin == "STATUS"){  // if serial in equals 'STATUS'
      Serial.println (status); // send status varible on serial port
    } // end of if serialin== 'STATUS'

  serialin = "";  //clears the serialin 

  } //end of while loop

 // The next three sections deternine the value of the status varible
   
 if ((digitalRead(Close_Limit) == 1) && (digitalRead(Open_Limit) == 0) && (lost == false)){ //if 'closed' switch is OPEN and 'open' switch is CLOSED, and 'lost' is false
   status = "OPENED"; //updates the status varible
 } //end of if 'closed'

 if ((digitalRead(Close_Limit) == 0) && (digitalRead(Open_Limit) == 1) && (lost == false)){ //if 'closed' switch is CLOSED and 'open' switch is OPEN, and 'lost' is false
   status = "CLOSED"; //updates the status varible
 } //end of if 'closed'
 
 if ((digitalRead(Close_Limit) == 1) && (digitalRead(Open_Limit) == 1) && (lost == true)){ //if both switches are OPEN, and 'lost' is true
   status = "UNKNOWN"; //updates the status varible
 } //end of if 'closed'
  //=========================================================================================================

   
   //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
   //Actuator status check, checks to see if the position is known or if the Actuator is lost 
   if(millis()>=end_time) { //I.E. >2 s have passed since program started or since 'end_time' was last reset!!
      if ((digitalRead(Close_Limit) == HIGH) && (digitalRead(Open_Limit)) == HIGH) { //if both switches read as OPEN contacts
      lost = true; //change the 'lost' varible to 'true' because...where the heck is the Actuator position?
      }//end of if closed == HIGH     
    } //end of if Actuator lost

   if ((digitalRead(Close_Limit) == LOW) or (digitalRead(Open_Limit)) == LOW){ // if either switch reads as a CLOSED contact
     lost = false; //set the 'lost' variable to 'false' because the roof state is known
     end_time=millis()+ActuatorTravelTime; //reset the timer 'end_time'
    }// end of if Actuator known
   //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$


    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    //This section is commented out, but can be turn back on for troubleshooting
    
    Serial.print("Close Limit is "); //prints text listed on Serial monitor
    Serial.println(Close_Limit);  //prints the varible
    Serial.print("Open Limit is "); //prints text listed on Serial monitor
    Serial.println(Open_Limit);  //prints the varible
    delay (500); //1/2 second delay to allow serial data to be readable
    Serial.print("status is "); //prints text listed on Serial monitor
    Serial.println(status);  //prints the varible
    Serial.print("lost is "); //prints text listed on Serial monitor
    Serial.println(lost);  //prints the varible
    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   
 

} //end of void loop

Your variables Close_Limit and Open_Limit contain the state of pins 1 and 2, not the pin numbers themselves. Compare those lines to earlier ones:

int Open_Limit =digitalRead(1); //creates an integer varible "Open_Limit" and reads pin 1 status into that varible (HIGH if limit switch is open(off), LOW if limit switch is closed(on))
int Close_Limit =digitalRead(2); //creates an integer varible "Close_Limit" and reads pin 2 status into that varible (HIGH if limit switch is open(off), LOW if limit switch is closed(on))

You should be using your variables directly, not within digitalRead()

if (Close_Limit == 1) && (Open_Limit == 0) && (lost == false)) ...

  • Careful, = is not the same as ==

:woozy_face:

ok, that makes sense, I’ll try adjusting that.

good catch

while (Open_Limit = HIGH) {

  • There’s two.

yup fixed them both, i’m not testing that section yet because i dont have the motor controller yet, but I’m sure you saved me more head scratching and grief .

image

That did it. I eliminated all those redundant digital reads, it responds perfectly now, seams a little faster response to the change as well.

Thank You both for the input. I’ve only been writing Arduino code for a couple years, but it has also been about a year since I have written any.

I think i copied that digital read from one of my other sketches that is current in use and in a critical (to me) application. I need to go back and look at that sketch and update it before my Observatory roof crashes.

And if you turn all warnings on, the compiler will catch ALL of them for you.

thanks. nice tip. I turned it on and opened an older version of this sketch and it discovered the = vs ==