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
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.