if LED turn on, turn off after few seconds. Please help me

Hi I'm Korean students preparing for some competition and I really really need help.

I wanna make circuit that if light sensor detect the darkness, than turn on the LED and

turn it off after 3 seconds no matter what.

this is my current cord


int cds-sensor = A0;
int LED = 2;

void setup() {
Serial.begin(9600)
pinMode(LED,OUTPUT);
}

void loop() {
int cds-val = analogRead(cds-sensor);

if(cds-val)>75){
digitalWrite(LED,HIGH);
}
else}
digitalWrite(LED,LOW);
}
}


I have problem with turn off the LED after 3 seconds. I really really need cord of that
Please help me thank you

and if there was a grammatical incorrect in my post, I'm sorry!

if(cds-val)>75){
digitalWrite(LED,HIGH);
}
else} ---> this should be { -- How did it even compile without errors !!
digitalWrite(LED,LOW);
}
}

You don’t give much information but this framework might work. You’ll probably have to adjust a few things to get it to work for you:

//.../CDS Sensor/CDS_Sensor.ino

//defines
#define LED_OFF     LOW                 //digitalWrite value to turn LED off
#define LED_ON      HIGH                //digitalWrite value to turn LED on

//state machine state names
#define ST_OFF              0
#define ST_TIMING           1
#define ST_WAIT_LIGHTNESS   2

//LED on-time constant
const unsigned long TIME_LED_ON = 3000ul;   //3000mS == 3-seconds

//CDS thresholds for what is considered "dark" and "light" Adjust as needed
#define CDS_DARKNESS        (int)75
#define CDS_LIGHTNESS       (int)100

//pin constants
const byte pinLED = 2;
const byte pinCDS = A0;

void setup() 
{
    //pinCDS will already be an input; defined here for clarity
    pinMode( pinCDS, INPUT );
    //set pinLED to output and turn off the LED
    pinMode( pinLED, OUTPUT );
    digitalWrite( pinLED, LED_OFF );
    
}//setup

void loop() 
{
    static byte
        stateLED = ST_OFF;
    static unsigned long
        timeLED;
    unsigned long
        timeNow;

    //grab the time (millis() value) now
    timeNow = millis();

    //execute the state we're in
    switch( stateLED )
    {
        case    ST_OFF:
            //LED is off now and we're waiting for "darkness"
            if( analogRead( pinCDS ) <= CDS_DARKNESS )
            {
                //darkness seen; turn on the LED, and
                digitalWrite( pinLED, LED_ON );
                //set the timeLED variable to the "current" time
                timeLED = timeNow;
                //and move to the timing state
                stateLED = ST_TIMING;
                
            }//if
            
        break;

        case    ST_TIMING:
            //wait until TIME_LED_ON milliseconds has elapsed since turning the LED on
            if( timeNow - timeLED >= TIME_LED_ON )
            {
                //then turn off the LED and go wait for the CDS sensor to show "light"
                //this ensures the LED stays off even if darkness persists
                digitalWrite( pinLED, LED_OFF );
                stateLED = ST_WAIT_LIGHTNESS;
                
            }//if
            
        break;

        case    ST_WAIT_LIGHTNESS:
            //wait for the sensor to see lightness before proceeding to check for darkness again
            if( analogRead( pinCDS ) >= CDS_LIGHTNESS )
            {
                //light seen; set state to "OFF" where we wait for the CDS sensor to show dark again
                stateLED = ST_OFF;
                
            }//if            
            
        break;
                
    }//switch
    
}//loop

Thank you thank you so much for your help. I’m actually first of this thing “Arduino”. I just learned little,

but my programming skill is not very enough. I red all of your explanations after the “//” and I organized

it. Can you check it for me that these are alight? And I got error in this part "case ST-

OFF;"Thank you once again.


#define LED_OFF LOW
#define LED_ON HIGH
#define ST_OFF 0
#define ST_TIMING 1
#define ST_WAIT_LIGHTNESS 2

const unsigned long TIME_LED_ON = 3000ul;

