I've been working on a project to automatically level my food trailer for a month now. This has been my only experience with coding and the Arduino. I'm very well versed in automotive DC wiring so I had a little head start there.
I'm using 4 electric tongue jacks that I modified and welded to each corner of the trailer. I'm using an Arduino Uno to control them. I'm using a toy Ambulance with a breadboard taped to the top of it, containing LEDs and a Joystick. the LEDs take the place of the actual jacks in my proof of concept/trial and error machine.
I have have the majority of the program written and ready to go for my first iteration of the system. Some things will be manual, but for the most part its automatic.
The last piece of the program for this first iteration is being able to long press the joystick (3 seconds) to write "corrected" values to the EEPROM, and an even longer press of the joystick (6 seconds) to write those values to 0, resetting the system to "factory settings"
Basically this will let me tell the controller that I have found a new "Level" that I want it to adjust to each time it executes.
Below is the sketch for that to happen, without the EEPROM writes included. Instead, it should return an ascending counter in the serial monitor to show its working.... or if it's not.
I don't see the problem, mainly because the outcomes are intermittent, with no rhyme or reason as to why. (at least that I can tell.)
int NewLevelTime = 3000;
int ResetLevelToZeroTime = 6500;
int JoyPinCurrentState = HIGH;
int JoyPinPreviousState = HIGH;
unsigned long NewLevelPress = 0;
unsigned long ResetLevelToZeroPress = 0;
int debounceDelay = 50;
int JoyPin;
int New = 0;
int Zero = 0;
void setup() {
Serial.begin(9600);
pinMode(0, INPUT_PULLUP);
}
void loop() {
Serial.print("New Level Value Saves=");
Serial.print(New);
Serial.print(" ");
Serial.print("Factory Value Saves=");
Serial.println(Zero);
JoyPinCurrentState = digitalRead(JoyPin);
if (JoyPinCurrentState == LOW && JoyPinPreviousState == HIGH) {
NewLevelPress = millis();
ResetLevelToZeroPress = millis();
JoyPinPreviousState = JoyPinCurrentState;
} else if (JoyPinCurrentState == HIGH && JoyPinPreviousState == LOW) {
JoyPinPreviousState = JoyPinCurrentState;
}
if (JoyPinCurrentState == LOW) {
//Debounce the button
if ((millis() - NewLevelPress) > debounceDelay) {
//long press of JoyPin will set a new "Level" for the AutoLevel Function by
//writing new values for AcXoff,AcYoff,AcZoff into the EEPROM
if ((millis() - NewLevelPress) == NewLevelTime) { //5 Seconds
//Write new value for FrontBackOff into the EEPROM
//Serial.println("Write New Level Value");
New++;
}
if ((millis() - ResetLevelToZeroPress) == ResetLevelToZeroTime) { //10 Seconds
//Write 0 values for AcXoff,AcYoff,AcZoff into the EEPROM
//Serial.println("Write Level Values to 0");
Zero++;
}
}
}
}
Thanks in advance for the help with this, I appreciate all the answers I have already found on here!