Problem in last if (reads true when not)

I am running this on a nano.

in the code below (which is messy sorry just learning) I am having trouble with the last IF statement in the "set mode" routine. It sets the mode to S when the "ret" switch is 1.

first the code then the outout showing switch states as well as mode being set to S

I started this on ide 1.8.x that did not have debug. I lownloaded ide 2 but did not take time to learn the debugger yet. I guess that may be next. For now I put in a bunch of print statements to see what was going on. Once debugged they will all get deleted.

i tried to follow how to post code, I will reread it.

In ide 2 I went to edit copy for forum and pasted it below.

but the line I am having trouble with is the last one. I know the problem is probably before that.

the code


    tripped = 1;
  }
  if (ret == 0 && front == 1) mode = 'S';  // ret switch set and front not hit
}  // end of setmode

the monitor output

Output

cut speed is steps per sec 8

or inches per min 1

Jog speed is steps per sec 807

or this many inches per min-42

current position -77

cut switch 1

return switch 0

jog switch 1

rear switch 1

front switch 1

mode is S

tripped is 1

parked by return is 1

in return case S return mode

You posted the portion of code nicely, but you should post your complete sketch in place of the small portion you posted.

The portion of code you posted says different...
if (ret == 0 and front button not pressed)... mode = 'S';

I tried to post the entire code using instructions i looked for.
In ide2 i used control t to format it

Then i went to edit copy for forum
And pasted it in the post.

I will try to get all of it.

This is the line

But in the monitor ret is set to 1
So is cut.

Sy the if should be false. But it is setting mode to ā€œSā€. This puts the program in the teturn mode when it should be stopped.

attempt two to post code. This time I "selected all" before copying for the forum. lets see how this is.

/*
   i need to add debounce to read switch some time

   There are 2 limit switches.  When it hits the back one it reverses direction, if it it hits the front it shuts off
  There is a one pole 3 position switch (on off on ) that controls the direction.  If in cut direction the speed is cut, if return direction the speed is jog.  If the switch is off  the motor stops.
  There are 2 pots to control speed one if cut speed the other is jog speed
  There is a jog button that only works in the cut direction this increases the speed from cut speed to jog speed while pressed
  I would like use position to set a 0 when starts and when rear limit is hit go back to 0
  if front switch is hit it stops
  putting the 3 way switch to center stops and resets variables like tripped that is set when rear limit is hit

  Basic operation :
  1)  Move carrage to start position either feed and jog or return (start position)
  2) Load rRock.
  3) start cut by pressing switch to feed.  If needed press jog to move quicker. (start position updated)
  4) continue cutting until rear limit switch is hit then reverse direction to start position  then stop
  note to keep track that rear switch has been set update a called tripped because as you move off of it it will go to low.

  pin assignment
  Ao is jog pot
  A1 is cut pot
  A2 cut switch
  A3 return switch
  A4 jog switch
  12  rear limit
  11 Front limit
  3 direction
  2 pulse
  4 cut led (green)
  5 ret led (yellow)
  6 off led (red)
  added pause to help debug 
  added all of the print statements to help debug, before switched to ide ver 2 that has a debug mode 
  remove all of them when it is working 
  note on modes 
  O is off
  C is cut
  J is Jog
  R is return from cut
  S is return mode from switch setting
end of comment 
setting up global variables is next they are global if declaired before setup 
*/
int driverdir = 3;       //direction pin
int driverpul = 2;       // pulsepin
char mode = 'O';         // mode will be cut jog return or off
int cutswitch = A2;      // global variable  for cut switch
int cut = 1;             //cut switch reading
int returnswitch = A3;   // global variable for return switch
int ret = 1;             // global for return
int jogswitch = A4;      // global for jog switch
int jog = 1;             //global for jog switch
int rearswitch = 12;     // global for rear limit
int rear = 1;            //global for rear
int frontswitch = 11;    // global for front limit
int front = 1;           //global for front
int cutpot = A0;         // global for cut pot
int jogpot = A1;         //global for jog pot
int cutspeed = 5;        //global for cut speed set to five untill pots working
int jogspeed = 300;      //global variable for jog speed change to non integer
int tripped = 1;         //variable to store tripped rear limit switch default to on same as switches defaulted hign
int parkedbyreturn = 1;  // a new variable to say parked by returning
int runled = 4;          // led to show in run
int retled = 5;          // led to show returning
int offled = 6;          // led to show off
int temp = 0;            // a global variable to calculate speed


