Need feedback on this code because I am trying to work a Stepper Motor 28BYJ-48 with this code

type or paste code here
                                                 Primary
// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementationis used in I2Cdev.h
#include "Wire.h"

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(0, 1); // CONNECT BT RX PIN TO ARDUINO 1 PIN | CONNECT BT TX PIN TO ARDUINO 0 PIN

#define OUTPUT_READABLE_YAWPITCHROLL
#define INTERRUPT_PIN 2  // use pin 2 on Arduino Uno & most boards
#define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6)

MPU6050 mpu;
bool blinkState = false;

// MPU control/status vars
bool dmpReady = false;  // set true if DMP init was successful
uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount;     // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer

// orientation/motion vars
Quaternion q;           // [w, x, y, z]         quaternion container
VectorFloat gravity;    // [x, y, z]            gravity vector
float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
float pitch = 0;
float roll = 0;
float yaw = 0;
int x;
int y;

// ================================================================
// ===               INTERRUPT DETECTION ROUTINE                ===
// ================================================================

volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
  mpuInterrupt = true;
}



// ================================================================
// ===                      INITIAL SETUP                       ===
// ================================================================

void setup() {
  // join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  Wire.begin();
  Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  Fastwire::setup(400, true);
#endif

  // initialize serial communication
  // (115200 chosen because it is required for Teapot Demo output, but it's
  // really up to you depending on your project)
  Serial.begin(115200);
  BTSerial.begin(9600);  // HC-05 default speed in AT command more
  
  while (!Serial); // wait for Leonardo enumeration, others continue immediately

  // NOTE: 8MHz or slower host processors, like the Teensy @ 3.3V or Arduino
  // Pro Mini running at 3.3V, cannot handle this baud rate reliably due to
  // the baud timing being too misaligned with processor ticks. You must use
  // 38400 or slower in these cases, or use some kind of external separate
  // crystal solution for the UART timer.

  // initialize device
  Serial.println(F("Initializing I2C devices..."));
  mpu.initialize();
  pinMode(INTERRUPT_PIN, INPUT);

  // verify connection
  Serial.println(F("Testing device connections..."));
  Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));

  // load and configure the DMP
  Serial.println(F("Initializing DMP..."));
  devStatus = mpu.dmpInitialize();

  // supply your own gyro offsets here, scaled for min sensitivity
  mpu.setXGyroOffset(126);
  mpu.setYGyroOffset(57);
  mpu.setZGyroOffset(-69);
  mpu.setZAccelOffset(1869); // 1688 factory default for my test chip

  // make sure it worked (returns 0 if so)
 if (devStatus == 0) {
    // turn on the DMP, now that it's ready
    Serial.println(F("Enabling DMP..."));
    mpu.setDMPEnabled(true);

    // enable Arduino interrupt detection
    Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
    attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), dmpDataReady, RISING);
    mpuIntStatus = mpu.getIntStatus();

    // set our DMP Ready flag so the main loop() function knows it's okay to use it
    Serial.println(F("DMP ready! Waiting for first interrupt..."));
    dmpReady = true;

    // get expected DMP packet size for later comparison
    packetSize = mpu.dmpGetFIFOPacketSize();
  } else {
    // ERROR!
    // 1 = initial memory load failed
    // 2 = DMP configuration updates failed
    // (if it's going to break, usually the code will be 1)
    Serial.print(F("DMP Initialization failed (code "));
    Serial.print(devStatus);
    Serial.println(F(")"));
  }

  // configure LED for output
  pinMode(LED_PIN, OUTPUT);
}



// ================================================================
// ===                    MAIN PROGRAM LOOP                     ===
// ================================================================

