Trying to add door limit features to egg incubator code

Alright so I found more complicated code for my egg incubator and finally got it working good however I want to add a door limit switch feature that turns on a light and spins the egg turner to a zeropoint and stops, if the racks are not centered when the door is opened. This is the code I had for doing that from my first simple script. How would I go about adding it to the code attached?

// When the door is open or closed features

void maindoor() {
  if (digitalRead (doorlimitswitch) == LOW) { // DOOR OPEN
    if (digitalRead (turnerinput) == HIGH)
     {
     digitalWrite(lightrelay, LOW);
     digitalWrite(turnerrelay1, LOW);
     lcd.clear();
     lcd.setCursor(0, 2);
     lcd.print("     Door  Open     ");
     lcd.setCursor(0, 3);
     lcd.print(" Centering Rack.... ");
     b = 1;
     }
     
    if (digitalRead (turnerinput) == LOW)
     {
      digitalWrite(lightrelay, LOW);
      digitalWrite(turnerrelay1, HIGH);
      lcd.clear();
      lcd.setCursor(0, 2);
      lcd.print("     Door  Open     ");
      lcd.setCursor(0, 3);
      lcd.print("   Rack  Centered   ");
      b = 0;
 
   if (digitalRead (doorlimitswitch) == HIGH) // Door CLOSED
    {
      digitalWrite(lightrelay, HIGH);
      digitalWrite(turnerrelay1, HIGH);
      b = 0;


    // Buzzer for when egg rack is turning to zero point
   
   if (b > 0)    // Makes a god awful noise well turning the egg rack to keep your dumb hands away.
   {
    tone(11,100);
    delay (300);
    tone(11,400);
    delay (300);
    tone(11,200);
    delay (100);
    noTone(11);
   }
  }
}

If I try adding it as a function but it didn't work

incmaincode_Hogen.ino (48.2 KB)

Is it not possible to experiment with a much shorter program - say 2k or 3k rather than 48k ?

...R

There is no such thing as a "void" except perhaps in interstellar space. They are called "functions".

aarg:
There is no such thing as a "void" except perhaps in interstellar space. They are called "functions".

haha sorry I am still a rookie and trying to learn all this terminology. I changed it to function :).

The code is rather intense but it would be perfect if it had this one more feature.

Anita_Bath:
haha sorry I am still a rookie and trying to learn all this terminology. I changed it to function :).

The code is rather intense but it would be perfect if it had this one more feature.

Thank you, but please don't go back and edit past posts, it makes the replies seem like nonsense.

What you are experiencing now, is the ancient problem, dating back probably as far as 60 years, of a huge monolithic program that needs "just a little feature", and it is hard to figure out where and how to add it. This is why "software engineering" evolved. With the use of functions and data structures, factoring the code is possible. If that is done, a new feature can usually be added easily without side effects.

Now that you're asking someone else to help, that problem has come home to roost. (That pun was unintentional, I swear!)

