Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- MultiBlinky v1.0
- Blink 2 leds at different frequencies.
- The code is extremely verbose, to make it easier for beginners to
- understand what's happening.
- For future, as this sketch will eat cpu cycles as a starving donkey,
- I will add a microDelay at the end of the loop() function. Calculating
- the amount of microseconds to delay is the tricky part. Feel free to try
- your hand at it.
- */
- /*
- Output leds
- */
- #define LED1 13
- #define LED2 12
- /*
- How many cycles per second (how many cycles per 1000000 microseconds)
- are we doing?
- */
- #define LED1_CYCLES 1
- #define LED2_CYCLES 2
- // these are here for clarity, you can define directly the microseconds/millisecond you need between HIGH/LOW
- uint32_t led1_period = 0;
- uint32_t led2_period = 0;
- // when did we last updated the led
- uint32_t led1_time;
- uint32_t led2_time;
- // state of the led
- char led1_state;
- char led2_state;
- void setup()
- {
- pinMode(LED1, OUTPUT);
- pinMode(LED2, OUTPUT);
- // calculate here the period, so we don't have to do it on each loop() run.
- led1_period = 1000000/LED1_CYCLES;
- led2_period = 1000000/LED2_CYCLES;
- // initialize the system
- led1_state = LOW;
- led2_state = LOW;
- digitalWrite(LED1,LOW);
- digitalWrite(LED2,LOW);
- led1_time = 0;
- led2_time = 0;
- } // end function setup
- void loop()
- {
- /*
- How many microseconds since the board was powered up.
- Arduino's internal timer will reset at some point and
- make our sketch miss a beat or two. If better timing
- is required, go on and use a RTC.
- */
- uint32_t current_time = micros();
- /*
- do we need to do anything to the first led?
- */
- if ( current_time - led1_time > led1_period ) {
- // switch the first led
- led1_time = current_time;
- if ( led1_state == LOW ) {
- led1_state = HIGH;
- digitalWrite(LED1, led1_state);
- } else {
- led1_state = LOW;
- digitalWrite(LED1, led1_state);
- } // end if
- } // end if
- /*
- do we need to do anything to the second led?
- */
- if ( current_time - led2_time > led2_period ) {
- // switch the first led
- led2_time = current_time;
- if ( led2_state == LOW ) {
- led2_state = HIGH;
- digitalWrite(LED2, led2_state);
- } else {
- led2_state = LOW;
- digitalWrite(LED2, led2_state);
- } // end if
- } // end if
- } // end function loop
Advertisement
Add Comment
Please, Sign In to add comment