TX serial is not working

Hello guys,
I’m facing this problem: after about 10 minutes the tx serial communication stop working, the led stop blinking.
I’m receiving distance from HCSR04 for moving 25 servos, I want to send “clacDist” (the distance calculated by HCSR04) and “variabilePatterns” (a random number) to the serial port, but after 10 minutes it’s freezed on one clacDis and one variabilePatterns and the TX led stop blinking.

#include <Servo.h>

#include <NewPing.h>

Servo servo01;
Servo servo02;
Servo servo03;
Servo servo04;
Servo servo05;
Servo servo06;
Servo servo07;
Servo servo08;
Servo servo09;
Servo servo10;
Servo servo11;
Servo servo12;
Servo servo13;
Servo servo14;
Servo servo15;
Servo servo16;
Servo servo17;
Servo servo18;
Servo servo19;
Servo servo20;
Servo servo21;
Servo servo22;
Servo servo23;
Servo servo24;
Servo servo25;

const int t=200;
const int p=100;

float veloValue=0;

int variabilePatterns=0;
int posizione= 0;
int posizione2=0;
int intervallo=6;
int velocita= 20;
int velocitaIniz= 10;
const int start= 0;
const int maxPos=170;
const int minPos=0;
const int kP2= 20;
const int minDist=30;
const int maxDist= 170;
const int halfDist= 100;
const int kxservo05=15;

int calcDist=0;
int patternDelay= 1000;

const int triggerPin =13;
const int echoPin =12;
int distanzaprecedente=0;
int distanzacorrente=0;
int kDist=0;

NewPing temporilevato (triggerPin,echoPin);

void setup() {

  pinMode (triggerPin,OUTPUT);
  pinMode (echoPin,INPUT);
  delay (5000);
 Serial.begin (57600);
  
}

void loop() {

calcDist= calcoloDistanza(distanzacorrente,0,distanzaprecedente,temporilevato);

if (calcDist<minDist) {
  variabilePatterns=4;
}

variabilePatterns= random(4);
Serial.print (variabilePatterns);
Serial.print (" ");
Serial.println (calcDist);

if (calcDist>maxDist ) {
  variabilePatterns=10;
  delay (t);
  servo01.detach();
  servo02.detach();
  servo03.detach();
  servo04.detach();
  servo05.detach();
  servo06.detach();
  servo07.detach();
  servo08.detach();
  servo09.detach();
  servo10.detach();
  servo11.detach();
  servo12.detach();
  servo13.detach();
  servo14.detach();
  servo15.detach();
  servo16.detach();
  servo17.detach();
  servo18.detach();
  servo19.detach();
  servo20.detach();
  servo21.detach();
  servo22.detach();
  servo23.detach();
  servo24.detach();
  servo25.detach();
  delay (1000);
}

else {
  delay(t);
  servo01.attach(49);
  servo02.attach(41);
  servo03.attach(50);
  servo04.attach(45);
  servo05.attach(52);
  servo06.attach(48);
  servo07.attach(39);
  servo08.attach(36);
  servo09.attach(53);
  servo10.attach(44);
  servo11.attach(26);
  servo12.attach(30);
  servo13.attach(32);
  servo14.attach(42);
  servo15.attach(43);
  servo16.attach(29);
  servo17.attach(34);
  servo18.attach(35);
  servo19.attach(38);
  servo20.attach(5);
  servo21.attach(4);
  servo22.attach(33);
  servo23.attach(24);
  servo24.attach(37);
  servo25.attach(27);
  delay (t);
}

if (calcDist< minDist) {
 variabilePatterns=4;
}


// PATTERN 1

if (variabilePatterns==0) {

intervallo=velocitaservo(calcDist,0,minDist,maxDist,halfDist);
while (posizione <= maxPos) {
  posizione= posizione+intervallo;
  servo01.write (posizione);
  intervallo=velocitaservo(calcDist,0,minDist,maxDist,halfDist);
  delay (velocita);
  
}
}
}
int calcoloDistanza (int distance,int distances, int previousdistance,NewPing timing) {
 distance= timing.ping_median(30); 
 distances= timing.convert_cm(distance);
 //previousdistance=distances;
//
// if ((distances-previousdistance)>20) {
//  distances=previousdistance;
// }
// else {
//  previousdistance=distances;
// }
 delay (50);
 return (distances);
}