One issue I see with the maindoor() function is that embedded within this conditional:

  if (digitalRead (doorlimitswitch) == LOW) { // DOOR OPEN

you have this conditional:

      if (digitalRead (doorlimitswitch) == HIGH) // Door CLOSED

It is unlikely (not impossible but highly unlikely) that the door status will not change between those two conditionals. Perhaps you had something else in mind.

Also, just FYI, the following code sequence and all similar to it:

    if (digitalRead (turnerinput) == HIGH)
    {
        // input is HIGH code
    }

    if (digitalRead (turnerinput) == LOW)
    {
        // input is LOW code
    }

can be reduced to:

    if (digitalRead (turnerinput) == HIGH)
    {
        // input is HIGH code
    }
    else
    {
        // input is LOW code
    }

Robin2:
Is it not possible to experiment with a much shorter program - say 2k or 3k rather than 48k ?

...R

I had this feature working fine in my first script that was 25 k however I would like to add it to this 48 k script then ill be able to sleep at night knowing my incubator is well equipped.

ToddL1962:
One issue I see with the maindoor() function is that embedded within this conditional:

  if (digitalRead (doorlimitswitch) == LOW) { // DOOR OPEN

you have this conditional:

      if (digitalRead (doorlimitswitch) == HIGH) // Door CLOSED

It is unlikely (not impossible but highly unlikely) that the door status will not change between those two conditionals. Perhaps you had something else in mind.

Also, just FYI, the following code sequence and all similar to it:

    if (digitalRead (turnerinput) == HIGH)

{
       // input is HIGH code
   }

if (digitalRead (turnerinput) == LOW)
   {
       // input is LOW code
   }




can be reduced to:



if (digitalRead (turnerinput) == HIGH)
   {
       // input is HIGH code
   }
   else
   {
       // input is LOW code
   }

I also had a buzzer come on for when the racks are centering. If the door is closed the motor is off and the buzzer is off and the light is off.

also

is it possible to use a while loop for when the door is open so it only focuses on that feature at that time? or am I lost again haha.

Anita_Bath:
I had this feature working fine in my first script that was 25 k however I would like to add it to this 48 k script then ill be able to sleep at night knowing my incubator is well equipped.

Oh, really? How would you know that there are not bugs in it that just haven't surfaced because they require certain input conditions that you haven't thought of, or tested for?

If you had written modular code, the modules could all be tested and proofread independently, so that doubt would not be as prominent in your mind.

aarg:
Oh, really? How would you know that there are not bugs in it that just haven't surfaced because they require certain input conditions that you haven't thought of, or tested for?

If you had written modular code, the modules could all be tested and proofread independently, so that doubt would not be as prominent in your mind.

The first script I had working was not modular. Coding in modular forum is new to me and I would like to learn it.

Anita_Bath:
The first script I had working was not modular. Coding in modular forum is new to me and I would like to learn it.

Maybe have a look at Planning and Implementing a Program

...R

Robin2:
Maybe have a look at Planning and Implementing a Program

...R

very good read! thank you for that. however I am a visual learner and unless someone can help show me how I would add this feature I am still going to be lost.

Alright so after looking through the modular code more I found a light relay section that uses the enter button to double as as a light switch. If I add another input on pin 13 and add it to this code as a door limit switch it could double as my door light. Only problem is i have no idea how to do that. I have been playing with it and i can't seem to make an OR statement to save my life. Here is the untouched code for the button light.

void  checkeditbutton() {

	lightButton = digitalRead(buttonEnter);
	editButton = digitalRead(buttonEdit);

	//after 2 mins kick out of menu and do not save changes
	unsigned long currentMillis5 = millis();
	if (currentMillis5 - previousMillis5 > 120000L) {
		escape = 1;
		noSave = 1;
		editMode = 0;
		previousMillis5 = currentMillis5;
	}

	if (editButton != preveditButton) {
		if (editButton == LOW) {
			editMode++;
			escape = 1;
			lcd.clear();
			previousMillis5 = currentMillis5;
		}
		preveditButton = editButton;
	}
	editMode = constrain(editMode, 0, 3);

	if (lightButton != prevlightButton) {
		if (lightButton == LOW) {
			editMode = 0;
			lcd.clear();
			escape = 1;
			noSave = 1;
			menuNumber = 0;
			subMenu = 0;
			setlightrelay = !setlightrelay;
		}
		prevlightButton = lightButton;
	}

	if (setlightrelay == 0) {
		digitalWrite(lightrelay, HIGH);
	}
	else {
		digitalWrite(lightrelay, LOW);
	}
}
	unsigned long currentMillis5 = millis();
...
			previousMillis5 = currentMillis5;

It is not necessary to protect variables inside a function from clashing with global variables, or variables in other functions, by giving them unique names like you have by appending a "5". It just makes clutter.

That is because variables declared inside a function are not shared with the rest of the program. It is called
"scope of a variable".

Your variable names don't reflect their usage very well

	lightButton = digitalRead(buttonEnter);

.. for example you should have something like:

	lightButtonInputValue = digitalRead(buttonEnterPin);

aarg:

	unsigned long currentMillis5 = millis();

...
previousMillis5 = currentMillis5;




It is not necessary to protect variables inside a function from clashing with global variables, or variables in other functions, by giving them unique names like you have by appending a "5". It just makes clutter.

That is because variables declared inside a function are not shared with the rest of the program. It is called 
"scope of a variable".

Your variable names don't reflect their usage very well


lightButton = digitalRead(buttonEnter);



.. for example you should have something like:


lightButtonInputValue = digitalRead(buttonEnterPin);

I understand that but that code isn't mine it was code I found and used for my egg incubator.

I am simply trying to add some features to it like a door light by adding a limit switch. How would I go about that?