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