Change Bool in seperate function

Hi all,

I have some experience with Arduino, but only small programs, and i rarely use seperate functions.
At the moment i am working on a simple program to open and close the door for the chickencoop depending on the light outside.

I have a problem with two Boolean values, i have Googled my but off but i can't seem to find the correct anwers. (probably because i am asking the wrong questions...).

The problem i have is with two buttons, these buttons get activated when the door is either up or down.
I have written a small function to check the buttons and set the Boolean values to True of False (and turn a LED on or off).
-- void checkButton()
But back in the "void loop()" these settings are not copied.'

I understand that a "void()" does not return a value, but i can't seem to find a solution for this problem.

I hope i explained my problem correctly, below i will paste a small part of the code involved.

Thank you for any help you can give me.

bool doorOpen = false;      // Set booleans for door open or closed state
bool doorClosed = false;


void loop() {


Other code.....


if (doorError == false)  {  //As long as there is no problem with the door, see if it is up or down and turn on the correct LED
  Serial.println("DoorError = False");
 checkDoor();  
}



void checkDoor()  {
 
  doorUpState = digitalRead(doorUp);
 if(doorUpState == HIGH)  {            // If the door is open turn on the red LED
  digitalWrite(redLed, HIGH);
  doorOpen = true;
 }
  doorUpState = digitalRead(doorUp);
 if(doorUpState == LOW)  {            // If the door is not open, turn off the red LED
   digitalWrite(redLed, LOW);
   doorOpen = false;
 }
 
   doorDownState = digitalRead(doorDown);
 if(doorDownState == HIGH)  {          // If the door is down, turn on the green LED
   digitalWrite(greenLed, HIGH);
   doorClosed = true;
 }
   doorDownState = digitalRead(doorDown);
 if(doorDownState == LOW)  {          // If the door is not down, turn off the green LED
   digitalWrite(greenLed, LOW);
   doorClosed = false;
 }

Nothing in the snippets of code that you have posted checks the value of the variable.

Please post a complete program that shows the problem. You have not even posted the checkButton() function

Sorry for that, i thought the complete code could be a bit messy. (and it probably is…)
Beow the complete code.

/*
Chickendoor Automation.
4 relays as l/r motor controller
R1 + R2 = Door up
R3 + R4 = Door down
Door takes about 8 sec to open or close
to reset error, fix the problem and reset arduino!! 

1 LDR on pin A0
4 Relays on pin D5, D6, D7 and D8
1 Door high button on pin D2
1 Door Low button on pin D3
1 Manual activation button on pin D4
1 Green LED on pin D9
1 Red LED on pin D1
*/

const int LDRpin = A0; // pin where we connected the LDR and the resistor

int LDRValue = 0;     // result of reading the analog pin

unsigned long previousMillis = 0;        // will store last time Timer was updated
const long interval = 1000;           // interval at which to check LDR (300000 milliseconds = 5 minutes)

const int morning = 300;         // LDR value for enough light for morning opening
const int evening = 100;         // LDR value for dark enough for night closing

const int doorDown = 3;    // Set button for door is down to pin 3
int doorDownState = 0;        // Set holder for buttonstate
const int doorUp = 2;      // Set button for door up to pin 2
int doorUpState = 0;        
const int doorManual = 4;  // Set button for manual control to pin 4
int doorManualState = 0;

const int R1 = 5; //R1 (Relay 1)is defined as pin #5
const int R2 = 6;
const int R3 = 7;
const int R4 = 8;

const int doorTime = 9000;    // Set the max runtime for the door

bool doorError = false;      // Set a boolean for error with the doormotor

bool doorOpen = false;      // Set booleans for door open or closed state
bool doorClosed = false;

const int greenLed = 9;      // Set the green LED to pin 9
const int redLed = 10;
int ledState = LOW;                    //Set the ledstate for blinking the red led on error
unsigned long previousMillisLed = 0;   //LED blink last timer update
const long intervalLed = 1000;          //interval for blinking error LED

void setup() {
  Serial.begin(9600); // sets serial port for communication

  pinMode (doorDown, INPUT);    //Set button pin as input
  pinMode (doorUp, INPUT);      //Set button pin as input
  pinMode (doorManual, INPUT);  //Set button pin as input
  
  pinMode (R1, OUTPUT);    // Set R1 (Relay 1) pin as ooutput
  pinMode (R2, OUTPUT);
  pinMode (R3, OUTPUT);
  pinMode (R4, OUTPUT);
  
  pinMode (greenLed, OUTPUT);    // Set LED pins as output
  pinMode (redLed, OUTPUT);
  
  digitalWrite(R1, HIGH); // Relaymodue seems to be reversed, HIGH = LOW//
  digitalWrite(R2, HIGH); //This way all relays are at rest before the loop runs//
  digitalWrite(R3, HIGH);
  digitalWrite(R4, HIGH);


}


void loop() {
  LDRValue = analogRead(LDRpin); // read the value from the LDR
  Serial.print("LDR = : ");
  Serial.println(LDRValue);      // print the value to the serial port
  delay(100);                    // wait a little
  
 unsigned long currentMillis = millis();

if(doorError == true)  {    //if there is a problem with the door, blink the red LED
  Serial.println("DoorError = True");
  blinkLED();
}

if (doorError == false)  {  //As long as there is no problem with the door, see if it is up or down and turn on the correct LED
  Serial.println("DoorError = False");
 checkDoor();  
}

doorManualState = digitalRead(doorManual);
if(doorManualState == HIGH)  {      //Check the manual button
  Serial.println("Doorbutton Pressed");
  if(doorOpen == true)  {      //Close the door when open
    closeDoor();
  }
  if(doorClosed == true)  {    //Open the door when closed
    openDoor();
  }
}

 if(currentMillis - previousMillis >= interval) {  // save the last time you checked the LDR 
    previousMillis += interval;   

        if(LDRValue >= morning) {                  //Open the door in the morning
            Serial.println("Opening Door");
          openDoor();
        }
        if(LDRValue <= evening)  {                //Close the door uin the evening
            Serial.println("Closing Door");
          closeDoor();
        }  
 }
}





void openDoor() {
 
  unsigned long currentMillisDoor = millis();    //Start timer for max motor runtime
 
 doorUpState = digitalRead(doorUp);
    Serial.println(doorUpState);
    
  while(doorUpState == LOW) {
     doorUpState = digitalRead(doorUp);
      Serial.println("DoorUp has to be low now");
    if(doorError == true){    // If motor has been running too long, exit this function
    digitalWrite(R1, HIGH);  // Turn off motor
    digitalWrite(R2, HIGH);
    Serial.println("THere seems to be a problem witth the door error");
      return;
    }
    Serial.println("Motor should start by now");
   digitalWrite(R1,LOW);    //Turn motor so the door opens
   digitalWrite(R2,LOW);    
  
  unsigned long millisNow = millis();
  
    if(millisNow - currentMillisDoor >= doorTime)  {   //Check if motor has been running too long, if so, set the doorError to true
         doorError = true;  
      
  
 } 
 
 
}
}




void closeDoor()  {
  
   unsigned long currentMillisDoor = millis();    //Start timer for max motor runtime
    
    doorDownState =digitalRead(doorDown);      // Check Doordown  button
    
  while(doorDownState == LOW) {
      doorDownState =digitalRead(doorDown);
    if(doorError == true){    // If motor has been running too long, exit this function
    digitalWrite(R3, HIGH);  //Turn of motor
    digitalWrite(R4, HIGH);
      return;
    }
    
   digitalWrite(R3,LOW);    //Turn motor so the door closes
   digitalWrite(R4,LOW);    
  
  unsigned long millisNow = millis();
  
    if(millisNow - currentMillisDoor >= doorTime)  {   //Check if motor has been running too long, if so, set the doorError to true
         doorError = true;  
      
  
 } 
 
 
} 
}






void blinkLED()  {
 digitalWrite(greenLed, LOW);
 unsigned long currentMillisLed = millis();                    //Blink red LED on door error
   
   if(currentMillisLed - previousMillisLed >= intervalLed)  {
    previousMillisLed += intervalLed;
    
    if (ledState == LOW)  
       ledState = HIGH;
    
     else 
       ledState = LOW;
     
       digitalWrite(redLed, ledState);
     
   }  
   
}




void checkDoor()  {
 
  doorUpState = digitalRead(doorUp);
 if(doorUpState == HIGH)  {            // If the door is open turn on the red LED
  digitalWrite(redLed, HIGH);
  doorOpen = true;
 }
  doorUpState = digitalRead(doorUp);
 if(doorUpState == LOW)  {            // If the door is not open, turn off the red LED
   digitalWrite(redLed, LOW);
   doorOpen = false;
 }
 
   doorDownState = digitalRead(doorDown);
 if(doorDownState == HIGH)  {          // If the door is down, turn on the green LED
   digitalWrite(greenLed, HIGH);
   doorClosed = true;
 }
   doorDownState = digitalRead(doorDown);
 if(doorDownState == LOW)  {          // If the door is not down, turn off the green LED
   digitalWrite(greenLed, LOW);
   doorClosed = false;
 }
 
  
}

Try a site search for 'chicken coop'. This wheel has already been invented many times.

Thank you dougp, i'll see if i can find some more information that way.

But just because i might need this for a future project, is there a simple way to explain to me how to get the boolean values back into the void loop.

Thanks

I have written a small function to check the buttons and set the Boolean values to True of False (and turn a LED on or off).
-- void checkButton()

Your program does not seem to have a function named checkButton() in it

I think you are misreading the cause of a problem. You have declared the boolean variables correctly and they should reflect any changes made in the checkDoor() function.

You might have a logic error somewhere. Why do you think the values are wrong in loop()?

Breaking down your program to test the variable setting you can see that the values change:

bool doorOpen = false;      // Set booleans for door open or closed state
bool doorClosed = false;


void setup() {

  Serial.begin(9600); // sets serial port for communication


  Serial.print("Door open:    ");
  Serial.println(doorOpen ? "true" : "false");

  Serial.print("Door closed:  ");
  Serial.println(doorClosed ? "true" : "false");

  checkDoor();

  Serial.print("Door open:    ");
  Serial.println(doorOpen ? "true" : "false");

  Serial.print("Door closed:  ");
  Serial.println(doorClosed ? "true" : "false");

}

void loop() { }

void checkDoor()  {

  doorOpen = true;
  doorClosed = true;

}

Serial monitor:

Door open:    false
Door closed:  false
Door open:    true
Door closed:  true

Moving the code from setup() to loop() should show the same results (but over and over).

BTW, it's always a good idea to do Tools=>Auto Format in the IDE so code is properly indented.

Thank you for the info about the autoformat, this makes everything more readable. (didn't know it was there).

I also tried to simplify the code, only in a different way. To be honest i don't rely understand your code.
But you are correct, the boolean values are working fine. So the problem must be somewhere else in the coding. I'll have to search a little further

Thanks for the help so far, i'll go digging and see if i can find the real problem.

p.s. for who would like to know, below the testcode i used to check only the boolean.

bool test1 = false;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:

  doTest();

  if (test1 == true) {

    Serial.println("True");

  }

  if (test1 == false) {
    Serial.println("False");

  }
  delay(1000);

}

void doTest() {
  if (test1 == false)
    test1 = true;
  else
    test1 = false;



}

My advice is to simplify your code. A boolean variable is either true or false, so a construct like this:

  if (test1 == true) {

    Serial.println("True");

  }

  if (test1 == false) {
    Serial.println("False");

  }

is better written like this:

  if (test1 == true) {

    Serial.println("True");

  } else {

    Serial.println("False");

  }

in the checkDoor() function the doorUp and doorDown pins are read twice, an LED is set HIGH if the value is HIGH and LOW if the value is LOW, a boolean is set true if the value is HIGH and false if the value is LOW.

The function can be condensed to:

void checkDoor()  {

  doorUpState = digitalRead(doorUp);
  digitalWrite(redLed, doorUpState);  // If the door is open turn on the red LED
  doorOpen = (doorUpState == HIGH);

  doorDownState = digitalRead(doorDown);
  digitalWrite(greenLed, doorDownState);         // If the door is down, turn on the green LED
  doorClosed = (doorDownState == HIGH);


}

One possible problem I can see is that the motor only seems to be turned off if there is a timeout error, it’s difficult to tell what is happening with the door since I can’t see it.

The openDoor() and closeDoor() functions are practically the same, they just use different pin numbers and set different global variables. If you folded those two functions into one, passing in the pin numbers to use, then you would only have one function to debug. For example, you might have a moveDoor() function that returns the proper state

int moveDoor(int doorPin, int r1, int r2) {

  int doorState = digitalRead(doorPin);;
  unsigned long currentMillisDoor = millis();    //Start timer for max motor runtime

  Serial.println(doorState);

  while (doorState == LOW) {

    Serial.println("Motor should start by now");
    digitalWrite(r1, LOW);   //Turn motor so the door opens
    digitalWrite(r2, LOW);

    unsigned long millisNow = millis();

    if (millisNow - currentMillisDoor >= doorTime)  {  //Check if motor has been running too long, if so, set the doorError to true
      doorError = true;

      Serial.println("THere seems to be a problem witth the door error");

      break;  // exit while loop

    }

    doorState = digitalRead(doorPin);

  }

  digitalWrite(r1, HIGH);  // Turn off motor
  digitalWrite(r2, HIGH);

  return (doorState);

}

and you could call it like this:

doorUpState = moveDoor(doorUp, R1, R2);
doorDownState = moveDoor(doorDown, R3, R4);

Thank you very much for your advice Blue Eyes.
I will defenately use this.

I only do some coding a few times a year, so i'm using very basic coding.
I do enjoy wrinting the whole thing rather then using someone elses code and modify it (though sometimes this is just easier). This way i think i might learn a little.

I did find the problem eventualy and solved it, the door seems to work exactly as i hoped it would.
Next to some small errors i made (one of which is forgetting to turn off the motor at the end of the openDoor() and closeDoor() functuions, like you said.) in the end the biggest problem was a defective button (the doorLow button).
I did check the button before and after installing, but somewhere along the line it just stopped working. Maby i need to stop using very cheap parts ;o)

Thanks for the help and the advice everybody.