Looped Code Randomly Stops Running After Short Amount of Time

Hello!

I am currently programming a code where an angle sensor (the MPU 6050) obtains angle readings and a stepper motor rotates to control the tilt of the platform so that it eventually comes within range of a set angle. However, the program runs for a very short period of time until it suddenly stops (despite it being looped for an infinite amount of time).

I've made it so that the stepper motor moves 20 steps before checking the angle again and then moving another 20 steps in the direction needed.

Would anyone know why the code randomly stops? Could it be that the Arduino board is receiving too much data and is overflowing?
Cheers.

/*Feedback control of the angle

============================================================
 *   A4988 stepper motor driver (connected to Arduino):
 *      MS1:H, MS2:H, MS3:L, thus 8th-step resolution
 *      MS1: 5V on Arduino (wired to POSITIVE on breadboard)
 *      MS2: 5V on Arduino (wired to POSITIVE on breadboard)
 *      MS3: GND on Arduino (wired to NEGATIVE  on breadboard)
 *      RESET: reset
 *      SLEEP: sleep
 *      STEP: D13
 *      DIR: D12
 *      GND: GND on Arduino (wired to NEGATIVE on breadboard)
 *      VDD: 5V on Arduino (wired to POSITIVE on breadboard)
 *      B1: black wire of stepper motor
 *      A1: green wire of stepper motor
 *      A2: red wire of stepper motor
 *      B2: blue wire of stepper motor
 *      GND (motor supply): GND of motor power supply (wall charger)
 *      VMOT: POSITIVE of motor power supply (wall charger)
 *      
 *    
 *    NEMA-17 17HS15-1684S-PG27 stepper motor (connected to stepper motor driver):
 *      Black wire: B1
 *      Green wire: A1
 *      Red wire: A2
 *      Blue wire: B2
 *      
 *   
 *    ELECTROLYTIC CAPACITOR (>47 uF) (connected to stepper motor driver):
 *      POSITIVE: VMOT
 *      NEGATIVE: GND 
 *      
 *   Accelerometer
 *      //VCC  -  5V
 *      /GND  -  GND
 *      //SDA  -  A4
 *      //SCL  -  A5
 *      //INT - port-2
 *   
 *   LCD Display
 *      * LCD RS pin to digital pin 11
        * LCD Enable pin to digital pin 10
        * LCD D4 pin to digital pin 4
        * LCD D5 pin to digital pin 5
        * LCD D6 pin to digital pin 6
        * LCD D7 pin to digital pin 7
        * LCD R/W pin to ground
        * LCD VSS pin to ground
        * LCD VCC pin to 5V
        * 10K resistor:
        * ends to +5V and ground
        * wiper to LCD VO pin (pin 3)
  */





//MPU 6050===========================
#include "Wire.h"
#include <MPU6050_light.h>

MPU6050 mpu(Wire);
unsigned long timer = 0;



//Stepper Motor======================
#define dir  12   // "Direction" pin of driver.  Defines direction of motor rotation.
#define pul  13   // 'STEP' pin of driver

//Define variables values
float setPitch = 10.5, livePitch; //set pitch and measured pitch



//LCD Display========================
#include <LiquidCrystal.h>
const int rs = 11, en = 10, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);



//==============================================================================================
//                                SETUP
//==============================================================================================
void setup() {
  Serial.begin(9600);
  Wire.begin();

  //MPU 6050=========================
  byte status = mpu.begin();
  Serial.print(F("MPU6050 status: "));
  Serial.println(status);
  while(status!=0){ } // stop everything if could not connect to MPU6050
  
//  Serial.println(F("Calculating offsets, do not move MPU6050"));
//  delay(1000);
//  // mpu.upsideDownMounting = true; // uncomment this line if the MPU6050 is mounted upside-down
//  mpu.calcOffsets(); // gyro and accelero
//  Serial.println("Done!\n");

   
  //LCD Display=======================
  lcd.begin(16, 2);
  lcd.print("Current Angle:");

  //Stepper Motor=====================
  pinMode(dir, OUTPUT);
  pinMode(pul, OUTPUT);
}



//==============================================================================================
//                                LOOP
//==============================================================================================
void loop() {
  mpu.update();
  
  livePitch=mpu.getAngleY();

    if ((livePitch-setPitch) > 0.1){
       for(int x=0;x<20;x++){
         digitalWrite(dir, HIGH);
         digitalWrite(pul, HIGH);
         delayMicroseconds(500);
         digitalWrite(pul, LOW);
         delayMicroseconds(500);
       }
//      digitalWrite(dir, HIGH);
//      digitalWrite(pul, HIGH);
//      delayMicroseconds(1000);
//      digitalWrite(pul, LOW);

      //Serial.println("board counterclockwise");
  }
  
    else if ((livePitch-setPitch)<-0.1){
        for(int x=0;x<20;x++){
         digitalWrite(dir, LOW);
         digitalWrite(pul, HIGH);
         delayMicroseconds(500);
         digitalWrite(pul, LOW);
         delayMicroseconds(500);
       }
//      digitalWrite(dir, LOW);
//      digitalWrite(pul, HIGH);
//      delayMicroseconds(1000);
//      digitalWrite(pul, LOW);

      //Serial.println("board clockwise");
  }
  
    else{
      digitalWrite(pul,LOW);
      delayMicroseconds(500);
  }

  //Serial.print("X : ");
  //Serial.print(mpu.getAngleX());
  lcd.setCursor(0, 1);
  lcd.print(livePitch);
  Serial.print("\n");
  Serial.print(livePitch);
}

Probably not, because so little information is presented. It is likely that the problem is with the circuitry that you forgot to describe.

Please post links to all the other components, along with a wiring diagram (hand drawn is preferred) with all pins, connections and components clearly labeled. A photo of the setup is usually helpful.

The "How to get the best out of this forum" post gives instructions on what is needed to help forum members help you.

1 Like

Inadequate power to the Arduino when the motors are working, resulting in brown-out.

1 Like

Post your annotated schematic, be sure to show all connections, power, ground, power sources and any other components such as resistors, capacitors, etc. Links to each of the hardware items helps if it gives technical information. Be sure to link the motors and anything else that uses more then 10mA to the appropriate technical specifications. Doing this can save you weeks.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.