int velocitaservo (int calcolodistanzasensore,int stepsservomotore,int distanzaminima, int distanzamassima, int distanzaintermedia) {
  
  if (calcolodistanzasensore<distanzaintermedia && calcolodistanzasensore>distanzaminima ) {
  stepsservomotore=20;
}

if (calcolodistanzasensore>distanzaintermedia && calcolodistanzasensore<distanzamassima ) {
  stepsservomotore=12;
}

return stepsservomotore;
}

Do you know why is it happening?

Thank

Albi

It begin to work again when I open the serial monitor or load the sketch again, but when it works for 10-15 mins it'll block, what can I do to solve it?

Thanks

Albi

Not the cause of your problem but

  if (calcDist < minDist)
  {
    variabilePatterns = 4;
  }
  variabilePatterns = random(4);

Won’t variabilePatterns always be random(4) in this situation ?

This, and other parts of your program

    servo01.attach(49);
    servo02.attach(41);
    servo03.attach(50);
    servo04.attach(45);
    servo05.attach(52);
    servo06.attach(48);
    servo07.attach(39);
    servo08.attach(36);
    servo09.attach(53);
    servo10.attach(44);
    servo11.attach(26);
    servo12.attach(30);
    servo13.attach(32);
    servo14.attach(42);
    servo15.attach(43);
    servo16.attach(29);
    servo17.attach(34);
    servo18.attach(35);
    servo19.attach(38);
    servo20.attach(5);
    servo21.attach(4);
    servo22.attach(33);
    servo23.attach(24);
    servo24.attach(37);
    servo25.attach(27);

strongly suggest that you should use arrays in your program.

Why do you only ever write to servo01 and not the others and why attach/detach them withing the program ?

Hello UKHeliBob thanks for your kind answer,

Won't variabilePatterns always be random(4) in this situation ?

Yes, it's an error of mine, I will correct it, thanks

Why do you only ever write to servo01 and not the others and why attach/detach them withing the program ?

Because I can't copy all the code, it's too long, and yes, I'll use arrays.
Have you got an idea why there is the serial problem?

Thanks for your attention

Albi

Presumably you're providing enough external current to the servos (as we've discussed before) and this isn't some crazy power-related thing?

Hello kenwood120, thanks again for your past help,

Presumably you’re providing enough external current to the servos

The servos are working fine and Arduino also, until 10 minutes more or less, i think it could be also an auto-reset of Arduino MEGA 2560, could it be? All is freezed after 10 minutes, TX is not blinking anymore.

How can I fix it?

Thanks

Albi

Albi_54:
Hello kenwood120, thanks again for your past help,

The servos are working fine and Arduino also, until 10 minutes more or less, i think it could be also an auto-reset of Arduino MEGA 2560,

You didn’t answer my question :wink:

Is the servo power external?

I asked that because I was also thinking of some kind of reset if the Arduino’s getting hot due to powering the servos, which may be “working fine” until it resets due to heat.

If the servo power is external- which it should be but you haven’t confirmed- then that’s one less thing to consider for the problem you’re having.

Thanks kenwoods120,

yes, the servos are powered externally, the Mega is USB powered and the GND is connected to negative power supply pole.

Thanks for your time

Albi

If your code is too long you can post your .ino file as an attachment.

Without seeing the whole program it is difficult to know what might be causing the problem.

...R

Hello Robin2,

sure, I’ll attach the .ino with this post.

Thanks

Albi

prv_d.ino (17.3 KB)

I can’t make sense of all that. You need to learn to use arrays and that will greatly shorten your program.

Also, don’t use WHILE because it blocks other things from happening. Allow loop() to do the repetition - that’s what it is there for.

The huge amount of code in loop() should be broken out into a number of functions. That should also reduce the length of the program by eliminating repetition. Have a look at Planning and Implementing a Program. Note how the code in loop() is very short and also provides a nice overview of that the program does.

I wonder if one of your WHILE loops never terminates?

…R