This is the full Sketch. its still only going through once. i probably have a fundamental issue somewhere
//RV auto leeveling program
//designed to run linear actuators to help leveling process
#include "Wire.h"
#include <MPU6050_light.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const byte ROWS = 1; //1 rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
};
byte rowPins[ROWS] = { 9 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 5, 4 }; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
MPU6050 mpu(Wire);
unsigned long timer = 0;
int i = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
pinMode(10, OUTPUT); //led
//digitalWrite(10, LOW);
pinMode(11, OUTPUT); //Relay module 1
digitalWrite(11, HIGH);
pinMode(12, OUTPUT);
digitalWrite(12, HIGH); //Relay module 1
pinMode(8, OUTPUT);
digitalWrite(8, HIGH); //Relay module 2
pinMode(7, OUTPUT);
digitalWrite(7, HIGH); //Relay module 2
Serial.begin(9600);
lcd.init();
lcd.setBacklight(HIGH);
Wire.begin();
customKeypad.addEventListener(keypadEvent); // Add an event listener for this keypad
byte status = mpu.begin();
lcd.setCursor(0, 0);
lcd.print(F("MPU6050 status: "));
lcd.setCursor(0, 1);
lcd.print(status);
while (status != 0) {} // stop everything if could not connect to MPU6050
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Calculating offsets,"));
lcd.setCursor(0, 1);
lcd.println("Do not move MPU6050");
// mpu.upsideDownMounting = true; // uncomment this line if the MPU6050 is mounted upside-down
mpu.calcOffsets(); // gyro and accelero
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("Done!\n");
delay(1000);
lcd.clear();
}
void loop() {
int pitch = mpu.getAngleX();
char customKey = customKeypad.getKey(); //converts character to integer
mpu.update();
lcd.setCursor(0, 0);
lcd.print("1: Auto 2: RESET");
lcd.setCursor(0, 1);
lcd.print("Pitch:");
lcd.print(mpu.getAngleX());
if (customKey) { // Waiting for button press from keypad
Serial.println(customKey);
Serial.println(pitch);
}
}
void keypadEvent(KeypadEvent customKey) {
switch (customKeypad.getState()) {
case HOLD:
if (customKey == '2') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Packing Up");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("returning motors to up postion");
digitalWrite(12, LOW);
digitalWrite(8, LOW);
delay(15000);
digitalWrite(12, HIGH);
digitalWrite(8, HIGH);
lcd.setCursor(0, 0);
lcd.print("Motors returned to up position");
delay(2000);
}
break;
case PRESSED:
if (customKey == '1') {
int pitch = mpu.getAngleX();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Auto run");
delay(3000);
lcd.clear();
leveling();
delay(10000);
digitalWrite(10, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Auto run Complete");
lcd.setCursor(0, 1);
lcd.print("System Level");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Current Angle");
lcd.setCursor(0, 1);
lcd.print(mpu.getAngleX());
delay(10000);
lcd.clear();
}
break;
}
}
void leveling() { //runs relay module. this activates linear actuators to level trailer
mpu.update();
if ((millis() - timer) > 10) { // print data every 10ms
Serial.println("X : ");
Serial.println(mpu.getAngleX());
if (mpu.getAngleX() < -1 && mpu.getAngleX() > -15) digitalWrite(11, HIGH); //parameters for out of level 15 degree max limit for 20ft trailer
else {
digitalWrite(11, HIGH);
}
if (mpu.getAngleX() > 1 && mpu.getAngleX() < 15) { //parameters for out of level 15 degree max limit for 20ft trailer
digitalWrite(7, HIGH);
} else {
digitalWrite(7, HIGH);
}
if (mpu.getAngleX() < 1 && mpu.getAngleX() > -1) digitalWrite(10, HIGH); //parameters for being within level
else {
digitalWrite(10, HIGH);
}
timer = millis();
}
else {
leveling();
}
}