#include <AccelStepper.h>      // this is a library that helps with stepper motors
int stepsPerRevolution = 400;  // change this to fit the number of steps per revolution
// initialize the stepper library driver mode pins 2 and 3
AccelStepper mystepper(1, driverpul, driverdir);
void setup() {
  Serial.begin(115200);                 // open the serial port at 9600 bps
  pinMode(jogpot, INPUT);               //jog pot
  pinMode(cutpot, INPUT);               //cut pot
  pinMode(cutswitch, INPUT_PULLUP);     //CUT
  pinMode(returnswitch, INPUT_PULLUP);  //RETURN
  pinMode(jogswitch, INPUT_PULLUP);     //jog
  pinMode(rearswitch, INPUT_PULLUP);    //rear limit
  pinMode(frontswitch, INPUT_PULLUP);   //front limit
  pinMode(driverpul, OUTPUT);           //pulse
  pinMode(driverdir, OUTPUT);           //direction
  pinMode(runled, OUTPUT);              // run led
  pinMode(retled, OUTPUT);              // return led
  pinMode(offled, OUTPUT);              // off led
  pinMode(10, INPUT_PULLUP);            // SWITCH TO ALLOW PASE TO HELP DEBUT
  mystepper.setMaxSpeed(4000);
  mystepper.setSpeed(40);  // a default low speed
}
void loop() {
  while (digitalRead(10) == HIGH) {  // set here to debug comment out when debuged
  }                                  // end of while for pause
  readpots();                        //reads pots
  readswitches();                    //reads switches
  setmode();                         //determines the mode
  Serial.print("mode is ");
  Serial.println(mode);
  Serial.print("tripped is ");
  Serial.println(tripped);
  Serial.print("parked by return is ");
  Serial.println(parkedbyreturn);
  switch (mode) {
    case 'O':
      digitalWrite(offled, HIGH);
      digitalWrite(runled, LOW);
      digitalWrite(retled, LOW);
      Serial.print(" CASE off mode");
      mystepper.disableOutputs();
      tripped = 1;                      // motor is off set rtipped high
      mystepper.setCurrentPosition(0);  // set position to 0
      break;
    case 'C':
      digitalWrite(offled, LOW);
      digitalWrite(runled, HIGH);
      digitalWrite(retled, LOW);
      Serial.print("CASE Cut Mode");
      mystepper.enableOutputs();
      mystepper.setSpeed(cutspeed);
      for (int i = 0; i <= 200; i++) {  //take half a turn
        mystepper.runSpeed();
        Serial.print("in cut for loop");
        Serial.println("my position is");
        Serial.println(mystepper.currentPosition());
      }
      break;
    case 'J':
      digitalWrite(offled, LOW);
      digitalWrite(runled, HIGH);
      digitalWrite(retled, LOW);
      Serial.print("CASE jog mode");  // in jog mode
      mystepper.setSpeed(jogspeed);
      for (int i = 0; i <= 200; i++) {  //take half a turns
        Serial.print("in jog for counter is ");
        Serial.println(i);
        mystepper.runSpeed();
      }                                 //take a step do it 200 times
      mystepper.setCurrentPosition(0);  // set cuttent position to 0
      break;
    case 'R':  //this is return during a cut
      digitalWrite(offled, LOW);
      digitalWrite(runled, LOW);
      digitalWrite(retled, HIGH);
      Serial.print("CASE return mode");
      mystepper.setSpeed(jogspeed * -1);
      for (int i = 0; i <= 200; i++) {  //take half a turn
        mystepper.runSpeed();
        if (mystepper.currentPosition() <= 0) {
          i = 200;
          mode = 'O';
        }
        Serial.print("in return for loop ");
        Serial.print("position is ");
        Serial.println(mystepper.currentPosition());
      };
      mystepper.setCurrentPosition(0);  // set cuttent position to 0
      break;
    case 'S':  // in this case we ahave return switch set so back up
      digitalWrite(offled, LOW);
      digitalWrite(runled, LOW);
      digitalWrite(retled, HIGH);
      Serial.print(" in return case S return mode");
      mystepper.setSpeed(jogspeed * -1);
      for (int i = 0; i <= 200; i++) {  //take half a turn
        mystepper.runSpeed();
      }
      break;
    default:  // default we are in unknown state shut things down
      mystepper.disableOutputs();
      tripped = 1;  // motor is off set rtipped high
      digitalWrite(offled, HIGH);
      digitalWrite(runled, LOW);
      digitalWrite(retled, LOW);
      break;
  }  //end of switch mode


}  // end of loop()


void readpots() {
  Serial.println(analogRead(A1));
  cutspeed = map((analogRead(A1)), 0, 1025, 0, 20);  // set cut speed
  Serial.print("cut speed is steps per sec  ");
  Serial.println(cutspeed);
  Serial.print("or inches per min ");
  temp = cutspeed * 60 / 400;
  Serial.println(temp);
  jogspeed = map((analogRead(A0)), 0, 1025, 0, 2000);  //set jog speed
  Serial.print("Jog speed is steps per sec ");
  Serial.println(jogspeed);
  Serial.print("or this many inches per min");
  temp = jogspeed * 60 / 400;
  Serial.println(temp);
  Serial.print("current position ");
  Serial.println(mystepper.currentPosition());
}

