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);
}