Reputation: 51
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
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
Reputation: 51873
generic code debug is off-topic here
you are writing to LED every cycle
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; }
if (min(all_times) > max) all_times -= max;
0xFFFFFFFF
acordinglyUpvotes: 0