Hi, everybody.
I'm a beginner in this stuff, but I hope someone of you could help me.
I'm trying to make charlieplexing leds (just two of them).
Found one example in forum, modified a little bit and seems like it works as I wanted. Code below:
#define LED_1 5
#define LED_2 6
void setup()
{
pinMode(LED_1, INPUT);
pinMode(LED_2, INPUT);
}
void loop()
{
set_pins(LED_1, LED_2);
delay(300);
set_pins(LED_2, LED_1);
delay(300);
}
void set_pins(int high_pin, int low_pin)
{
reset_pins();
pinMode(high_pin, OUTPUT);
pinMode(low_pin, OUTPUT);
digitalWrite(high_pin, HIGH);
digitalWrite(low_pin,LOW);
}
void reset_pins()
{
pinMode(LED_1, INPUT);
pinMode(LED_2, INPUT);
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
}
But I want to make it blinking only 6 hours per day (after start), and another 18 hours stays in the dark.
I've read lots of forums, and tried lots of them. With no luck.
I undestand, that it is only lack of knowledge, and i hope you can help me.
Six hours is 21600000 milliseconds, eighteen hours is 64800000 milliseconds
Thank you for quick reply, but I know how to do math (no offence, really), I just have no idea, how to code it.
Somewhere in forum I found That is posibility to use FOR loop, but haven't get the result.
Post your best attempt(s).
Explain why they didn't do what you expected, or did do what you didn't expect.
(Kudos for using code tags on your first post though)
#define LED_1 5
#define LED_2 6
#define TWELVE_HRS 5000UL // 5 seconds because don't want to wait half day to test it
unsigned long startTime;
void setup()
{
pinMode(LED_1, INPUT);
pinMode(LED_2, INPUT);
startTime = millis();
}
void loop()
{
if (millis() - startTime > TWELVE_HRS)
{
set_pins(LED_1, LED_2);
delay(300);
set_pins(LED_2, LED_1);
delay(300);
startTime = millis();
}
}
void set_pins(int high_pin, int low_pin)
{
reset_pins();
pinMode(high_pin, OUTPUT);
pinMode(low_pin, OUTPUT);
digitalWrite(high_pin, HIGH);
digitalWrite(low_pin,LOW);
}
void reset_pins()
{
pinMode(LED_1, INPUT);
pinMode(LED_2, INPUT);
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
}
"best" attempt. both leds blinks once, and then have to be 5 seconds gap, but one of them stays lighting..
You never call reset_pins() except that one place where you are about to turn on a light. If you want the lights to go out, call reset_pins(). A good place would be after the 300 millisecond delay() (at least the second one).
If you don't want your cycle to drift by 600 milliseconds every time, set 'startTime' to "startTime + TWELVE_HRS" instead of setting it to millis().
Thank You for reply Johnwasser.
Now I see, why lights is still on. I got it about reset_pins.
But i'm not sure i understand about setting startTime to TWELVE_HRS.
Where I have to do it? in the end of the loop?
void loop()
{
if (millis() - startTime > TWELVE_HRS)
{
set_pins(LED_1, LED_2);
delay(300);
set_pins(LED_2, LED_1);
delay(300);
reset_pins();
startTime = TWELVE_HRS();
}
}
Now It's blinks just once.
I know, that I'm starting from beginning, but now I'm reading about millis and how to use it.
I had to do it before asking for a help.
If You have any ideas, I would really appreciate it.
darwudas:
But i'm not sure i understand about setting startTime to TWELVE_HRS.
You have misunderstood. Read the message again.
rjha94
December 17, 2019, 6:05pm
9
You can use two states to check what to do.
#define LED_1 5
#define LED_2 6
#define TWELVE_HOURS 12000UL
#define SIX_HOURS 6000UL
unsigned long startTime;
uint8_t state;
startTime = millis();
void setup()
{
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
startTime = millis();
// state 0: OFF
// state 1: ON
state = 1 ;
}
void loop() {
switch(state) {
case 0:
if( (millis() - startTime) > TWELVE_HOURS) {
state = 1;
startTime = millis();
}
break;
case 1:
set_pins();
if( (millis() - startTime) > SIX_HOURS) {
// change state
state = 0;
turn_off();
startTime = millis();
}
break;
default:
break;
}
}
void turn_off() {
digitalWrite(LED_1, LOW); delay(10);
digitalWrite(LED_2, LOW); delay(10);
}
void set_pins() {
digitalWrite(LED_1, HIGH); delay(10);
digitalWrite(LED_2, LOW); delay(10);
delay(300);
digitalWrite(LED_1, LOW); delay(10);
digitalWrite(LED_2, HIGH); delay(10);
}
unsigned long startTime;
uint8_t state;
startTime = millis();
Oops