Some help in basic code please!

Hi, I want that the Arduino reads me an analog input, and if this input is lower than 500 it must switch on a LED for 6secs and if the analog value is over 500, then it has to turn on another LED, until the value is less than 500.
What I’m trying to explain, is that if the value is under 500 one LED switches on (only 6 seconds), and if the value is over 500, the other switches on. But never both at the same time. The problem is that if the value drops to a lower number than 500, both LEDs stay on.
This is the code:

int moisture = 0;
int red = 12;
int green = 13;
int sensor = A0;
void setup() {
  
  Serial.begin(9600);
  pinMode(red,OUTPUT);
  pinMode(green,OUTPUT);
}

void loop() {
  
  moisture = analogRead(sensor);
  Serial.println(moisture);
  if (moisture <= 500)
  {
    digitalWrite(red, HIGH);
    delay(6000);
    digitalWrite(red,LOW);
   }
 else 
    {
     digitalWrite(green,HIGH);
    }
  delay(2000);
}

I hope that there is someone of you that is not so dumb as me, and can help me with this.

Moritz

P.S.:Sorry for my english

Is what you have not working? What's it doing wrong?

"someone of you that is not so dumb as me" so you are calling us dumb :o

What are you finding is not working? Are you aware delay() stops your sketch for the delay period?

LarryD:
“someone of you that is not so dumb as me”
so you are calling us dumb :o

What are you finding is not working?
Are you aware delay() stops your sketch for the delay period?

And what can I do for it? Wich of both delays?

Change this section of code:

  if (moisture <= 500)
  {
    digitalWrite(red, HIGH);
    delay(6000);
    digitalWrite(red,LOW);
   }

To:

  if (moisture <= 500)
  {
    digitalWrite(green, LOW);
    digitalWrite(red, HIGH);
    delay(6000);
    digitalWrite(red, LOW);
   }

The green LED wasn’t ever told to turn off.

Do for what? You still haven't said what the undesired behavior is. delay isn't evil if you really have nothing else to do.

If you want to get rid of them then look at the "Blink Without Delay" example for some inspiration on how to time things without using delay.

So there is a warning led that stays on for 6 seconds after being above 500 and a ok led that is lit only when below or equal 500.

My suggestion:

const byte red = 12;
const byte green = 13;
const byte sensor = A0;

void setup() {
  Serial.begin(9600);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
}
unsigned long mPrint;
unsigned long mLRed;
unsigned long mLoop;

void loop() {
  mLoop = millis();
  if (mLoop - mPrint > 100) {
    int moisture = analogRead(sensor);
    Serial.println(moisture);
    if (moisture > 500) {
      mLRed = mLoop;
      digitalWrite(red, HIGH);
      digitalWrite(green, LOW);
    } else {
      digitalWrite(green, HIGH);
    }
    mPrint = mLoop;
  }
  if (mLoop - mLRed > 6000) {
    digitalWrite(red, LOW);
  }
}

DuaneDegn:
Change this section of code:

  if (moisture <= 500)

{
    digitalWrite(red, HIGH);
    delay(6000);
    digitalWrite(red,LOW);
  }




To:



if (moisture <= 500)
  {
    digitalWrite(green, LOW);
    digitalWrite(red, HIGH);
    delay(6000);
    digitalWrite(red, LOW);
  }




The green LED wasn't ever told to turn off.

Thank you! It worked!

The delay stuff does not work, once below, it will be blind for 8 seconds. So there is no way to switch on the other led which should follow the above below state.

With out delay() :

int moisture = 0;
int red = 12;
int green = 13;
int sensor = A0;

unsigned long lastMillis;
bool myFlag;

void setup() 
{
  Serial.begin(9600);
  pinMode(red,OUTPUT);
  pinMode(green,OUTPUT);
  myFlag = true;
}

void loop() 
{
  moisture = analogRead(sensor);
  Serial.println(moisture);
  if (myFlag && moisture <= 500)
  {
    lastMillis = millis();
    myFlag = false; 
  }

  if(!myFlag && millis() - lastMillis <= 6000UL )
  {
    digitalWrite(red, HIGH);
    digitalWrite(green,LOW);
  } 
  else if(!myFlag && moisture > 500)
  {
    digitalWrite(green,HIGH);
    myFlag = true;
  }

  else
  {
    digitalWrite(red,LOW); 
  }

} //END of loop()

Edit: minor changes