Back to Parent

//read moisture level
int soilPin = A0;
int thresholdValue = 2500;
//turn on white led when press the switch button
//turn on redlight if moisture level is low
int redPin = D2;
int ledPin = D0;
int switchPin = D1;

void setup()
{
  pinMode(switchPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  pinMode(soilPin, INPUT);
  pinMode(redPin, OUTPUT);
}

void loop()
{
  int buttonState = digitalRead(switchPin);
  if( buttonState == LOW)
  {
    digitalWrite(ledPin, HIGH);
    if (analogRead(soilPin) > thresholdValue)
    {
      digitalWrite(redPin, HIGH);
    }else{digitalWrite(redPin, LOW);
    }
  }else{
    digitalWrite(ledPin, LOW);
    digitalWrite(redPin, LOW);
  }
  delay(1000);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0