I am trying to make an observatory through 2 GY-521 gyroscopes and using the outputs I have to make a motor move, but I have a problem.
When I move one of the two gyroscopes the outputs change, but then as soon as I leave it it returns to the initial value, the data I see via an ESP8266 on an internet site, this is the code:
#include <Wire.h>
#include "WiFiEsp.h"
// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(18, 19); // RX, TX
#endif
char ssid[] = "Vodafone-A61356555"; // your network SSID (name)
char pass[] = "YgtHnkJHNpFAcPfH"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
int reqCount = 0; // number of requests received
WiFiEspServer server(80);
int MPU = 0x69;
int MPU1 = 0x69;
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
int16_t AcX1, AcY1, AcZ1, Tmp1, GyX1, GyY1, GyZ1;
int gyro_state = 2;
int gyro_state_1 = 3;
int RED_LED = 8;
int GREEN_LED = 9;
void setup(){
pinMode(gyro_state, OUTPUT);
pinMode(gyro_state_1, OUTPUT);
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
digitalWrite(gyro_state, HIGH);
digitalWrite(gyro_state_1, HIGH);
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(115200);
// initialize serial for ESP module
Serial1.begin(9600);
// initialize ESP module
WiFi.init(&Serial1);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
Serial.println("You're connected to the network");
printWifiStatus();
// start the web server on port 80
server.begin();
}
void loop() {
// listen for incoming clients
WiFiEspClient client = server.available();
if (client) {
Serial.println("New client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
Serial.println("Sending response");
// send a standard http response header
// use \r\n instead of many println statements to speedup data send
client.print(
"HTTP/1.1 200 OK\r\n"
//"Content-Type: text/html\r\n"
"Connection: close\r\n" // the connection will be closed after completion of the response
"Refresh: 5\r\n"
"\r\n");
client.print("<!DOCTYPE HTML>\r\n");
client.print("<html>\r\n");
client.print("<br>\r\n");
client.print("Accelerometro Telescopio X: ");
client.print(AcX);
client.print(" Accelerometro Cupola X: ");
client.print(AcX1);
client.print("<br>\r\n");
client.print("<br>\r\n");
client.print("Accelerometro Telescopio Y: ");
client.print(AcY);
client.print(" Accelerometro Cupola Y: ");
client.print(AcY1);
client.print("<br>\r\n");
client.print("<br>\r\n");
client.print("Accelerometro Telescopio Z: ");
client.print(AcZ);
client.print(" Accelerometro Cupola Z: ");
client.print(AcZ1);
client.print("<br>\r\n");
client.print("<br>\r\n");
client.print("Giroscopio Telescopio X: ");
client.print(GyX);
client.print(" Giroscopio Cupola X: ");
client.print(GyX1);
client.print("<br>\r\n");
client.print("<br>\r\n");
client.print("Giroscopio Telescopio Y: ");
client.print(GyY);
client.print(" Giroscopio Cupola Y: ");
client.print(GyY1);
client.print("<br>\r\n");
client.print("<br>\r\n");
client.print("Giroscopio Telescopio Z: ");
client.print(GyZ);
client.print(" Giroscopio Cupola Z: ");
client.print(GyZ1);
client.print("<br>\r\n");
client.print("<br>\r\n");
client.print("Temperatura Telescopio: ");
client.print(Tmp);
client.print(" Temperatura Cupola: ");
client.print(Tmp1);
client.print("<br>\r\n");
client.print("<br>\r\n");
client.print("</html>\r\n");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(100);
// close the connection:
client.stop();
Serial.println("Client disconnected");
}
digitalWrite(gyro_state, HIGH);
digitalWrite(gyro_state_1, LOW);
do {
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU, 14, true); // request a total of 14 registers
AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
Tmp = Tmp / 340 + 36.53;
Serial.println("First Gyro");
Serial.println(GyZ);
digitalWrite(gyro_state_1, HIGH);
digitalWrite(gyro_state, LOW);
delay(1000);
} while(digitalRead(gyro_state)== HIGH);
do {
Wire.beginTransmission(MPU1);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU1, 14, true); // request a total of 14 registers
AcX1 = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY1 = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ1 = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp1 = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX1 = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY1 = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ1 = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
Tmp1 = Tmp1 / 340 + 36.53;
Serial.println("Second Gyro");
Serial.println(Gy1);
digitalWrite(gyro_state, HIGH);
digitalWrite(gyro_state_1, LOW);
delay(1000);
} while(digitalRead(gyro_state_1) == HIGH);
if (GyY > GyY1) {
digitalWrite(9, HIGH);
digitalWrite(8, LOW);
}
else if (GyY < GyY1) {
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print where to go in the browser
Serial.println();
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
Serial.println();
}
Output:
First Gyro
1503
Second Gyro
3730
First Gyro
1558
Second Gyro
-162
First Gyro
1498
Second Gyro
-164
Can anyone explain to me why once I stop moving the gyroscope, the value returns to the initial position? And if it is possible to convert to degrees?