Code issue? greenhouse project

led 3, 4, and 6 are not working no matter what me and my friend try. i don't know if there's something wrong in code or the board,
i can send picture of arduino board im working on tinkercad, but i need help ASAP

/*
greenhouse
*/

// Temperature Sensor
const int tempSensorPin = A0;
float tempC = 0;
float tempF = 0;
float sensorVal = 0;

// LEDs for all systems
int led1Pin = 13; // always on, yellow
int led2Pin = 12; // on and off, associated with sprinkler too, yellow
int led3Pin = 11; // door button, green, turn on for 5 sec if buttondoor is pressed, if not turn off
int led4Pin = 10; // sprinkler, blue, turn on for 5 second if led2 is on and button2 is pressed if either one is not satisfied don't turn on
int led5Pin = 9; // vent, always on, blue
int led6Pin = 8; // fan, red, turn on for 7 sec when led2 is on
int buttondoorPin = 5;
int buttonsprinklerPin = 4;

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

// Initialize LEDs and sprinkler pins as output
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
pinMode(led5Pin, OUTPUT);
pinMode(led6Pin, OUTPUT);

// Initialize door sensor pins as INPUT
pinMode(buttondoorPin, INPUT);
pinMode(buttonsprinklerPin, INPUT);

// Always on LEDs
// Turn on led1
digitalWrite(led1Pin, HIGH);

// Turn on vent
digitalWrite(led5Pin, HIGH);

}
void loop() {
// LED2 Control: Turn on for 12 seconds, then off for 12 seconds
digitalWrite(led2Pin, HIGH);
delay(12000);
digitalWrite(led2Pin, LOW);
delay(12000);

// Sprinkler Control: Turn on led4Pin for 5 seconds if led2Pin is on and sprinkler button is pressed

if (digitalRead(buttonsprinklerPin) == HIGH){
digitalWrite(led4Pin, HIGH);

}

// Fan Control: Turn on led6Pin if led2Pin is on; turn off led6Pin if led2Pin is off
digitalWrite(led6Pin, digitalRead(led2Pin));

// Door Button Control: Turn on led3Pin for 5 seconds if door button is pressed; turn off led3Pin if not pressed
if (digitalRead(buttondoorPin) == HIGH) {
digitalWrite(led3Pin, HIGH);
delay(5000);
digitalWrite(led3Pin, LOW);
} else {
digitalWrite(led3Pin, LOW);
}
}

Welcome to the forum

Your topic has been moved to the Programming category of the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

You're programm is stopping at least 24 seconds.

Read this
Blink Without Delay

Write a very small program that does nothing more than blinking a single LED.
And with this code you can check if the wiring for this single LED is OK

If you want other to check your wiring you have to take time to get to trustlevel one to be able to post pictures

It is always a bad idea to start late on a task or to ask late on a task

best regards Stefan

To post images etc. you need trust level 1, you can get there by:

  • Entering at least 5 topics
  • Reading at least 30 posts
  • Spend a total of 10 minutes reading posts

Users at trust level 1 can...

  • Use all core Discourse functions; all new user restrictions are removed
  • Send PMs
  • Upload images and attachments

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

a button press can be recognized by detecting a change in state and becoming LOW

suggest you change the how button pins are configured and that they are tested to be LOW

not sure if your LEDs are wired to turn on when set HIGH or LOW

since led6 is set to whatever LED2 is, i would expect it to be off if LOW is off.

with 12 second delays around setting LED2, buttons would only ever be checked every 24 seconds. using millis() would be better, but suggest commenting out this code to start

without a pull-up or pull-down, reading the button pins is a bit ambiguous. LED4 might never be being set HIGH

but same could be said for LED3

I have also made a small code review

All in all, a proper time module is missing in the zeroth approach.

this statement can never be true given that led2Pin was turned off 12 seconds earlier

not sure what led2Pin is suppose to represent, but it seems it's intended to enable things for 12 second cycles. would be easily accomplished using millis() instead of delay()

void loop()
{
    unsigned long msec = millis ();
    if (msec - msecLst >= 12000)  {
        msecLst = msec;

        digitalWrite (led2Pin, ! digitalRead (led2Pin));
    }
    ...
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.