void loop() {
  // if programming failed, don't try to do anything
  if (!dmpReady) return;

  // wait for MPU interrupt or extra packet(s) available
  while (!mpuInterrupt && fifoCount < packetSize) {
    // other program behavior stuff here
    // .
    // .
    // .
    // if you are really paranoid you can frequently test in between other
    // stuff to see if mpuInterrupt is true, and if so, "break;" from the
    // while() loop to immediately process the MPU data
    // .
    // .
    // .
  }

  // reset interrupt flag and get INT_STATUS byte
  mpuInterrupt = false;
  mpuIntStatus = mpu.getIntStatus();

  // get current FIFO count
  fifoCount = mpu.getFIFOCount();

  // check for overflow (this should never happen unless our code is too inefficient)
  if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
    // reset so we can continue cleanly
    mpu.resetFIFO();
    Serial.println(F("FIFO overflow!"));

    // otherwise, check for DMP data ready interrupt (this should happen frequently)
  } else if (mpuIntStatus & 0x02) {
    // wait for correct available data length, should be a VERY short wait
    while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();

    // read a packet from FIFO
    mpu.getFIFOBytes(fifoBuffer, packetSize);

    // track FIFO count here in case there is > 1 packet available
    // (this lets us immediately read more without waiting for an interrupt)
    fifoCount -= packetSize;

#ifdef OUTPUT_READABLE_YAWPITCHROLL
    // display Euler angles in degrees
    mpu.dmpGetQuaternion(&q, fifoBuffer);
    mpu.dmpGetGravity(&gravity, &q);
    mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);

    yaw = ypr[0] * 180 / M_PI;
    pitch = ypr[1] * 180 / M_PI;
    roll = ypr[2] * 180 / M_PI;

    if (roll > -100 && roll < 100)
      x = map (roll, -100, 100, 0, 100);

    if (pitch > -100 && pitch < 100)
      y = map (pitch, -100, 100, 100, 200);

    Serial.print(x);
    Serial.print("\t");
    Serial.println(y);

    if((x>=45 && x<=55) && (y>=145 && y <=155)){
      BTSerial.write('S');
    }else if(x>60){
      BTSerial.write('R');
    }else if(x<40){
      BTSerial.write('L');
    }else if(y>160){
      BTSerial.write('B');
    }else if(y<140){
      BTSerial.write('F');
    }
#endif

    // blink LED to indicate activity
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);
  }
}

                                                                         Secondary

