MPU6050 Accelerometer Gyroscope to toggle a digital output

I am reading a variable analog input from a 0-5kohm potentiometer. This input is being converted and passed as a PPM signal to an electronic speed control. The MPU6050 calculated pitch and roll and when it passes 45 degrees on either axis, drives an LED indication. This all works fine. However, I would also like the roll and pitch indication to stop the PPM output. The PPM drives two motors and I am trying to cut power to them when the mechanism flips or rolls. Is this even possible?

This is the code that reads the analog and writes the digital so anyone who can help knows how the arduino is interpreting the signals:

const uint8_t VescOutputPin = 5; //PPM Output
const uint8_t PotentiometerPin = A0; //Analog Input
Servo esc;

//Establishing Connection to VESC//

esc.attach(VescOutputPin);
esc.writeMicroseconds(1500);

esc.writeMicroseconds(map(analogRead(PotentiometerPin), 0 , 1023, 1000, 2000));

And, I guess I’ll just attach the full code if you’re interested

Propulsion_System_PPM.ino (1.82 KB)

You could have it write a PPM signal to the esc if the roll/pitch is less than 45deg and greater than -45deg:

if(pitch <= 45 && pitch >= -45 && roll <= 45 && roll >= -45)
{
  esc.writeMicroseconds(map(analogRead(PotentiometerPin),0,1023,1000,2000));
}else{
  esc.detach();   // or write 1000ms - this will prevent you picking up whatever and causing a motor to start up
}

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

If i esc.detach, wouldn't I then need to attach it again when the roll/pitch is less than 45 degrees?

sp. “PWM”

I wish it were that way, but the VESC program calls it PPM. State of the world I guess lol

Then the “VESC program” is also wrong.

Yes, you would need to reattach. My ESC work is all with propellers that can remove fingers. So when I need the motor to stop, I make sure I can’t easily start it again by accident. It’s definitely not necessary and you can just set it to stop moving instead of detaching.

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