Arduino code problem

The Rocketry Forum

Help Support The Rocketry Forum:

This site may earn a commission from merchant affiliate links, including eBay, Amazon, and others.

kanfuti

New Member
Joined
Dec 14, 2022
Messages
3
Reaction score
0
Location
Earth
This is some test code, I am having some trouble getting the relay to turn on when the mpu6050 is tilted beyond +-15 degrees (90degrees start angle). Can anyone see the error? Or do I have to redo it all?(By the way I am not good at coding)



#include <Wire.h>

#include <MPU6050.h>

#include <Servo.h>

Servo sg901;

Servo sg902;

int servo_pin2 = 2;

int servo_pin3 = 3;

int RelayPin = 6;

MPU6050 sensor ;

int16_t ax, ay, az ;

int16_t gx, gy, gz ;

void setup ( )

{
pinMode(RelayPin, OUTPUT);
digitalWrite(RelayPin, HIGH);


sg901.attach ( servo_pin2 );

sg902.attach ( servo_pin3 );

Wire.begin ( );

Serial.begin (9600);

Serial.println ( "Initializing the sensor" );

sensor.initialize ( );

Serial.println (sensor.testConnection ( ) ? "Successfully Connected" : "Connection failed");

delay (1000);

Serial.println ( "Taking Values from the sensor" );

delay (1000);

}




void loop ( ) {

sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);
sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);
ax = map (ax, -17000, 17000, 0, 180) ;
ay = map (ay, -17000, 17000, 0, 180) ;

Serial.println (ax);
Serial.print(" ");
Serial.print(',');
Serial.println (ay);
Serial.print(',');
sg902.write (ay);
sg901.write (ax);

delay (10);

}
void systemabort(){
if(75 > ax > 105 || 75 > ay > 105){
digitalWrite(RelayPin, LOW);
}
}
 
Last edited:
Systemabort() is never called from loop()!

Put the if statement from systemabort() inside loop(). Get rid of systemabort()
 
Yea, I've never seen systemabort in Ardiuno code.

Some hints:
Write code to test each part, read sensor, control servo.

Have you done 'blink the LED' code? This is always a good place to start to ensure hardware works and you understand the code. I've been coding for over 40 years and still do the blink the LED as the first step when using a new processor chip, code language or development tools.

Do a simple toogle the relay on then off in loop() with a 1-2sec delay.
Use a Voltmeter (DMM) on the Ardiuno output pin. Does it turn on/off?
If no then disconnect the relay circuit and re-check. If it now turns on/off it is the relay circuit hanging the pin.
Are you driving the relay coil through a transistor or directly for the processor pin? The pin typically will not drive the relay coil.

Are you getting correct values from the MPU6050?
Have the loop() Only read and output (print) the accel x,y & z values. Do these make sense? Are the values what you expect with board flat on a table, turned 90 degrees?
What units are the accel values, m/s/s, G's, ft/s/s? Which do you want.

Can you control the servos?
In loop() send command to servo sg901.write(data).
do a delay of say 1000msec (1 sec) then add to data and send again. A delay is important since the servos do not react instantaneous, in factor they only get a new pulse every 20ms.
Does the servo respond as you expect?


I am not familiar with the MPU or Servo code library you are using so do not know what the MPU read returns or what the servo write expects. There should be some documentation on the libraries to tell you this.

I have found that Adafruit and Sparkfun have some very good tutorial on Arduino code and using senors & Servos.
Check them out.

In general the code looks ok and is how I do most Ardiuno code (with the systemabort() exception).
So probable just tweaks to the code.

Do you know about using comment to disable lines of code. This is a common method to remove code during testing and un-commenting later to ad back in. A comment is anything after the '//' .

Keep working on it and you will get it to work.
 
As a matter of good practice, you might want to consider using #define instead of a variable for values that never change, such as pin assignments.

Replace this...
Code:
int servo_pin2 = 2;

int servo_pin3 = 3;

int RelayPin = 6;

...with this...
Code:
#define servo_pin2 2

#define servo_pin3 3

#define RelayPin 6

Reference: https://www.arduino.cc/reference/en/language/structure/further-syntax/define/
 
Last edited:
Thanks for the reply @waltr , Have tested the code with LED blink and relay but each time I test trying to use the ax or ay ,it doesn't work. Also putting into loop() doesn't work, where should I put it. Using pin 6 on arduino directly working fine turning on relay. Only reading and outputting (ax,ay,az) doesn't work, I tried after reading your reply. The servos respond well but sometimes they bug out and just rotate. Sometimes (very often) everything just stops unexpectedly.Do you know any other codes, or ways to make it more reliable (for a launch). I copied ,systemabort(), from code I found somewhere else (here). How do you disable lines of code with comments, (I am new to this, don't know much). Thank you very much for this learnt a lot.
Also thanks to @A-ron , already changed that thanks to you.
This is for a pyro charge do you know any other ways to code or any other hardware (I don't have mosfets).
 
Yes, understand you are new to this but that is ok. It does take time to learn how to program and use electronics.

Comment example:
Code:
int ax;     // variable for acc x-axis
// int ay   // this line is commented out
the '//' after int ax starts a comment and is ignored by the complier.
the '//' before int ay comments out the entire line. In example code below I will use comments.

I also used "code tags" to post code. this is [ c o d e ] 'code you want to post' [ / c o d e ]
remove the spaces when using. I needed to put in the spaces so you can see the tags.

in this part of your code:
Code:
sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);
ax = map (ax, -17000, 17000, 0, 180) ;
ay = map (ay, -17000, 17000, 0, 180) ;
Serial.println (ax);
Serial.print(" ");
Serial.print(',');
Serial.println (ay);
Serial.print(',');
What values are you getting for ax & ay?
Also, comment out the 'map()' calls and see what the raw ax & ay values are.
or better do Serial.println (ax); before calling the map().

maybe
Code:
 wait(10)  // 10ms
is not long enough. As I posted, typical Servo signal is updated every 20ms. Try 100ms

Try ONLY writing known values to the servos in the loop() like this:
Code:
int ser_val = 0;
loop() {
    sg901.write(ser_val);
    Serial.println (ser_val);    // out value sent to servo
    ser_val++   // add 1 to value
    if (ser_val > 180) {
       ser_val = 0;   // ser value goes from 0 to 180 then back to 0
    }
    wait(500)    // 500 msec delay, slow enough to see values printed
}
If the servo acts bad at the end points, 0 & 180 then reduce the range to 45-90 and see if that helps.


Thanks for link to where you say the systemstateabout(). That is a function that set LEDs if the calculated Kalman angles exceed 40.
 
Last edited:
Ok the raw ax & ay values are from -17000 to +17000 so I think the map() reduces it to usable degrees so I will keep it from 0 to 180. As 0 to 360 it doesn't work after +180, but 0 to 180 it good enough for me.
Code:
void loop ( ) {
 
sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);
sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);
ax = map (ax, -17000, 17000, 0, 180) ;
ay = map (ay, -17000, 17000, 0, 180) ;

Serial.println (ax);
Serial.print("          ");
Serial.print(',');
Serial.println (ay);
sg902.write (ay);
sg901.write (ax);

delay (60);

}

The reliability is much better probably because of the increase delay time (60 from 10) not overloading the servo. Thanks will send feedback if I get it to work.
 
1. If you are not good at coding (your words), why are you even attempting to do what you are trying to do?
2. What are you doing where your start angle is 90°? That is horizontal........
3. You just created an account yesterday to ask these questions......

What exactly are you doing???
 
Back
Top