Okej so this is my first "real project" an i´m pretty much a beginner to both programming and arduino so bare with me ive compiled this of many different codes and tried to fill in the gaps with my own code
what im trying to do is a kind of course holding autpilot, im shure the code is filled with errors and many people would have made things differently but this is my approach and it works!
The only thing im having trouble with is that im trying to replace the "delay" in the left/right turn functions (turnright is corrected, turnleft is untouched) at the bottom of the code with millis, mainly because i want to be able to adjust the "lockedHeading" while the arduino is executing a turn, but since this is going in a boat i need the motor to "turn, wait, turn back" when the boat is off course, but if i do this with delay i cant adjust my heading until im back in "kurs OK" (course OK) but if i use millis() the code stops the turn function as soon as i get to the "kurs OK" area, meaning it can stop while leaving relay 1 active or really at any time in the function!
Is there a way to tell the code to execute the whole function beforing returning to loop, even if the course is back to its specified range?
appreciate the help, thanks!'
EDIT: had to remove parts of the code due to to many characters.. important parts still at the bottom
void loop() {
//----------------------------------------FishMode---------------------------------
// read the state of the switch into a local variable:
int reading = digitalRead(SW_pin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState2) {
buttonState2 = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState2 == HIGH) {
ledState = !ledState;
}
}
}
// set the LED:
fishModeState, ledState;
lastButtonState = reading;
if (ledState == HIGH){
Serial.println("FISK!");
if (millis() - lastUpdateTime3 > updateDelayFish) {
lcd.setCursor (5,0);
lcd.print("FISK!!");
lastUpdateTime3 = millis();
if ((millis() - lastUpdateTime4) > updateDelayFish2) {
lcd.clear();
lastUpdateTime4 = millis();
}
}
fishMode;
}
else {
//---------------------------------------kompass--------------------------------------------
qmc.read(&x, &y, &z,&azimuth);
//azimuth = qmc.azimuth(&y,&x);//you can get custom azimuth
buttonState = digitalRead (buttonPin);
Heading=(azimuth);
//---------------------------------------------------Switch joystick----------------------------------------
if ((millis() - lastUpdateTime5) > updateDelay) {
if (analogRead(Y_pin)<=100){storedHeading = (storedHeading - 5);}
lastUpdateTime5 = millis();
}
if ((millis() - lastUpdateTime6) > updateDelay) {
if (analogRead(Y_pin)>900){storedHeading = (storedHeading + 5);}
lastUpdateTime6 = millis();
}
if ((millis() - lastUpdateTime7) > updateDelay) {
if ((analogRead(X_pin)<=100) && (Deviation<MaxDeviation)) {Deviation = (Deviation + 2);}
lastUpdateTime7 = millis();
}
if ((millis() - lastUpdateTime8) > updateDelay){
if ((analogRead(X_pin)>900) && (Deviation>MinDeviation)){Deviation = (Deviation - 2);}
lastUpdateTime8 = millis();
}
void CorrectHeading(){ // Svänga höger eller vä?
if (Heading < storedHeading){
Serial.println("Vanster");
lcd.setCursor (0,1);
lcd.print (" ");
lcd.setCursor (0,1);
lcd.print("Correcting LEFT");
TurnLeft();
delay(20);
}
else{
Serial.println("Hoger");
lcd.setCursor (0,1);
lcd.print (" ");
lcd.setCursor (0,1);
lcd.print("Correcting RIGHT");
TurnRight();
delay(20);
}
}
void TurnRight(){ //Sväng höger
digitalWrite(relayPin1, HIGH);
if ((millis() - lastUpdateTime9) > turnTime) {
digitalWrite(relayPin1, LOW);
}
if ((millis() - lastUpdateTime9) > waitTime + turnTime) {
digitalWrite(relayPin2, HIGH);
if ((millis() - lastUpdateTime9) > turnBackTime + waitTime + turnTime) {
digitalWrite(relayPin2, LOW);
if ((millis() - lastUpdateTime9) > turnBackTime + (waitTime * 2) + turnTime) {
digitalWrite(relayPin2, LOW);
lastUpdateTime9 = millis();
}
}
}
}
void TurnLeft(){ //Sväng vänster
digitalWrite(relayPin2, HIGH);
delay(turnTime);
digitalWrite(relayPin2, LOW);
delay(waitTime);
digitalWrite(relayPin1, HIGH);
delay(turnBackTime);
digitalWrite(relayPin1, LOW);
delay(waitTime);
}
void fishMode(){