Hi.
I have done my sketch and it looks messy compared to others I have seen.
What can I do to improve the sketch is my question.
The project description:- Balancing robot controller using a 5D Rocker Joystick Independent Keyboard and a MPU6050. So I use the "Set" and the "Reset" buttons to change between control methods.
The sketch is working. But I am open to a better method of doing that as well.
Sketch below.
The Print to Serial monitor statement's are for debugging only and will be removed before completion of project.
Regards Antony.
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
const int up = 6;
const int dwn = 7;
const int let = 8;
const int rht = 9;
const int mid = 10;
const int set = 11;
const int rst = 12;
int buttonState = 0;
int tilt_angle = 15;
int Accelerometer = 0;
byte send_byte;
void setup()
{
while (!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
Serial.begin(38400);// 57600
Serial.print(" Starting up");
pinMode(up, INPUT_PULLUP);// on D6
pinMode(dwn, INPUT_PULLUP);// on D7
pinMode(let, INPUT_PULLUP);// on D8
pinMode(rht, INPUT_PULLUP);// on D9
pinMode(mid, INPUT_PULLUP);// on 10
pinMode(set, INPUT_PULLUP);// on 11
pinMode(rst, INPUT_PULLUP);// on 12
}
void loop()
{
send_byte = B00000000;
if (Accelerometer == 1) {
tilt_messure();
}
buttonState = digitalRead(let); // white wire to D9
if (buttonState == LOW) {
send_byte |= B00000001; Serial.print(" left"); // Left Turn
}
buttonState = digitalRead(rht); // yellow wire to D8
if (buttonState == LOW) {
send_byte |= B00000010; Serial.print(" right"); // Right Turn
}
buttonState = digitalRead(dwn); // Blue wire to D7
if (buttonState == LOW) {
send_byte |= B00000100; Serial.print(" back"); // backwards
}
buttonState = digitalRead(up); // red wire to D6
if (buttonState == LOW) {
send_byte |= B00001000; Serial.print(" forward"); // forward
}
buttonState = digitalRead(set); // yellow wire to D11
if (buttonState == LOW) {
Serial.print(" Jumping to Acc");
Accelerometer = 1;
tilt_messure();
}
/* Add thease buttons as wanted
buttonState = digitalRead(mid); // green wire to D10
if (buttonState == LOW) {
send_byte |= B00001000; // middle button
}
*/
Serial.print(" .. "); Serial.println(send_byte);
if (send_byte)Serial.print((char)send_byte);
delay(40);
}
void tilt_messure() {
Vector normAccel = mpu.readNormalizeAccel();
int pitch = -(atan2(normAccel.XAxis, sqrt(normAccel.YAxis * normAccel.YAxis + normAccel.ZAxis * normAccel.ZAxis)) * 180.0) / M_PI;
int roll = (atan2(normAccel.YAxis, normAccel.ZAxis) * 180.0) / M_PI;
// Serial.print(" Pitch = ");
// Serial.print(pitch);
// Serial.print(" Roll = ");
//Serial.print(roll);
Serial.println();
if (pitch <= -tilt_angle) {
send_byte |= B00000001;
Serial.print(" left");// Left Turn
}
if (pitch >= tilt_angle) {
send_byte |= B00000010;
Serial.print(" right"); // Right Turn
}
if (roll <= -tilt_angle) {
send_byte |= B00000100;
Serial.print(" back"); // backwards
}
if (roll >= tilt_angle) {
send_byte |= B00001000;
Serial.print(" forward"); // forward
}
Serial.print(send_byte);
delay(10);
buttonState = digitalRead(rst); // Green wire to D12
if (buttonState == LOW) {
Accelerometer = 0;
}
}