posting code as a code-section means the effort of having your code inside the arduino-IDE is two steps small taking 15 seconds:
-1. Click Copy-Button of the code-section
-
- press Ctrl-V into arduino-IDE
done
compare this to downloading a picture starting two write down your code new
You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps
- press Ctrl-T for autoformatting your code
- do a rightclick with the mouse and choose "copy for forum"
- paste clipboard into write-window of a posting
Your leds have two different frequencies = two different times to be on/off. These are timeperiods
Your code uses two different timers to measure
how much time has passed by since last timeperiod
first timer measures "has 1 seconds passed by?"
second second timer measures "has 1,5 seconds passed by?"
Each time the timeperiod is over invert the state of the LED
from ON to OFF
or
from OFF to ON
that creates the signal-pattern that the picture shows
measuring time-differences on a microcontroller is done with the same principle as you look onto your watch:
How much time has passed by since midnight.
A microcontroller doesn't have a "standard-watch 23:59
it has a milliseconds counter counting up from 0 to 4.294.967.296
if you calculate timedifferencies the absolute value is irrelevant
example
24 milliseconds - 3 milliseconds = 21 milliseconds
1024 milliseconds - 1003 millisecocds = 21 milliseconds
9024 milliseconds - 9003 milliseconds = 21 milliseconds
1.000.009.024 milliseconds - 1.000.009003 milliseconds = 21 milliseconds
This is valid for any time-difference
calculating a time-difference is what this function is doing
boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - periodStartTime >= TimePeriod )
{
periodStartTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
you define a timer-variable which must be of type unsigned long!!
unsigned long MyTimer1 = 0; // variables MUST be of type unsigned long
unsigned long MyTimer2 = 0; // variables MUST be of type unsigned long
here is the complete sketch
boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - periodStartTime >= TimePeriod )
{
periodStartTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
unsigned long MyTimer1 = 0; // variables MUST be of type unsigned long
unsigned long MyTimer2 = 0; // variables MUST be of type unsigned long
const byte LED1_Pin = 11;
const byte LED2_Pin = 12;
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
pinMode(LED1_Pin, OUTPUT);
pinMode(LED2_Pin, OUTPUT);
}
void loop() {
if ( TimePeriodIsOver(MyTimer1,1000) ) {
digitalWrite( LED1_Pin, !digitalRead(LED1_Pin) );
}
if ( TimePeriodIsOver(MyTimer2,1500) ) {
digitalWrite( LED2_Pin, !digitalRead(LED2_Pin) );
}
}
digitalWrite( LED1_Pin, !digitalRead(LED1_Pin) );
does this
digitalRead(LED1_Pin)
read current state of LED1_Pin. Can be done with an IO-pin configured as output too
The attention-mark "!" means logical NOT
!HIGH = LOW
!LOW = HIGH
which does inverting the current state of the IO-pin
digitalWrite( LED1_Pin, NEWSTATE
NEWSTATE is the result of !digitalRead(LED1_Pin)
all in all inverst the state of the IO-pin ON-OFF-ON-OFF....