1
votes

I am just creating a simple basic program, but I can't figure out what's going wrong.

I have set three pins as output and three pins as input. When those three pins digitalRead == HIGH they will set an LED to HIGH, but instead my LED is always staying high.

Here is my Arduino code:

int LED_Low = 4; // Red LED
int LED_Avg = 3; // Yellow LED
int LED_High = 2; // Green Led

int WaterLow = 7;
int WaterAvg = 8;
int WaterHigh = 9;


void setup() {
  // Put your setup code here, to run once:
  pinMode(LED_Low,  OUTPUT);
  pinMode(LED_Avg,  OUTPUT);
  pinMode(LED_High, OUTPUT);

  pinMode(WaterLow,  INPUT);
  pinMode(WaterAvg,  INPUT);
  pinMode(WaterHigh, INPUT);
}

void check(){
    if(digitalRead(WaterLow) == HIGH){ // If Water level is low
        digitalWrite(ledLow, HIGH); // Turn on red LED indication water level is low
    }
    else{
        digitalWrite(ledLow, LOW);
    }
    if(digitalRead(WaterAvg) == HIGH){ // If water level is medium
        digitalWrite(ledAvg, HIGH); // Turn on yellow LED indicating water level is average
    }
    else{
        digitalWrite(ledAvg, LOW);
    }
    if(digitalRead(WaterHigh) == HIGH){ //
        digitalWrite(ledHigh, HIGH); //
    }
    else{
        digitalWrite(ledHigh, LOW);
    }
}

void loop() {
    // Put your main code here, to run repeatedly:
    check();
}

Enter image description here

Enter image description here

In the above image I have connected led on pin 2, 3, and 4 with 1.5 kilohm resistor and three wires in pin 7, 8, and 9 which will receive input from the 5 volt pin and turn on the LED. Accordingly, the 5 volt pin is connected to the positive terminal on the power bus and with 9.1 *2 resistors in series and then this wire connect with pin 2, 3, and 4.

2
Can you post a picture of your Arduino with everything wired up?MikeB
I updated my question with images and discriptionSkyyy

2 Answers

1
votes

I found the issue. My code was OK. It was my circuit.

The pins I declared to receive input were not connected to ground.

1
votes

You can make that with the help of two cases

  1. Define the delay

    if (digitalRead(WaterLow) == HIGH) // If Water level is low
    {
        digitalWrite(ledLow, HIGH); // Turn red LED indication water level is low
        delay(2000);
    }
    else
    {
        digitalWrite(ledLow, LOW);
    }
    
  2. Make a condition like this

    int stateled = LOW;
    int previous = LOW;
    long time = 0;
    long debounce = 200;
    
    void loop()
    {
        stateButton = digitalRead(WaterLow);
        if (stateButton == HIGH && previous == LOW && millis() - time > debounce)
        {
            if(stateLED == HIGH)
            {
                stateLED = LOW;
            }
            else
            {
                stateLED = HIGH;
            }
            time = millis();
        }
        digitalWrite(ledlow, stateLED);
        previous == stateButton;
    }