Arduino Uno - Serial stops printing, Arduino stops processing

Hi all,

I've been working on a small car for a project. I'm trying to make the car go a certain distance and stop when it has reached that, and have used the PID library to accomplish this. As part of the tuning process, I printed out the current PWM value (declared as variable power) and the current number of encoder Ticks. However, at random points, the Serial monitor stops printing and the Arduino Uno stops processing. There is still power though; for example, if the motors were currently at power of 255, they stay there even after the monitor freezes.

#include <PID_v1.h>
#include <RedBot.h>
#include <Adafruit_MotorShield.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *rightWheel = AFMS.getMotor(4);
Adafruit_DCMotor *leftWheel = AFMS.getMotor(3);

RedBotEncoder encoder = RedBotEncoder(A4, 10);
double numCounts = 0;
double goal = 6.5*PI;//goal distance in cm
double goalRevs = 5;
double goalCounts = goalRevs*192;
double power = 100;

boolean go = false;

int buttonPin = 9;//replace w/actual button pin
PID encoderPID = PID(&numCounts, &power, &goalCounts, 0.6, 0.9, 0.9, DIRECT);

void setup() {
  Serial.begin(9600);
  AFMS.begin();
  rightWheel->run(FORWARD);
  leftWheel->run(FORWARD);
  
  encoderPID.SetMode(AUTOMATIC);
 // encoderPID.SetOutputLimits(0, 255);
  //encoderPID.SetSampleTime(300);
  pinMode(buttonPin, INPUT_PULLUP);
  
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
   encoder.clearEnc(BOTH);
   
  /*Serial.println(F("???"));
  Serial.println(F("???"));
  Serial.println(F("???"));
 
  //pinMode(A2, INPUT);  
  Serial.println("Finished setup");*/
  
}


void loop() {
  //Serial.println(encoder.getTicks(LEFT));
 // Serial.println(digitalRead(buttonPin));
  if (digitalRead(buttonPin) == LOW)
    go = true;
  if (!go){
    //Serial.println(F("Waiting for Button"));
    //Serial.println(encoder.getTicks(LEFT));
    return;
  }
  else
  {
  driveDistance(goalCounts);
  }
  //numCounts = 0;
}

void driveDistance(double goalCounts)
{   
   //encoder.clearEnc(BOTH);
   if(numCounts<goalCounts)
   {
     encoderPID.Compute();
     //Serial.print(F("Traveling at: "));
     Serial.println(power);
     rightWheel->setSpeed(power);
     leftWheel->setSpeed(power);
     numCounts = (double) encoder.getTicks(LEFT);
     delay(210);
     Serial.println(numCounts);
     delay(210);
   }  
   else{
    rightWheel->setSpeed(0);
    leftWheel->setSpeed(0);
    //Serial.println(F("Finished"));
    exit(1);
   }

   
}

I researched this problem online, but didn't find a solution.

Here is what I tried:

  • wrapping my Serial.println literal string in F()
  • changing the baud rate
  • checking the free memory available using the MemoryFree Library (I deleted this from my code); it was always 13529 (I think)
  • removing all of the Serial prints
  • running on battery power
  • adding in delays

I think there could be a problem with the board. I'm using the Adafruit Motorshield V2.

I also attached a picture of the serial monitor frozen.

Please lmk what you think the problem is and how I could solve it.

Thanks!

Are the motors running off the same power supply as the Arduino? That's a no-no - motors and other inductive loads put a lot of noise onto the power supply, and that in turn can reset or hang the microcontroller. That's why you want to use a separate power supply for the motors. The adafruit shield probably provides a facility for that - consult their documentation for it (since it's adafruit, there actually is documentation)

DrAzzy:
Are the motors running off the same power supply as the Arduino? That's a no-no - motors and other inductive loads put a lot of noise onto the power supply, and that in turn can reset or hang the microcontroller. That's why you want to use a separate power supply for the motors. The adafruit shield probably provides a facility for that - consult their documentation for it (since it's adafruit, there actually is documentation)

According to Adafruit, I can use a single battery pack (7.2V) to run both the Arduino and DC motors. I tried that, but the same problem occurs (although I can't actually read data from the serial monitor). I'm going to get separate batteries for the motors but I'm not sure that's the problem since the documentation says using a single/double lead acid battery pack would work.

Here's the link to the adafruit documentation: Powering Motors | Adafruit Motor Shield V2 | Adafruit Learning System

Thanks for the quick reply!

[EDIT:] My battery just ran out of power, so I think that's probably what was causing the problem! Thank you for your help!!

    exit(1);

I wonder what verychowdery thinks that this does?

I don't care what Adafruit says (although Adafruit is usually correct), separate batteries (with a common ground) should be used.

A single battery pack may work, but I would try separate battery packs first.

Hi all,

I tried putting the Arduino and motors on separate power supplies, but the problem still persists. What else do you think could be responsible for this?