Different behaviour with the same code using a different computer

Hi everyone!

I'm having this strange thing : I've changed from one computer to another one to control an Arduino Uno board via the Serial Communication and it doesn't behave the same way..

The difference is : with the first computer everything worked fine, but now after starting an initialization procedure, it stops and doesn't come back to the main loop..

I just copy and past the code so no differences here, i'm using the same software version (1.6.9), but the OS is different: it was Windows 10 on the first computer and now it's Vista, could it have a link with the problem ?

Here's the code:

#include<AccelStepper.h>
#define STEPS 200
#define SPEED 1000
#define POSITION_MAX_RIGHT 35
#define POSITION_MAX_LEFT 0

volatile boolean fin_course = false;
const byte interruptPin = 3;  // pin used for interruption
String inputString = "";    // a string to hold incoming data
int desired_position=0;
int real_position=0;
int deplacement=0;
long steps_motor_1=0;     
int move_number=0;

AccelStepper motor_1(AccelStepper::FULL2WIRE,2,4);    

void setup() {
  attachInterrupt (digitalPinToInterrupt(interruptPin), procedure_fincourse, RISING);   // interruption
  Serial.begin(9600);     // initialize serial
  pinMode(6,OUTPUT);      // set pin 6 as output
  inputString.reserve(200);   // reserve 200 bytes for the inputString

  motor_1.setMaxSpeed(SPEED);
  motor_1.setAcceleration(SPEED/4);
  Serial.println("\nHi there! \nPress enter to start the initializion"); 
}

void loop() {
 while(Serial.available()){ 
  if (move_number == 0) {
    procedure_init(); 
  }
  else {
    char inChar=(char)Serial.read(); // c'est pour le gouffre freeeere 
    inputString+=inChar;
    
    // ------- loop speed/number of steps once we press enter -------- //
    if (inChar=='\n'){
  
      // ------ setup steps/position ------ //
      desired_position=inputString.toInt();

      // ------ upper and lower edge ------ //
      if(desired_position>POSITION_MAX_RIGHT){
        desired_position=POSITION_MAX_RIGHT;
        Serial.println("Desired position set to 35cm");
        Serial.println();
      }
      if(desired_position<POSITION_MAX_LEFT){
        desired_position=POSITION_MAX_LEFT;
        Serial.println("Desired position set to 0cm");
        Serial.println();
      }
    
      // -------- Sign for the steps and run -------- //
      if (desired_position<real_position){
        Serial.print("You set the horizontal position at : ");
        Serial.print(desired_position);
        Serial.println(" cm");
        Serial.println();
        deplacement=(desired_position - real_position);
        real_position=(real_position + deplacement);
        steps_motor_1=(deplacement)*1000L;
        digitalWrite(6,LOW);
        procedure_mvt();
      }
      if (desired_position>real_position){
        Serial.print("You set the horizontal position at : ");
        Serial.print(desired_position);
        Serial.println(" cm");
        Serial.println();       
        deplacement=(desired_position - real_position);
        real_position=(real_position + deplacement);
        steps_motor_1=(deplacement)*1000L;   
        digitalWrite(6,HIGH);
        procedure_mvt();
      }

      if (fin_course == true) {
        Serial.println("You reached the end of the course");
        Serial.println();
        Serial.print("The horizontal position is now : ");
        Serial.print(real_position);
        Serial.println(" cm");
        Serial.println();
        Serial.println(" ---------------------------------------------");
        Serial.println();
      }
      else {
        Serial.print("The horizontal position is now : ");
        Serial.print(real_position);
        Serial.println(" cm");
        Serial.println();
        Serial.println(" ---------------------------------------------");
        Serial.println();
      }

      move_number++;
      fin_course=false;
      inputString="";

      Serial.print("move number : ");
      Serial.println(move_number);
      Serial.println();
      Serial.println("Enter desired position (from 0 cm to 35cm) and press enter");
      Serial.println();
  
    } // end if (inChar=='\n')
  } // end else (move_number=0)
 } // end while Serial.available
} // end loop

void procedure_init() {  
  // initialize by going right until it reachs the limit switch //
  Serial.println("\nInitializing \n");
  steps_motor_1=42000; 
  while (fin_course == false) {
    digitalWrite(6,LOW);
    motor_1.moveTo(steps_motor_1);
    motor_1.run();
  }
  real_position=0; // set real_position to 0 
  move_number++;
  fin_course=false;
 
  Serial.print("Position = ");
  Serial.print(real_position);
  Serial.print(" cm\n");
  Serial.println();
  return;
}

void procedure_fincourse() {
  fin_course = true;
}
    
void procedure_mvt() {
  while ( fin_course == false && motor_1.currentPosition() != steps_motor_1) {  
    motor_1.moveTo(steps_motor_1);
    motor_1.run(); 
  }
  if (fin_course == true) {
    procedure_stop();
  }
  return;
}   

void procedure_stop() {
  motor_1.stop();
  if (digitalRead(6) == LOW){
    real_position = 0;
  }
  else if (digitalRead(6) == HIGH){
    real_position = 35;
  }
  return;
}

I attached a screenshot of the Serial Interface when it works well, and now it stops after printinig "Position = 0 cm".

I've been looking around the internet but I didn't find any reasons for this difference of behaviour, so if the same thing ever happened to you or if you have some advice, it'll be very helpfull :slight_smile:

Thank you,

P.S. : I think it's due to a problem of Arduino itself so I posted here, don't hesitate to tell me if it must be posted anywhere else.

    if (inChar=='\n'){

Maybe you have the Serial Monitor line ending set differently. There is a menu for that at the bottom of the Serial Monitor window.

How much ram does that use? You're printing a lot of string constants, and you don't seem to know about the F() macro, so between that and your use of the String class, I'm uncomfortable. Every time you have a Serial.print() or println(), and all you're passing it is a static string in quotes, wrap the string in F() - that puts the string into flash only. Otherwise the string gets loaded into RAM, and we only have 2048 bytes of RAM available on an 328 based board.

This is unlikely to be the issue at hand, but it does make me uncomfortable about the sketch (especially since you're using String and your sketch is not hardened against perverse input)

You need to determine whether the issue depends on the system used to compile and upload the code, or the system that it's plugged in on. What about resetting the board while it's connected to serial monitor?

Thanks for your answers!

Indeed, the Serial Monitor line ending was set differently ("No line ending" instead of "both NL & CR") and now everything works fine again :smiley:

Thanks also for telling me about the F() macro, I didn't know about this, but it seems quite interesting.

Thank you,