Serial Communication don't interfere in the rest of code

Hi!

I 'm doing a code for controlling stepper motors among other things , but I needed to perform the serial communication , but did not want this interferes with the engine delay time. I'm already using the code below , but even then it is noted differences when is or is not communicating. Does anyone know how can you change this?

Thanks!!
if ((unsigned long)(currentMillis - previousmillis_Stepper) >= Stepper_Vel) {
Stepper_dir = (analogRead(Stepper_Pot));

if (Stepper_dir <= 500) {
Stepper_Vel=10000;
//Stepper_Vel = (10 + (Stepper_dir * 30));
}
if (Stepper_dir > 500) {
Stepper_Vel = (((analogRead(Stepper_Pot)) * 10) - 5000); //puxar
}
previousmillis_Stepper = currentMillis;

jreis:
Hi!

I 'm doing a code for controlling stepper motors among other things , but I needed to perform the serial communication , but did not want this interferes with the engine delay time. I'm already using the code below , but even then it is noted differences when is or is not communicating. Does anyone know how can you change this?

Thanks!!
if ((unsigned long)(currentMillis - previousmillis_Stepper) >= Stepper_Vel) {
Stepper_dir = (analogRead(Stepper_Pot));

if (Stepper_dir <= 500) {
Stepper_Vel=10000;
//Stepper_Vel = (10 + (Stepper_dir * 30));
}
if (Stepper_dir > 500) {
Stepper_Vel = (((analogRead(Stepper_Pot)) * 10) - 5000); //puxar
}
previousmillis_Stepper = currentMillis;

Please explain in brief what you want to make.
Do you want to control steppers using Serial monitor ?

I have five stppers working, but I want change their speed through a program in visual basic. If I have the communication, the speed of the motors changed.

Can you post the whole arduino code ?

I control 3 steppers on a small lathe using a Python program on my PC. I have arranged things so that the PC sends the data which is designed to be less than 64 bytes so it fits in the serial input buffer. The Arduino reads that data and asks for the data for the following move. While the PC is sending that data the Arduino is controlling the movement of the motors. When the movement has finished the data for the next move will be waiting.

I hope that makes sense.

...R
Serial Input Basics
Python Binary Data demo.
Stepper Motor Basics

MalharD:
Can you post the whole arduino code ?

This is my code

// MOTORES
#define Blue_STEP_PIN         54
#define Blue_DIR_PIN          55
#define Blue_ENABLE_PIN       38

#define Yellow_STEP_PIN       60
#define Yellow_DIR_PIN        61
#define Yellow_ENABLE_PIN     56

#define White_STEP_PIN        46
#define White_DIR_PIN         48
#define White_ENABLE_PIN      62

#define Black_STEP_PIN        26
#define Black_DIR_PIN         28
#define Black_ENABLE_PIN      24

#define Red_STEP_PIN          36
#define Red_DIR_PIN           34
#define Red_ENABLE_PIN        30

//Cartucho de Aquecimento
#define HEATER_0_PIN           9

//THERMISTOR NTC 3650 100KOhm
#define TEMP_0_PIN            13
#define THERMISTORNOMINAL  95000    //temperatura
#define TEMPERATURENOMINAL    25    //temperatura
#define NUMSAMPLES             5    //temperatura
#define BCOEFFICIENT        4000    //temperatura
#define SERIESRESISTOR      4700    //temperatura
int samples[NUMSAMPLES];            //temperatura
//Ventoinha
#define FAN_PIN               10

//Potenciometros
#define Blue_Pot              12
#define Yellow_Pot             4
#define White_Pot              5
#define Black_Pot              3
#define Red_Pot               11



//Distancia do filamento
int Blue_dist   = 0;
int Yellow_dist = 0;
int White_dist  = 0;
int Black_dist  = 0;
int Red_dist    = 0;

//Direcao dada pelos poteciometros
int Blue_dir    = 0;
int Yellow_dir  = 0;
int White_dir   = 0;
int Black_dir   = 0;
int Red_dir     = 0;

//Velocidade dos motores
int Blue_Vel    = 0;
int Yellow_Vel  = 0;
int White_Vel   = 0;
int Black_Vel   = 0;
int Red_Vel     = 0;

//Codigo em separado
unsigned long currentmillis = 0;
unsigned long previousmillis_Blue = 0;
unsigned long previousmillis_Yellow = 0;
unsigned long previousmillis_White = 0;
unsigned long previousmillis_Black = 0;
unsigned long previousmillis_Red = 0;
unsigned long previousmillis_temp = 0;
unsigned long print_temp = 1000000;

void setup() {
 pinMode(TEMP_0_PIN             , INPUT);
 //pinMode(TEMP_1_PIN,INPUT);
 //pinMode(TEMP_2_PIN,INPUT);
 //analogReference(EXTERNAL);    //temperatura

 pinMode(FAN_PIN                , OUTPUT);
 digitalWrite(FAN_PIN, HIGH);
 pinMode(HEATER_0_PIN           , OUTPUT);
 //pinMode(HEATER_1_PIN,OUTPUT);
 //pinMode(LED_PIN,OUTPUT);

 pinMode(Blue_STEP_PIN           , OUTPUT);
 pinMode(Blue_DIR_PIN            , OUTPUT);
 pinMode(Blue_ENABLE_PIN         , OUTPUT);

 pinMode(Yellow_STEP_PIN         , OUTPUT);
 pinMode(Yellow_DIR_PIN          , OUTPUT);
 pinMode(Yellow_ENABLE_PIN       , OUTPUT);

 pinMode(White_STEP_PIN          , OUTPUT);
 pinMode(White_DIR_PIN           , OUTPUT);
 pinMode(White_ENABLE_PIN        , OUTPUT);

 pinMode(Black_STEP_PIN          , OUTPUT);
 pinMode(Black_DIR_PIN           , OUTPUT);
 pinMode(Black_ENABLE_PIN        , OUTPUT);

 pinMode(Red_STEP_PIN            , OUTPUT);
 pinMode(Red_DIR_PIN             , OUTPUT);
 pinMode(Red_ENABLE_PIN          , OUTPUT);


 digitalWrite(Blue_ENABLE_PIN    , LOW);
 digitalWrite(Yellow_ENABLE_PIN  , LOW);
 digitalWrite(White_ENABLE_PIN   , LOW);
 digitalWrite(Black_ENABLE_PIN   , LOW);
 digitalWrite(Red_ENABLE_PIN     , LOW);

 pinMode(Blue_Pot                , INPUT);
 pinMode(Yellow_Pot              , INPUT);
 pinMode(White_Pot               , INPUT);
 pinMode(Black_Pot               , INPUT);
 pinMode(Red_Pot                 , INPUT);
 Serial.begin(250000);
}


// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

const long interval = 1000;           // interval at which to blink (milliseconds)

void loop () {


 unsigned long currentMillis = micros();

 if ((unsigned long)(currentMillis - previousmillis_Blue) >= Blue_Vel) {
   Blue_dir = (analogRead(Blue_Pot));

   if (Blue_dir <= 500) {
     Blue_Vel=10000;
     //Blue_Vel = (10 + (Blue_dir * 30)); //empurrar
   }
   if (Blue_dir > 500) {
     Blue_Vel = (((analogRead(Blue_Pot)) * 10) - 5000); //puxar
   }
   previousmillis_Blue = currentMillis;

   if (Blue_dir <= 475) {
     digitalWrite(Blue_DIR_PIN, HIGH);

     digitalWrite(Blue_STEP_PIN    , HIGH);
     digitalWrite(Blue_STEP_PIN    , LOW);
     //Serial.println(Blue_dir);
     //Serial.println(Blue_Vel);
   }
   if ((Blue_dir >= 525) && (Blue_dir <= 1000)) {
     digitalWrite(Blue_DIR_PIN, LOW);

     digitalWrite(Blue_STEP_PIN    , HIGH);
     digitalWrite(Blue_STEP_PIN    , LOW);
     //Serial.println(Blue_dir);
   }
 }
 if ((unsigned long)(currentMillis - previousmillis_Yellow) >= Yellow_Vel) {
   Yellow_dir = (analogRead(Yellow_Pot));

   if (Yellow_dir <= 500) {
     //Yellow_Vel = (10 + (Yellow_dir * 30));
     Yellow_Vel = 10000;
   }
   if (Yellow_dir > 500) {
     Yellow_Vel = (((analogRead(Yellow_Pot)) * 10) - 5000);
   }
   previousmillis_Yellow = currentMillis;

   if (Yellow_dir <= 475) {
     digitalWrite(Yellow_DIR_PIN, HIGH);

     digitalWrite(Yellow_STEP_PIN    , HIGH);
     digitalWrite(Yellow_STEP_PIN    , LOW);
     //Serial.println(Yellow_dir);
     //Serial.println(Yellow_Vel);
   }
   if ((Yellow_dir >= 525) && (Yellow_dir <= 1000)) {
     digitalWrite(Yellow_DIR_PIN, LOW);

     digitalWrite(Yellow_STEP_PIN    , HIGH);
     digitalWrite(Yellow_STEP_PIN    , LOW);
     //Serial.println(Yellow_dir);
   }
 }
 if ((unsigned long)(currentMillis - previousmillis_White) >= White_Vel) {
   White_dir = (analogRead(White_Pot));

   if (White_dir <= 500) {
     //White_Vel = (10 + (White_dir * 30));
     White_Vel = 10000;
   }
   if (White_dir > 500) {
     White_Vel = (((analogRead(White_Pot)) * 10) - 5000);
   }
   previousmillis_White = currentMillis;

   if (White_dir <= 475) {
     digitalWrite(White_DIR_PIN, HIGH);

     digitalWrite(White_STEP_PIN    , HIGH);
     digitalWrite(White_STEP_PIN    , LOW);
     //Serial.println(White_dir);
     //Serial.println(White_Vel);
   }
   if ((White_dir >= 525) && (White_dir <= 1000)) {
     digitalWrite(White_DIR_PIN, LOW);

     digitalWrite(White_STEP_PIN    , HIGH);
     digitalWrite(White_STEP_PIN    , LOW);
     //Serial.println(White_Pot);
   }
 }
 if ((unsigned long)(currentMillis - previousmillis_Black) >= Black_Vel) {
   Black_dir = (analogRead(Black_Pot));

   if (Black_dir <= 500) {
     //Black_Vel = (10 + (Black_dir * 30));
     Black_Vel=10000;
   }
   if (Black_dir > 500) {
     Black_Vel = (((analogRead(Black_Pot)) * 10) - 5000);
   }
   previousmillis_Black = currentMillis;

   if (Black_dir <= 475) {
     digitalWrite(Black_DIR_PIN, HIGH);

     digitalWrite(Black_STEP_PIN    , HIGH);
     digitalWrite(Black_STEP_PIN    , LOW);
     //Serial.println(Black_dir);
     //Serial.println(Black_Vel);
   }
   if ((Black_dir >= 525) && (Black_dir <= 1000)) {
     digitalWrite(Black_DIR_PIN, LOW);

     digitalWrite(Black_STEP_PIN    , HIGH);
     digitalWrite(Black_STEP_PIN    , LOW);
     //Serial.println(Black_dir);
   }
 }
 if ((unsigned long)(currentMillis - previousmillis_Red) >= Red_Vel) {
   Red_dir = (analogRead(Red_Pot));

   if (Red_dir <= 500) {
     //Red_Vel = (10 + (Red_dir * 30));
     Red_Vel = 10000;
   }
   if (Red_dir > 500) {
     Red_Vel = (((analogRead(Red_Pot)) * 10) - 5000);
   }
   previousmillis_Red = currentMillis;

   if (Red_dir <= 475) {
     digitalWrite(Red_DIR_PIN, HIGH);

     digitalWrite(Red_STEP_PIN    , HIGH);
     digitalWrite(Red_STEP_PIN    , LOW);
     //Serial.println(Red_dir);
     //Serial.println(Red_Vel);
   }
   if ((Red_dir >= 525) && (Red_dir <= 1000)) {
     digitalWrite(Red_DIR_PIN, LOW);

     digitalWrite(Red_STEP_PIN    , HIGH);
     digitalWrite(Red_STEP_PIN    , LOW);
     //Serial.println(Red_dir);
   }
 }
 int value_temp = (analogRead(TEMP_0_PIN));
 float TEMPERATURA = temp[value_temp];
 if ((unsigned long)(currentMillis - previousmillis_temp) >= print_temp) {
   previousmillis_temp = currentMillis;
   
   Serial.print("Temperature ");
   Serial.print(temp[value_temp]);
   Serial.println(" Graus Celsius");
 }

Robin2:
I control 3 steppers on a small lathe using a Python program on my PC. I have arranged things so that the PC sends the data which is designed to be less than 64 bytes so it fits in the serial input buffer. The Arduino reads that data and asks for the data for the following move. While the PC is sending that data the Arduino is controlling the movement of the motors. When the movement has finished the data for the next move will be waiting.

I hope that makes sense.

...R
Serial Input Basics
Python Binary Data demo.
Stepper Motor Basics

Yes, but i want control the motors at the same time.

jreis:
Yes, but i want control the motors at the same time.

That is how my lathe works.

For each movement the PC sends the total duration (in microsecs) and the interval between steps for each motor. The Arduino then translates that into the appropriate number and timing for the steps.

...R

I have been searching and I found a scheduler library, but I have a arduino mega 2560 and in compilation the arduino program gives me this error:
WARNING: library Scheduler claims to run on [sam architecture(s) and may be incompatible with your current board which runs on samd] architecture(s).
C:\Users\JOO~1\AppData\Local\Temp\cc4JeyQw.s: Assembler messages:

C:\Users\JOO~1\AppData\Local\Temp\cc4JeyQw.s:46: Error: constant value required

exit status 1
Error compiling for board Arduino Leonardo.
What do I do?

How will a scheduler library solve the problem? In general those libraries uses more CPU cycles than doing it the simple way.

Show us some examples of what data you want to send to the Arduino.

...R

With scheduler library I can have some loops and thus have a loop for the motors and another for the communication eg.

I want to monitor the temperature in real time, for example.

jreis:
With scheduler library I can have some loops and thus have a loop for the motors and another for the communication eg.

I want to monitor the temperature in real time, for example.

The way you have described that it sounds as if you think the library will enable the Arduino to do more, when in fact it will cause it to do less because of the overhead of the library code.

The code in Several Things at a Time will allow you to manage multiple activities with a minimum of overhead.

I did not post that link earlier because I thought your problem was only about receiving serial data.

You have not responded to my request in Reply #9 for examples of the serial data.

...R

several things at the same time achieves the same thing.

void loop()
{
  readSerial();
  controlMotor1();
  controlMotor2();
  ...
  ...
  writeSerial("some data");
}

You must just write the functions not to block and you have multiple loops :wink:

By the way, the code in reply #5 is not the complete code. I suspect it's missing a '}' and the compiler states that temp is not declared in the line float TEMPERATURA = temp[value_temp];.

Also, looking at that particular part of the code, is temp indeed an array with 1024 elements? And why do you have a float TEMPERATURA that is never used?

Robin2:
The way you have described that it sounds as if you think the library will enable the Arduino to do more, when in fact it will cause it to do less because of the overhead of the library code.

The code in Several Things at a Time will allow you to manage multiple activities with a minimum of overhead.

I did not post that link earlier because I thought your problem was only about receiving serial data.

You have not responded to my request in Reply #9 for examples of the serial data.

...R

The serial data I want to send is: Delay between High and low, turn on or turn off some outputs, control the temperature. More and less this is.

sterretje:
several things at the same time achieves the same thing.

void loop()

{
  readSerial();
  controlMotor1();
  controlMotor2();
  ...
  ...
  writeSerial("some data");
}



You must just write the functions not to block and you have multiple loops ;)

By the way, the code in reply #5 is not the complete code. I suspect it's missing a '}' and the compiler states that *temp* is not declared in the line *float TEMPERATURA = temp[value_temp];*.

Also, looking at that particular part of the code, is *temp* indeed an array with 1024 elements? And why do you have a *float TEMPERATURA* that is never used?

The code is a example of the mine, i can't publish all the code.

Your sugestion is sounds good, basically if I have a delay in controlmotor1(), it does not interfere with controlmotor2()?

Thanks for your help!

jreis:
The serial data I want to send is: Delay between High and low, turn on or turn off some outputs, control the temperature. More and less this is.

I want you to tell us the exact text that you want to send. I doubt if you have in mind sending
"Delay between High and low"
or
"turn on or turn off some outputs"
or
"control the temperature"

For successful programming you must get the fine detail correct. Without knowing that detail it is hard to give useful advice.

...R

Robin2:
I want you to tell us the exact text that you want to send. I doubt if you have in mind sending
"Delay between High and low"
or
"turn on or turn off some outputs"
or
"control the temperature"

For successful programming you must get the fine detail correct. Without knowing that detail it is hard to give useful advice.

...R

my string is:

vel_1.vel_2.vel_3.vel_4.vel_5.color.est_hot.est_ven1.est_ven2 (...)
this is final string
30000.35000.32000.25000.28000.1.1.1 (...)

thanks for your help

jreis:
Your sugestion is sounds good, basically if I have a delay in controlmotor1(), it does not interfere with controlmotor2()?

If you use non-blocking delays, it will work. You seem to be well on your way. Just a question why you assign micros() to a variable called millis?

Robin2's examples for the serial port reading should not interfere with your code.

jreis:
The code is a example of the mine, i can't publish all the code.

I don't see what the danger is of adding a temp variable. Or adding a closing '}' in the right place. The problem is that you might think that it is the serial communication that causes problems but it might actually be something else.

As we don't have any idea what temp is (and how many elements it contains), you might very well be printing some text that is outside the bounds of the array; and yes, that might be very slow.

I call millis because inicaly I used millis() but I needed more resolution and then just changed the function.

I have a thermistor to measure the temperature and then habe a vecti with temperatures corresponding to the sensor gives (0-1023).

I Have managed to solve the problem of sending but now need to read the information and read byte for byte I think it is not the best way , but also read the string takes a long time , what is the best way?