If statements interacting with each other.

So I have two specific if statements that make sure to turn a motor at a specific time, besides that I want to have a flip switch that can turn the motor either way by flipping it.

My problem with my current code is that as soon as the motor is activated by the specific time my if statement further down flips the motor back. I have a really hard time wrapping my head around this and coming up with a solution to avoid this.

So currently at exactly 01:00:00, my code makes my motor spin, but for some reason, my motor spins back again. I believe it has to do with how I wrote the manual flip switch if the statement, that should only be activated when the physical flip switch is actually flipped.

#include <DS3231.h>

// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);

//Motor pins
const int motorPin1 = 12;
const int motorPin2 = 11;

//Toggle Switch
const int doorSwitchPin = 10;

//Leds
const int ledRunning = 9;
const int ledDone = 8;

//Toggle Switch state
int doorSwitchState = 0;

//Door closed or opened
int doorStatus = 1;

//Door opening/closing process on
int timerOveride = 0;

//Other
char * time_tracker;
int rotationValue = 5000;


void setup() {
  Serial.begin(9600);
  rtc.begin();

  // Set time and date, uncomment when time has been set.
  //rtc.setDOW(TUESDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(14, 9, 40);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(20, 8, 2019);   // Set the date to January 1st, 2014
  
  pinMode(doorSwitchPin, INPUT_PULLUP);
  pinMode(ledRunning, OUTPUT);
  pinMode(ledDone, OUTPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
}

void loop() {
  doorSwitchState = digitalRead(doorSwitchPin);
  time_tracker = rtc.getTimeStr();

  // Send time
  Serial.println( rtc.getTimeStr() );
  Serial.println( doorStatus );
  Serial.println( timerOveride );

  //Open door at a given time
  if(strcmp(time_tracker, "01:00:00") == 0 && doorStatus == 0) {
    timerOveride = 1;
    openDoor();
    timerOveride = 0;
  }

  //Close door at a given time
  if(strcmp(time_tracker, "01:00:30") == 0 && doorStatus == 1) {
    timerOveride = 1;
    closeDoor();
    timerOveride = 0;
  }

  //Manually opening and closing of door via toggle switch
  if(timerOveride == 0) {
    if(doorSwitchState == HIGH && doorStatus == 0) {
      openDoor();
    }
  
    if(doorSwitchState == LOW && doorStatus == 1) {
      closeDoor();
    }
  }
}

void openDoor() {
    //Spin motor
    analogWrite(motorPin1, 180);
    analogWrite(motorPin2, 0);
    
    digitalWrite(ledRunning, HIGH);
    delay(rotationValue);
    
    analogWrite(motorPin1, 0);

    digitalWrite(ledRunning, LOW);
    digitalWrite(ledDone, HIGH);
    doorStatus = 1;
    
    delay(1000);
    digitalWrite(ledDone, LOW);
}

void closeDoor() {
  //Spin motor
    analogWrite(motorPin1, 0);
    analogWrite(motorPin2, 180);
    
    digitalWrite(ledRunning, HIGH);
    delay(rotationValue);
    
    analogWrite(motorPin2, 0);

    digitalWrite(ledRunning, LOW);
    digitalWrite(ledDone, HIGH);
    doorStatus = 0;
    
    delay(1000);
    digitalWrite(ledDone, LOW);
}

I react upon a few things in Your code.

There are no, I say no what so ever comments to what the code line is supposed to do. Reading Your code for us, helpers, strangers, gets much more difficult. We need to chrystal ball into your mind to figure out what You are thinking.

I think You have Serial Monitor running. Before difficult if-statements You could print out the Components and check their value. You can’t imagine how many problems I cracked during 40 years using that technic.

Railroader:
I react upon a few things in Your code.

There are no, I say no what so ever comments to what the code line is supposed to do. Reading Your code for us, helpers, strangers, gets much more difficult. We need to chrystal ball into your mind to figure out what You are thinking.

I think You have Serial Monitor running. Before difficult if-statements You could print out the Components and check their value. You can't imagine how many problems I cracked during 40 years using that technic.

Hi, I've tried to make my code more understandable now. I've tried using Serial to get feedback but so far I haven't been able to fix my problem.

 doorSwitchState = digitalRead(doorSwitchPin);

You only read this once at the top of loop(). Your code has delays and door openings and closings. I doubt the doorSwitchState is what it was when you last read it.

Try adding the reading to the end of the openDoor() and closeDoor() functions.

cattledog:

 doorSwitchState = digitalRead(doorSwitchPin);

You only read this once at the top of loop(). Your code has delays and door openings and closings. I doubt the doorSwitchState is what it was when you last read it.

Try adding the reading to the end of the openDoor() and closeDoor() functions.

So you recommend adding that part of the code to the void startup instead?

Anyways I think I found what is causing my problem, my question is now how do I fix it.

So I have a physical flip switch that is either set to low or high, if at high it activates the motor making it spin to the right. This is where the problem appears, as soon as the code that activates the motor at a specific time and makes it spin to the left, my physical flip switch code turns it to the right again. This is not supposed to happpen, the switch shouldn't do anything unless it has been flipped.

I guess introducing a variable more that somehow keeps track of if the switch state with an if statement could perhaps fix that, I am going to try that.

I fixed my problem by having a variable keeping check of which way the motor had been turned. Basically a boolean.