```#include <SoftwareSerial.h>
SoftwareSerial BTSerial(1, 0); // CONNECT BT RX PIN TO ARDUINO 1 PIN | CONNECT BT TX PIN TO ARDUINO 0 PIN
#include <Stepper.h>
#include "Wire.h"

char tiltDirection;

int STEPPER_Pin_1 = 8;     // IN1 on ULN2003 ==> Blue   on 28BYJ-48
int STEPPER_Pin_2 = 9;    // IN2 on ULN2004 ==> Pink   on 28BYJ-48
int STEPPER_Pin_3 = 10;    // IN3 on ULN2003 ==> Yellow on 28BYJ-48
int STEPPER_Pin_4 = 11;    // IN4 on ULN2003 ==> Orange on 28BYJ-48
const int stepsPerRevolution = 2038;
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);

void setup() {

  pinMode(STEPPER_Pin_1, OUTPUT);
  pinMode(STEPPER_Pin_2, OUTPUT);
  pinMode(STEPPER_Pin_3, OUTPUT);
  pinMode(STEPPER_Pin_4, OUTPUT);

  digitalWrite(STEPPER_Pin_1, LOW);
  digitalWrite(STEPPER_Pin_2, LOW);
  digitalWrite(STEPPER_Pin_3, LOW);
  digitalWrite(STEPPER_Pin_4, LOW);

  Serial.begin(115200);      // Serial communication is activated at 38400 baud/s.
  BTSerial.begin(9600);    // HC-05 default speed in AT command more
}
//Robot lk
void loop() {
  if (BTSerial.available()) {   // Witing for data incoming from the other XBee module
    tiltDirection = BTSerial.read();
    if(tiltDirection == 'F'){
      Serial.println(" reverse");
       reverse();
   }else if(tiltDirection == 'B'){
      Serial.println("Forward");
      forward();
    //}else if(tiltDirection == 'R'){
    // Serial.println("Left");
     // left();
 // }else if(tiltDirection == 'L'){
     // Serial.println("Right");
      //right();
    //}else if(tiltDirection == 'S'){
     // Serial.println("Stop");
    // stopCar();
    }
  }
}
//Robot lk
void forward()
{
  /*The pin numbers and high, low values might be different depending on your connections */
  digitalWrite(STEPPER_Pin_1, LOW);
  digitalWrite(STEPPER_Pin_2, HIGH);
  digitalWrite(STEPPER_Pin_3, LOW);
  digitalWrite(STEPPER_Pin_4, HIGH);
}
void reverse()
{
  /*The pin numbers and high, low values might be different depending on your connections */
  digitalWrite(STEPPER_Pin_1, HIGH);
 digitalWrite(STEPPER_Pin_2, LOW);
  digitalWrite(STEPPER_Pin_3, HIGH);
  digitalWrite(STEPPER_Pin_4, LOW);
}
//void right()
//{
  /*The pin numbers and high, low values might be different depending on your connections */
// digitalWrite(motorPin1, LOW);
  //digitalWrite(motorPin2, HIGH);
 // digitalWrite(motorPin3, HIGH);
 // digitalWrite(motorPin4, LOW);
//}
//void left()
//{
  /*The pin numbers and high, low values might be different depending on your connections */
  //digitalWrite(motorPin1, HIGH);
  //digitalWrite(motorPin2, LOW);
  //digitalWrite(motorPin3, LOW);
 //digitalWrite(motorPin4, HIGH);
//}

//void stopCar() {
  //digitalWrite(motorPin1, LOW);
  //digitalWrite(motorPin2, LOW);
  //digitalWrite(motorPin3, LOW);
  //digitalWrite(motorPin4, LOW);
//}

You don't say which Arduino you're using.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for advice on (nor for problems with) your project.

How does it not work as expected?

The way I am trying to use it is jack my two gimbals one primary and one secondary and if I was to spin the primary gimbal the secondary gimbal will react and spin. I am using an Arduino Uno, HC-05, MPU6050, Step Motor 28BYJ-48. My problem is I got this code I found online and it’s for controlling remote control car with your hand and I was wanting to modify it to work for my project but I am stuck on how to modify it. Any feedback will be appreciated.

The problem is when I go to upload this code above my Step Motor 28BYJ-48 will not respond. So that why I need help trying to figure out how to get my step motor 28BYJ-48 responding to my code.


                                                                  Primary

// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementationis used in I2Cdev.h
#include "Wire.h"

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(1, 0); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN

#define OUTPUT_READABLE_YAWPITCHROLL
#define INTERRUPT_PIN 2 // use pin 2 on Arduino Uno & most boards
#define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6)

MPU6050 mpu;
bool blinkState = false;

// MPU control/status vars
bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount; // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer

// orientation/motion vars
Quaternion q; // [w, x, y, z] quaternion container
VectorFloat gravity; // [x, y, z] gravity vector
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
float pitch = 0;
float roll = 0;
float yaw = 0;
int x;
int y;

// ================================================================
// === INTERRUPT DETECTION ROUTINE ===
// ================================================================

volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
mpuInterrupt = true;
}

// ================================================================
// === INITIAL SETUP ===
// ================================================================

void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif

// initialize serial communication
// (115200 chosen because it is required for Teapot Demo output, but it's
// really up to you depending on your project)
Serial.begin(115200);
BTSerial.begin(9600); // HC-05 default speed in AT command more

while (!Serial); // wait for Leonardo enumeration, others continue immediately

// NOTE: 8MHz or slower host processors, like the Teensy @ 3.3V or Arduino
// Pro Mini running at 3.3V, cannot handle this baud rate reliably due to
// the baud timing being too misaligned with processor ticks. You must use
// 38400 or slower in these cases, or use some kind of external separate
// crystal solution for the UART timer.

// initialize device
Serial.println(F("Initializing I2C devices..."));
mpu.initialize();
pinMode(INTERRUPT_PIN, INPUT);

// verify connection
Serial.println(F("Testing device connections..."));
Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));

// load and configure the DMP
Serial.println(F("Initializing DMP..."));
devStatus = mpu.dmpInitialize();

// supply your own gyro offsets here, scaled for min sensitivity
mpu.setXGyroOffset(126);
mpu.setYGyroOffset(57);
mpu.setZGyroOffset(-69);
mpu.setZAccelOffset(1869); // 1688 factory default for my test chip

// make sure it worked (returns 0 if so)
if (devStatus == 0) {
// turn on the DMP, now that it's ready
Serial.println(F("Enabling DMP..."));
mpu.setDMPEnabled(true);

// enable Arduino interrupt detection
Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), dmpDataReady, RISING);
mpuIntStatus = mpu.getIntStatus();

// set our DMP Ready flag so the main loop() function knows it's okay to use it
Serial.println(F("DMP ready! Waiting for first interrupt..."));
dmpReady = true;

// get expected DMP packet size for later comparison
packetSize = mpu.dmpGetFIFOPacketSize();

} else {
// ERROR!
// 1 = initial memory load failed
// 2 = DMP configuration updates failed
// (if it's going to break, usually the code will be 1)
Serial.print(F("DMP Initialization failed (code "));
Serial.print(devStatus);
Serial.println(F(")"));
}

// configure LED for output
pinMode(LED_PIN, OUTPUT);
}

// ================================================================
// === MAIN PROGRAM LOOP ===
// ================================================================

void loop() {
// if programming failed, don't try to do anything
if (!dmpReady) return;

// wait for MPU interrupt or extra packet(s) available
while (!mpuInterrupt && fifoCount < packetSize) {
// other program behavior stuff here
// .
// .
// .
// if you are really paranoid you can frequently test in between other
// stuff to see if mpuInterrupt is true, and if so, "break;" from the
// while() loop to immediately process the MPU data
// .
// .
// .
}

// reset interrupt flag and get INT_STATUS byte
mpuInterrupt = false;
mpuIntStatus = mpu.getIntStatus();

// get current FIFO count
fifoCount = mpu.getFIFOCount();

// check for overflow (this should never happen unless our code is too inefficient)
if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
// reset so we can continue cleanly
mpu.resetFIFO();
Serial.println(F("FIFO overflow!"));

// otherwise, check for DMP data ready interrupt (this should happen frequently)

} else if (mpuIntStatus & 0x02) {
// wait for correct available data length, should be a VERY short wait
while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();

// read a packet from FIFO
mpu.getFIFOBytes(fifoBuffer, packetSize);

// track FIFO count here in case there is > 1 packet available
// (this lets us immediately read more without waiting for an interrupt)
fifoCount -= packetSize;

#ifdef OUTPUT_READABLE_YAWPITCHROLL
// display Euler angles in degrees
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);

yaw = ypr[0] * 180 / M_PI;
pitch = ypr[1] * 180 / M_PI;
roll = ypr[2] * 180 / M_PI;

if (roll > -100 && roll < 100)
  x = map (roll, -100, 100, 0, 100);

if (pitch > -100 && pitch < 100)
  y = map (pitch, -100, 100, 100, 200);

Serial.print(x);
Serial.print("\t");
Serial.println(y);

if((x>=45 && x<=55) && (y>=145 && y <=155)){
  BTSerial.write('S');
}else if(x>60){
  BTSerial.write('R');
}else if(x<40){
  BTSerial.write('L');
}else if(y>160){
  BTSerial.write('B');
}else if(y<140){
  BTSerial.write('F');
}

#endif

// blink LED to indicate activity
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);

}
}
Secondary
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(11, 10); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN

char tiltDirection;
int motorInput1 = 5;
int motorInput2 = 6;
int motorInput3 = 7;
int motorInput4 = 8;

void setup() {

pinMode(motorInput1, OUTPUT);
pinMode(motorInput2, OUTPUT);
pinMode(motorInput3, OUTPUT);
pinMode(motorInput4, OUTPUT);

digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, LOW);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, LOW);

Serial.begin(115200); // Serial communication is activated at 38400 baud/s.
BTSerial.begin(9600); // HC-05 default speed in AT command more
}

void loop() {
if (BTSerial.available()) { // Witing for data incoming from the other XBee module
tiltDirection = BTSerial.read();
if(tiltDirection == 'F'){
Serial.println(" reverse");
reverse();
}else if(tiltDirection == 'B'){
Serial.println("Forward");
forward();
}else if(tiltDirection == 'R'){
Serial.println("Left");
left();
}else if(tiltDirection == 'L'){
Serial.println("Right");
right();
}else if(tiltDirection == 'S'){
Serial.println("Stop");
stopCar();
}
}
}

void forward()
{
/*The pin numbers and high, low values might be different depending on your connections */
digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, HIGH);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, HIGH);
}
void reverse()
{
/*The pin numbers and high, low values might be different depending on your connections */
digitalWrite(motorInput1, HIGH);
digitalWrite(motorInput2, LOW);
digitalWrite(motorInput3, HIGH);
digitalWrite(motorInput4, LOW);
}
void right()
{
/*The pin numbers and high, low values might be different depending on your connections */
digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, HIGH);
digitalWrite(motorInput3, HIGH);
digitalWrite(motorInput4, LOW);
}
void left()
{
/*The pin numbers and high, low values might be different depending on your connections */
digitalWrite(motorInput1, HIGH);
digitalWrite(motorInput2, LOW);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, HIGH);
}

void stopCar() {
digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, LOW);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, LOW);
}

I need help because for the code above is for a dc motor and I need it to work for stepper motor 28byj-48

Google is a good friend.

_gesture_control_car_receiver.ino (2.6 KB)
_gesture_control_car_transmitter.ino (7.3 KB)

I would like help with my code. I am trying to use a MPU 6050 to control a stepper motor 28BYJ-48 but also using 2 HC-05 of primary and the other secondary. It is for this project I am working on where I have two gimbals and the idea is when I spin one gimbal which has the mpu6050 and HC-05 primary attached it should send the data of movement to the secondary gimbal which has HC-05 secondary and a stepper motor 28BYJ-48 attached. The problem I am having is getting the Stepper motor to respond to my code and input. I am using two Arduino Uno also and I got the code from using hand gestures to control a rc car and I am trying to modify that dc motor receiver to work for a stepper motor.

Please post your code, in code tags.

This isn't an Introductory tutorial, and did not belong in that forum section.

How far have you proceeded? How is your setup behaving now?

I have not made much progress due to not getting my step motor 28BYJ-48 to respond to my secondary code.

Please post your code.

there is a big difference between driving a DC-motor and a stepper-motor.
A DC-motor just needs a single digitalWrite(directionPin,LOW/HIGH)
or maybe in looooong time-intervals (once every 0,5 seconds) a new
analogWrite(myPWM-Pin,dutyCycle);

A stepper-motor needs a new step-pulse with very precise timing once every few milliseconds
once evey 0,002 seconds which is 500 times per second.

depending on the stepper-library you are using this means your code has to execute
very fast or the code is blocking because the code is 101% busy with precise step-pulse creation.

Your smartphone has a 32bit 2 GHz microprocessor which is able to do 100000 times more calculations than an arduino Uno which is a 8 bit 0.016 GHz-microcontroller.
You are reading right: 0.016 GHz - Zero point zero one six GHz.

The etxremely tight and precise timing that a stepper-motor needs is a hard task for an arduino
If the arduino can concentrate on the step-pulse-creation it is doable.
If you want to do more than this things get tough and not everything will be combinable with the step-pulse-creation.

The MobaTools-library will enable to do the step-pulse-creation "in the backround" so that you can execute code that cares about receiving serial data and reading the MPU6050 sensor

This video explains how stepper-motors work

best regards Stefan

                                                       Secondary

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(11, 10); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN

char tiltDirection;
int motorInput1 = 5;
int motorInput2 = 6;
int motorInput3 = 7;
int motorInput4 = 8;

void setup() {

pinMode(motorInput1, OUTPUT);
pinMode(motorInput2, OUTPUT);
pinMode(motorInput3, OUTPUT);
pinMode(motorInput4, OUTPUT);

digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, LOW);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, LOW);

Serial.begin(115200); // Serial communication is activated at 38400 baud/s.
BTSerial.begin(9600); // HC-05 default speed in AT command more
}

void loop() {
if (BTSerial.available()) { // Witing for data incoming from the other XBee module
tiltDirection = BTSerial.read();
if(tiltDirection == 'F'){
Serial.println(" reverse");
reverse();
}else if(tiltDirection == 'B'){
Serial.println("Forward");
forward();
}else if(tiltDirection == 'R'){
Serial.println("Left");
left();
}else if(tiltDirection == 'L'){
Serial.println("Right");
right();
}else if(tiltDirection == 'S'){
Serial.println("Stop");
stopCar();
}
}
}

void forward()
{
/*The pin numbers and high, low values might be different depending on your connections */
digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, HIGH);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, HIGH);
}
void reverse()
{
/*The pin numbers and high, low values might be different depending on your connections */
digitalWrite(motorInput1, HIGH);
digitalWrite(motorInput2, LOW);
digitalWrite(motorInput3, HIGH);
digitalWrite(motorInput4, LOW);
}
void right()
{
/*The pin numbers and high, low values might be different depending on your connections */
digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, HIGH);
digitalWrite(motorInput3, HIGH);
digitalWrite(motorInput4, LOW);
}
void left()
{
/*The pin numbers and high, low values might be different depending on your connections */
digitalWrite(motorInput1, HIGH);
digitalWrite(motorInput2, LOW);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, HIGH);
}

void stopCar() {
digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, LOW);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, LOW);
}
Primary
// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementationis used in I2Cdev.h
#include "Wire.h"

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(1, 0); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN

#define OUTPUT_READABLE_YAWPITCHROLL
#define INTERRUPT_PIN 2 // use pin 2 on Arduino Uno & most boards
#define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6)

MPU6050 mpu;
bool blinkState = false;

// MPU control/status vars
bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount; // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer

// orientation/motion vars
Quaternion q; // [w, x, y, z] quaternion container
VectorFloat gravity; // [x, y, z] gravity vector
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
float pitch = 0;
float roll = 0;
float yaw = 0;
int x;
int y;

// ================================================================
// === INTERRUPT DETECTION ROUTINE ===
// ================================================================

volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
mpuInterrupt = true;
}

// ================================================================
// === INITIAL SETUP ===
// ================================================================

void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif

// initialize serial communication
// (115200 chosen because it is required for Teapot Demo output, but it's
// really up to you depending on your project)
Serial.begin(115200);
BTSerial.begin(9600); // HC-05 default speed in AT command more

while (!Serial); // wait for Leonardo enumeration, others continue immediately

// NOTE: 8MHz or slower host processors, like the Teensy @ 3.3V or Arduino
// Pro Mini running at 3.3V, cannot handle this baud rate reliably due to
// the baud timing being too misaligned with processor ticks. You must use
// 38400 or slower in these cases, or use some kind of external separate
// crystal solution for the UART timer.

// initialize device
Serial.println(F("Initializing I2C devices..."));
mpu.initialize();
pinMode(INTERRUPT_PIN, INPUT);

// verify connection
Serial.println(F("Testing device connections..."));
Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));

// load and configure the DMP
Serial.println(F("Initializing DMP..."));
devStatus = mpu.dmpInitialize();

// supply your own gyro offsets here, scaled for min sensitivity
mpu.setXGyroOffset(126);
mpu.setYGyroOffset(57);
mpu.setZGyroOffset(-69);
mpu.setZAccelOffset(1869); // 1688 factory default for my test chip

// make sure it worked (returns 0 if so)
if (devStatus == 0) {
// turn on the DMP, now that it's ready
Serial.println(F("Enabling DMP..."));
mpu.setDMPEnabled(true);

// enable Arduino interrupt detection
Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), dmpDataReady, RISING);
mpuIntStatus = mpu.getIntStatus();

// set our DMP Ready flag so the main loop() function knows it's okay to use it
Serial.println(F("DMP ready! Waiting for first interrupt..."));
dmpReady = true;

// get expected DMP packet size for later comparison
packetSize = mpu.dmpGetFIFOPacketSize();

} else {
// ERROR!
// 1 = initial memory load failed
// 2 = DMP configuration updates failed
// (if it's going to break, usually the code will be 1)
Serial.print(F("DMP Initialization failed (code "));
Serial.print(devStatus);
Serial.println(F(")"));
}

// configure LED for output
pinMode(LED_PIN, OUTPUT);
}

// ================================================================
// === MAIN PROGRAM LOOP ===
// ================================================================

void loop() {
// if programming failed, don't try to do anything
if (!dmpReady) return;

// wait for MPU interrupt or extra packet(s) available
while (!mpuInterrupt && fifoCount < packetSize) {
// other program behavior stuff here
// .
// .
// .
// if you are really paranoid you can frequently test in between other
// stuff to see if mpuInterrupt is true, and if so, "break;" from the
// while() loop to immediately process the MPU data
// .
// .
// .
}

// reset interrupt flag and get INT_STATUS byte
mpuInterrupt = false;
mpuIntStatus = mpu.getIntStatus();

// get current FIFO count
fifoCount = mpu.getFIFOCount();

// check for overflow (this should never happen unless our code is too inefficient)
if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
// reset so we can continue cleanly
mpu.resetFIFO();
Serial.println(F("FIFO overflow!"));

// otherwise, check for DMP data ready interrupt (this should happen frequently)

} else if (mpuIntStatus & 0x02) {
// wait for correct available data length, should be a VERY short wait
while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();

// read a packet from FIFO
mpu.getFIFOBytes(fifoBuffer, packetSize);

// track FIFO count here in case there is > 1 packet available
// (this lets us immediately read more without waiting for an interrupt)
fifoCount -= packetSize;

#ifdef OUTPUT_READABLE_YAWPITCHROLL
// display Euler angles in degrees
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);

yaw = ypr[0] * 180 / M_PI;
pitch = ypr[1] * 180 / M_PI;
roll = ypr[2] * 180 / M_PI;

if (roll > -100 && roll < 100)
  x = map (roll, -100, 100, 0, 100);

if (pitch > -100 && pitch < 100)
  y = map (pitch, -100, 100, 100, 200);

Serial.print(x);
Serial.print("\t");
Serial.println(y);

if((x>=45 && x<=55) && (y>=145 && y <=155)){
  BTSerial.write('S');
}else if(x>60){
  BTSerial.write('R');
}else if(x<40){
  BTSerial.write('L');
}else if(y>160){
  BTSerial.write('B');
}else if(y<140){
  BTSerial.write('F');
}

#endif

// blink LED to indicate activity
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);

}
}

Hi @logancb02
from just posting your code as plain text you are writing between the lines:

I (@logancb02 )did not read the forum-guide-lines.
And I (@logancb02) load all the work that
I (@logancb02 ) could do myself onto the shoulders of the community

the community shall do this work for me

  1. carefully mark and copy my code by hand (instead of a single mouse-click)
  2. analyse what might be wrong with my code
  3. speculate what my questions about the code may be and write down a 2 pages tutorial that answer my unasked questions
    or
  4. even better post ready to use code based on the raw and sketchy code that I have posted

you are the one who wants help
so you should do all the work that you can do yourself

which is

  • posting code as code-section
  • describing the behaviour of your code. Even if the behaviour is "nothing moves at all"
  • asking specific questions

best regards Stefan

you are welcome to the forum.
nevertheless if you want the sympathy of the users here you must post your code as a

code-section

This is a code-section which has a limited height making it much more convenient to scrol down.

this is a code-section
which makes it very easy to copy & paste your code
and the font of a code-section has a non-proportional font that shows all the indentions properly

use the code-button above the edit-field to create a code-section

best regards Stefan

1 Like
                                                    Secondary
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(11, 10); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN

char tiltDirection;
int motorInput1 = 5;
int motorInput2 = 6;
int motorInput3 = 7;
int motorInput4 = 8;

void setup() {

pinMode(motorInput1, OUTPUT);
pinMode(motorInput2, OUTPUT);
pinMode(motorInput3, OUTPUT);
pinMode(motorInput4, OUTPUT);

digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, LOW);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, LOW);

Serial.begin(115200); // Serial communication is activated at 38400 baud/s.
BTSerial.begin(9600); // HC-05 default speed in AT command more
}

void loop() {
if (BTSerial.available()) { // Witing for data incoming from the other XBee module
tiltDirection = BTSerial.read();
if(tiltDirection == 'F'){
Serial.println(" reverse");
reverse();
}else if(tiltDirection == 'B'){
Serial.println("Forward");
forward();
}else if(tiltDirection == 'R'){
Serial.println("Left");
left();
}else if(tiltDirection == 'L'){
Serial.println("Right");
right();
}else if(tiltDirection == 'S'){
Serial.println("Stop");
stopCar();
}
}
}

void forward()
{
/*The pin numbers and high, low values might be different depending on your connections */
digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, HIGH);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, HIGH);
}
void reverse()
{
/*The pin numbers and high, low values might be different depending on your connections */
digitalWrite(motorInput1, HIGH);
digitalWrite(motorInput2, LOW);
digitalWrite(motorInput3, HIGH);
digitalWrite(motorInput4, LOW);
}
void right()
{
/*The pin numbers and high, low values might be different depending on your connections */
digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, HIGH);
digitalWrite(motorInput3, HIGH);
digitalWrite(motorInput4, LOW);
}
void left()
{
/*The pin numbers and high, low values might be different depending on your connections */
digitalWrite(motorInput1, HIGH);
digitalWrite(motorInput2, LOW);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, HIGH);
}

void stopCar() {
digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, LOW);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, LOW);
}
Primary
// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementationis used in I2Cdev.h
#include "Wire.h"

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(1, 0); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN

#define OUTPUT_READABLE_YAWPITCHROLL
#define INTERRUPT_PIN 2 // use pin 2 on Arduino Uno & most boards
#define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6)

MPU6050 mpu;
bool blinkState = false;

// MPU control/status vars
bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount; // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer

// orientation/motion vars
Quaternion q; // [w, x, y, z] quaternion container
VectorFloat gravity; // [x, y, z] gravity vector
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
float pitch = 0;
float roll = 0;
float yaw = 0;
int x;
int y;

// ================================================================
// === INTERRUPT DETECTION ROUTINE ===
// ================================================================

volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
mpuInterrupt = true;
}

// ================================================================
// === INITIAL SETUP ===
// ================================================================

void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif

// initialize serial communication
// (115200 chosen because it is required for Teapot Demo output, but it's
// really up to you depending on your project)
Serial.begin(115200);
BTSerial.begin(9600); // HC-05 default speed in AT command more

while (!Serial); // wait for Leonardo enumeration, others continue immediately

// NOTE: 8MHz or slower host processors, like the Teensy @ 3.3V or Arduino
// Pro Mini running at 3.3V, cannot handle this baud rate reliably due to
// the baud timing being too misaligned with processor ticks. You must use
// 38400 or slower in these cases, or use some kind of external separate
// crystal solution for the UART timer.

// initialize device
Serial.println(F("Initializing I2C devices..."));
mpu.initialize();
pinMode(INTERRUPT_PIN, INPUT);

// verify connection
Serial.println(F("Testing device connections..."));
Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));

// load and configure the DMP
Serial.println(F("Initializing DMP..."));
devStatus = mpu.dmpInitialize();

// supply your own gyro offsets here, scaled for min sensitivity
mpu.setXGyroOffset(126);
mpu.setYGyroOffset(57);
mpu.setZGyroOffset(-69);
mpu.setZAccelOffset(1869); // 1688 factory default for my test chip

// make sure it worked (returns 0 if so)
if (devStatus == 0) {
// turn on the DMP, now that it's ready
Serial.println(F("Enabling DMP..."));
mpu.setDMPEnabled(true);

// enable Arduino interrupt detection
Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), dmpDataReady, RISING);
mpuIntStatus = mpu.getIntStatus();

// set our DMP Ready flag so the main loop() function knows it's okay to use it
Serial.println(F("DMP ready! Waiting for first interrupt..."));
dmpReady = true;

// get expected DMP packet size for later comparison
packetSize = mpu.dmpGetFIFOPacketSize();
} else {
// ERROR!
// 1 = initial memory load failed
// 2 = DMP configuration updates failed
// (if it's going to break, usually the code will be 1)
Serial.print(F("DMP Initialization failed (code "));
Serial.print(devStatus);
Serial.println(F(")"));
}

// configure LED for output
pinMode(LED_PIN, OUTPUT);
}

// ================================================================
// === MAIN PROGRAM LOOP ===
// ================================================================

void loop() {
// if programming failed, don't try to do anything
if (!dmpReady) return;

// wait for MPU interrupt or extra packet(s) available
while (!mpuInterrupt && fifoCount < packetSize) {
// other program behavior stuff here
// .
// .
// .
// if you are really paranoid you can frequently test in between other
// stuff to see if mpuInterrupt is true, and if so, "break;" from the
// while() loop to immediately process the MPU data
// .
// .
// .
}

// reset interrupt flag and get INT_STATUS byte
mpuInterrupt = false;
mpuIntStatus = mpu.getIntStatus();

// get current FIFO count
fifoCount = mpu.getFIFOCount();

// check for overflow (this should never happen unless our code is too inefficient)
if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
// reset so we can continue cleanly
mpu.resetFIFO();
Serial.println(F("FIFO overflow!"));

// otherwise, check for DMP data ready interrupt (this should happen frequently)
} else if (mpuIntStatus & 0x02) {
// wait for correct available data length, should be a VERY short wait
while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();

// read a packet from FIFO
mpu.getFIFOBytes(fifoBuffer, packetSize);

// track FIFO count here in case there is > 1 packet available
// (this lets us immediately read more without waiting for an interrupt)
fifoCount -= packetSize;
#ifdef OUTPUT_READABLE_YAWPITCHROLL
// display Euler angles in degrees
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);

yaw = ypr[0] * 180 / M_PI;
pitch = ypr[1] * 180 / M_PI;
roll = ypr[2] * 180 / M_PI;

if (roll > -100 && roll < 100)
  x = map (roll, -100, 100, 0, 100);

if (pitch > -100 && pitch < 100)
  y = map (pitch, -100, 100, 100, 200);

Serial.print(x);
Serial.print("\t");
Serial.println(y);

if((x>=45 && x<=55) && (y>=145 && y <=155)){
  BTSerial.write('S');
}else if(x>60){
  BTSerial.write('R');
}else if(x<40){
  BTSerial.write('L');
}else if(y>160){
  BTSerial.write('B');
}else if(y<140){
  BTSerial.write('F');
}
#endif

// blink LED to indicate activity
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
}
}

I just need help modifying this code for Dc motor and covert it so it works for a Stepper Motor 28BYJ-48

So what have you managed so far, and where are you stuck?