Strange error when printing variable value

Hi,

There’s something wrong with my code, but I can’t figure the problem. Here’s my complete sketch:

#include <SimpleStepper.h>


#define LIMIT1        8
#define LIMIT2        9
#define BUTTON        10

int ledaux = 0;
int stepval = 1;

SimpleStepper mot1(2, 3, 4);
SimpleStepper mot2(5, 6, 7);

void setup() {
  
  pinMode(LED_BUILTIN, OUTPUT);
  
  Serial.begin(9600);
  Serial.println("setup ok");
}

void loop() {
  while(digitalRead(BUTTON)) {
    digitalWrite(LED_BUILTIN, ledaux);
    ledaux = 1 - ledaux;
    delay(200);
  }
  Serial.println("Serial ok");

  while(1) {
    handleSerial();
  }
}

void home(SimpleStepper ss, int limit_pin, int spin) {
  int debounce = 0;
  spin = (spin < 0) ? -1 : 1;
  while (1) {
    while (digitalRead(limit_pin)) {
      debounce ++;
      if (debounce > 5) {
        Serial.print("pos: "); Serial.println(ss.currentPosition());
        return;
      }
    }
    debounce = 0;
    ss.move(spin);
    ss.runToPosition();
  }
}

void handleSerial() {
  int moved = 0;
  int debounce = 0;
  while (Serial.available() > 0) {
    char received = Serial.read();
    switch (received) {
      case 'i':
      case 'I':
        stepval = 1;
        break;
      case 'v':
      case 'V':
        stepval = 5;
        break;
      case 'x':
      case 'X':
        stepval = 10;
        break;
      case 'l':
      case 'L':
        stepval = 50;
        break;
      case 'c':
      case 'C':
        stepval = 100;
        break;
      case 'd':
      case 'D':
        stepval = 500;
        break;
      case 'm':
      case 'M':
        stepval = 1000;
        break;
      case '1':
        mot1.enable();
        mot1.move(-stepval);
        mot1.runToPosition();
        mot1.disable();
        break;
      case '2':
        mot1.enable();
        mot1.move(stepval);
        mot1.runToPosition();
        mot1.disable();
        break;
      case '3':
        mot2.enable();
        mot2.move(-stepval);
        mot2.runToPosition();
        mot2.disable();
        break;
      case '4':
        mot2.enable();
        mot2.move(stepval);
        mot2.runToPosition();
        mot2.disable();
        break;
      case '5':
        Serial.println("Homing mot1...");
        Serial.print("Inipos: "); Serial.println(mot1.currentPosition());
        mot1.setCurrentPosition(0);
        mot1.enable();
        home(mot1, LIMIT1, -1);
        mot1.disable();
        Serial.print("Moved: "); Serial.println(mot1.currentPosition());
        mot1.setCurrentPosition(0);
        
        Serial.println("Homing mot2...");
        Serial.print("Inipos: "); Serial.println(mot2.currentPosition());
        mot2.setCurrentPosition(0);
        mot2.enable();
        home(mot2, LIMIT2, -1);
        mot2.disable();
        Serial.print("Moved: "); Serial.println(mot2.currentPosition());
        mot2.setCurrentPosition(0);
        break;
    }
    Serial.print(mot1.currentPosition());
    Serial.print(", ");
    Serial.println(mot2.currentPosition());
  }
}

The code drives a small pan&tilt robot, and I’m now working on my homing routine. The problem is at the end of the sketch, inside case: “5” of the switch statement in handleSerial() function. When I try to print how much the motor moved to reach the zero position, I always get 0, no matter what the initial position was.
I added another print statement in the home function, just to print the position before exiting the function, and that works fine.

This is a typical output when I run the home command:

Homing mot1...
Inipos: 352
pos: -350    // printed from home() using Serial.print("pos: "); Serial.println(ss.currentPosition());
Moved: 0    // printed from handleSerial() using Serial.print("Moved: "); Serial.println(mot1.currentPosition());
Homing mot2...
Inipos: 449
pos: -449    // printed from home() using Serial.print("pos: "); Serial.println(ss.currentPosition());
Moved: 0    // printed from handleSerial() using Serial.print("Moved: "); Serial.println(mot2.currentPosition());
0, 0

I really don’t understand what’s going on. It probably is a stupid mistake but I really can’t find it. Can anyone help with this?

I’m running on an Arduino Nano. The SimpleStepper library is very similar to AccelStepper library, but without the acceleration stuff (I wrote it my self trying to keep that API compatible).

Thanks in advance.

        mot1.setCurrentPosition(0);
        mot1.enable();
        home(mot1, LIMIT1, -1);
        mot1.disable();
        Serial.print("Moved: ");
        Serial.println(mot1.currentPosition());

If I understand correctly you set the current position to zero then a few lines later you print the current position, which is, of course, zero

Surely you should be printing the current position before setting it to zero.

UKHeliBob: If I understand correctly you set the current position to zero then a few lines later you print the current position, which is, of course, zero

Surely you should be printing the current position before setting it to zero.

Right, but the home() call moves the motor and thus changes the current position. You can see this in the sample output I included. The thing is, the current position prints ok from the home function, but not from the caller.

Ok I just found the problem...

I was passing the SimpleStepper object by value, effectively creating a copy of it inside the home() function. I just modified the sketch to pass a reference and now it works like a charm.

Cheers!