void readswitches() {
  cut = digitalRead(cutswitch);
  Serial.print("cut switch ");
  Serial.println(cut);
  ret = digitalRead(returnswitch);
  Serial.print("return switch ");
  Serial.println(ret);
  jog = digitalRead(jogswitch);
  Serial.print("jog switch ");
  Serial.println(jog);
  rear = digitalRead(rearswitch);
  Serial.print("rear switch ");
  Serial.println(rear);
  front = digitalRead(frontswitch);
  Serial.print("front switch ");
  Serial.println(front);
}
void setmode() {
  // note pins are high by default same for tripped and parked
  if (cut == 1 && ret == 1) {  //cut and return switches are off defaulted high clear settings
    mode = 'O';                // set mode to off
    parkedbyreturn = 1;        // reset parked by return
    tripped = 1;               //remember default to high  do this even if tripped or parked
    Serial.println(" if #2 cut and return are off");
  }
  if (cut == 0 && tripped == 0 && front == 0) {  // was in return mode but hit front switch
    mode = 'O';
    parkedbyreturn = 0;
  }
  if (cut == 0 && tripped == 0 && mystepper.currentPosition() <= 0) {  // in return mode from tripped but position is home if 3
    mode = 'O';
    tripped = 1;         // reset tripped
    parkedbyreturn = 0;  // set parked
    Serial.println("if #3 cut and return tripped with position home");
  }
  if (cut == 0 && parkedbyreturn == 0) {  //cut switch is on but have been parked so stop if 4
    mode = 'O';
    tripped = 1;  // reset tripped sonce now home
    Serial.println("if #4 cut is on but parkedbyreturn");
  }
  if (ret = 0 && front == 0) {  //in return mode but hit front limit
    mode = 'O';
    Serial.println("was returning but hit front switch");
  }
  if (cut == 0 && tripped == 1 && parkedbyreturn == 1) {  // in cut mode but not tripped yet and parked has been reset  if 5
    mode = 'C';
    Serial.println("if #5 cut on but not tripped or parked");
  }
  if (cut == 0 && jog == 0 && tripped == 1 && parkedbyreturn == 1) {  // in cut mode, and jogg pressed but not tripped, not parkedif 6
    mode = 'J';
    Serial.println("if #6 cut and jog npot tripped not parked");
  }
  if (cut == 0 && rear == 0) {  // in cut mode but hit rear stop staqrt return mode, set tripped  if 7
    mode = 'R';
    tripped = 0;
    Serial.println("if #7 cut and just hit rear limit");
  }
  if (cut == 0 && tripped == 0 && mystepper.currentPosition() > 0) {  // you were cutting but now returning but not at 0 so step again if 8
    mode = 'R';
    Serial.println("if #8 cut and tripped but position is not 0");
  }
  if (cut == 0 && tripped == 0 && mystepper.currentPosition() <= 0) {
    Serial.println("hit 0 pn return");
    mode = 'O';
    parkedbyreturn = 0;
    tripped = 1;
  }
  if (ret == 0 && front == 1) mode = 'S';  // ret switch set and front not hit
}  // end of setmode

it looks better,

Sorry I just noticed that in fact in the example cut is 0 and front is 1. so mode should be "S"

I ran this test a few times. in all cases I was getting an S. I will have to get the monitor outout that shows the problem. (cut =1 front = 1)

sorry about the confusing posts and print statements.

I will retest it again.

the same program this time the data that shows the issue.

cut speed is steps per sec 5

or inches per min 0

Jog speed is steps per sec 556

or this many inches per min-80

current position -21

cut switch 1

return switch 1

jog switch 1

rear switch 1

front switch 1

if #2 cut and return are off

mode is S

tripped is 1

parked by return is 1

in return case S return mode

in this case return is 1
but still getting mode 'S'

this is anouther example, cut switch is 0 everything else is 1. So should be in cut mode, instead in S mode.

cut speed is steps per sec 8

or inches per min 1

Jog speed is steps per sec 821

or this many inches per min-40

current position -182

cut switch 0

return switch 1

jog switch 1

rear switch 1

front switch 1

if #5 cut on but not tripped or parked

mode is S

tripped is 1

parked by return is 1

in return case S return mode

I have commented out the last if, and things work, except cannot get into a return mode by the switch.

In the latest test no matter what switches are set, i end up in ā€˜Sā€™ mode.

In your code, this will assign the value 0 to the ret variable.

  if (ret = 0 && front == 0)

I found that quite easily because I've set compiler warnings in file ā†’ preferences to ALL. The warning that I got:

C:\Users\Wim\AppData\Local\Temp\.arduinoIDE-unsaved202382-11272-4bayrk.kjyo\sketch_sep2a\sketch_sep2a.ino: In function 'void setmode()':
C:\Users\Wim\AppData\Local\Temp\.arduinoIDE-unsaved202382-11272-4bayrk.kjyo\sketch_sep2a\sketch_sep2a.ino:255:11: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if (ret = 0 && front == 0)
       ~~~~^~~~~~~~~~~~~~~~~
1 Like

thanks I will set preferences
I woke up this morning at 5:00 and found the same error after i added a new print inside the if with the value of ret. it was 0, daaaaaaa.
I will have to learn the debugger. i took a stab at it but did not have much luck. I will have to do more reading.

thanks so much for the replies and taking time.
I have reset the preferences. it showed up right away.

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