covsq
covsq

Reputation: 51

Arduino turn on a led for 2 seconds and then turn off and wait 3 seconds and start all over again

I'm using a microcontroler PIC 32 in a diligent cerebot Mx4cK. I have switch implemented on my protoboard and I want to turn on a led after the switch it's activated, then this led have to be in this state for 2 seconds and then have to be off for another 3 seconds and start all over again (on->2 seconds off->3 seconds)

This is my code so far, I think it's missing one condition but I can't find it... can you help me?

const int led=PIN_LED1;
const int pinSwitch1=16;

void setup()
{
  pinMode(pinSwitch1,INPUT);
  pinMode(led,OUTPUT);
     digitalWrite(led,LOW);
}
void loop()
{
   unsigned long actual_time=millis();
   static unsigned long cicle_time=0;
   static unsigned long off_time=0;
 
   static int switch_state1=0;

  switch_state1=digitalRead(pinSwitch1);

  if (switch_state1==HIGH)
  {
       if((actual_time-cicle_time)<5000)
     {
       digitalWrite(led,HIGH);
       cicle_time=actual_time; 
     }
       if((actual_time-off_time)>2000)
      {
       digitalWrite(led,LOW); 
      off_time=actual_time;
      }
                  
}
else 
{
  digitalWrite(led,LOW); 
}        

}

Actually my code, blincks for 2 seconds and it's not consider the 3 seconds that it has to be off.

[This is my new code, I missing an initial condition to light for the first time]

const int led=PIN_LED1; const int pinSwitch1=16;

void setup()
{
  pinMode(pinSwitch1,INPUT);
  pinMode(led,OUTPUT);
     digitalWrite(led,LOW);
}
void loop()
{
   unsigned long actual_time=millis();
   static unsigned long cicle_time=0;
   static unsigned long off_time=0;
 
   static int switch_state1=0;
   
   static int cicle_on=0;

  switch_state1=digitalRead(pinSwitch1);

  if (switch_state1==HIGH)
  {
       if((actual_time-cicle_time)>5000)
     {
       digitalWrite(led,HIGH);
       cicle_time=actual_time; 
       cicle_on=HIGH;
     }
  }
 else 
{
  digitalWrite(led,LOW); 
}        

       if((actual_time-off_time)>2000)
      {
       digitalWrite(led,LOW); 
       off_time=actual_time;
       cicle_on=LOW;
      }
                  


}

Upvotes: 0

Views: 6686

Answers (2)

Harsha
Harsha

Reputation: 669

This should solve your problem:

   const int led=PIN_LED1;
    const int pinSwitch1=16;
    unsigned long cicle_time=0;
    void setup()
    {
      pinMode(pinSwitch1,INPUT);
      pinMode(led,OUTPUT);
      digitalWrite(led,LOW);
    }
    void loop()
    {
       unsigned long actual_time=0;
       static int switch_state1=0
       switch_state1=digitalRead(pinSwitch1);
       digitalWrite(led,LOW); 
         while(switch_state1==HIGH)
          {
            digitalWrite(led,HIGH);
            cicle_time=millis();
           while((millis()-cicle_time)!>=2000)
         {       
         }
         cicle_time=millis();
         digitalWrite(led,LOW); 
           while((millis()-circle_time)!>=3000)
          {  
          }

    }
}

Upvotes: 0

Spektre
Spektre

Reputation: 51873

  1. generic code debug is off-topic here

    • and you even do not specify what this does instead of what it should do
  2. you are writing to LED every cycle

    • which slows things down
    • when you add more stuff then it could give you many head ages later
  3. use absolute time instead of relative for your planned events and update only when needed

        static unsigned long time_LED_on =0xFFFFFFFF;
        static unsigned long time_LED_off=0xFFFFFFFF;
        //...
        if ((switch_state1==HIGH)&&(time_LED_on!=0xFFFFFFFF)) // init on switch toggle only would be better in interrupt
          {
          time_LED_on =actual_time;
          time_LED_off=actual_time+2000;
          }
        if (switch_state1==LOW ) // stop if switch off also would be better in interrupt
          {
          time_LED_on =0xFFFFFFFF;
          time_LED_off=0xFFFFFFFF;
          }
        // handle LED event
        if (time_LED_on >=actual_time) { digitalWrite(led,HIGH); time_LED_on +=5000; }
        if (time_LED_off>=actual_time) { digitalWrite(led,LOW ); time_LED_off+=5000; }
    
    • beware that time can overflow you can handle it by
    • if (min(all_times) > max) all_times -= max;
    • dont know how many bits your platform have if it is not 32 then change the 0xFFFFFFFF acordingly

Upvotes: 0

Related Questions