#define CDS_DARKNESS (int)75
#define CDS_LIGHTNESS (int)100

const byte pinLED = 2;
const byte pinCDS = A0;

void setup()
{
pinMode( pinCDS, INPUT );
pinMode( pinLED, OUTPUT );
digitalWrite( pinLED, LED_OFF );

}

void loop()
{
static byte
stateLED = ST_OFF;
static unsigned long
timeLED;
unsigned long
timeNow;

{
case ST_OFF:
if( analogRead( pinCDS ) <= CDS_DARKNESS )
{

}

break;

case ST_TIMING:
if( timeNow - timeLED >= TIME_LED_ON )
{
digitalWrite( pinLED, LED_OFF );
stateLED = ST_WAIT_LIGHTNESS;

}

break;

case ST_WAIT_LIGHTNESS:
if( analogRead( pinCDS ) >= CDS_LIGHTNESS )
{

}

break;

}

}

The code does not compile

Reformatting your code shows up some problems

#define LED_OFF     LOW
#define LED_ON      HIGH
#define ST_OFF              0
#define ST_TIMING           1
#define ST_WAIT_LIGHTNESS   2

const unsigned long TIME_LED_ON = 3000ul;

#define CDS_DARKNESS        (int)75
#define CDS_LIGHTNESS       (int)100

const byte pinLED = 2;
const byte pinCDS = A0;

void setup()
{
  pinMode( pinCDS, INPUT );
  pinMode( pinLED, OUTPUT );
  digitalWrite( pinLED, LED_OFF );
}

void loop()
{
  static byte
  stateLED = ST_OFF;
  static unsigned long
  timeLED;
  unsigned long
  timeNow;
  {
  case    ST_OFF:
    if ( analogRead( pinCDS ) <= CDS_DARKNESS )
    {
    }
    break;
  case    ST_TIMING:
    if ( timeNow - timeLED >= TIME_LED_ON )
    {
      digitalWrite( pinLED, LED_OFF );
      stateLED = ST_WAIT_LIGHTNESS;
    }
    break;
  case    ST_WAIT_LIGHTNESS:
    if ( analogRead( pinCDS ) >= CDS_LIGHTNESS )
    {
    }
    break;
  }
}

For instance, where is the switch command that belongs with the case statements ?
Note the if statements with no dependant code. Is that deliberate ?
Putting the data types on a separate line to the variable names is a little unusual

Note how Auto Formatting the code in the IDE and putting it in code tags here helps to make it more readable

Thank you for your advise and help. and I got a error in case part, saying that "case label '0' not within a switch statement"
I don't understand your first sentence because I know just little English. Sorry :frowning:

I got a error in case part, saying that "case label '0' not within a switch statement"

Look at the code in post #2. In particular note that which case is true depends on the value of stateLED in the switch statement. Your code has no switch statement so you get the compiler error.

Hi
It’s my first time with “arduino” and I don’t know almost anything about programming…

I wrote some post and some very kind peoples answered and made some cord for me.

but I have a problem with “case” errors keep occur in that part.

How can I do to solve it?

below is part of cord that people wrote it for me.


case ST_OFF:
if ( analogRead( pinCDS ) <= CDS_DARKNESS )
{
}
break;
case ST_TIMING:
if ( timeNow - timeLED >= TIME_LED_ON )
{
digitalWrite( pinLED, LED_OFF );
stateLED = ST_WAIT_LIGHTNESS;
}
break;
case ST_WAIT_LIGHTNESS:
if ( analogRead( pinCDS ) >= CDS_LIGHTNESS )
{
}
break;
}
}

And the errors…?

(And the rest of the code)

Is this topic related to this ?

How can I make switch statement of what information for needed?

It appears that you want different code to run depending on the state of the LED held in the stateLed variable

Look at the code in reply #2 and you will see

    switch( stateLED )
    {
       //code for each case goes here
    }

See my reply in your other thread.

You have no switch() statement in your code. Post #2 in the other thread has an example of how to program it.

@Min7, please do not cross-post. Threads merged.