View difference between Paste ID: w4eLAt5G and b9mhuNMK
SHOW: | | - or go back to the newest paste.
1
    /*
2
        MultiBlinky v1.0
3
        Blink 2 leds at different frequencies. 
4
        The code is extremely verbose, to make it easier for beginners to 
5
        understand what's happening.
6
        
7
        For future, as this sketch will eat cpu cycles as a starving donkey,
8
        I will add a microDelay at the end of the loop() function. Calculating 
9
        the amount of microseconds to delay is the tricky part. Feel free to try 
10
        your hand at it.
11
    */
12
13
    /*
14
        Output leds
15
    */
16
    #define LED1 13
17
    #define LED2 12
18
19
    /*
20
        How many cycles per second (how many cycles per 1000000 microseconds)
21
        are we doing?
22
    */
23
    #define LED1_CYCLES 1
24
    #define LED2_CYCLES 2
25
26
    // these are here for clarity, you can define directly the microseconds/millisecond you need between HIGH/LOW
27
    uint32_t led1_period = 0;
28
    uint32_t led2_period = 0;
29
30
    // when did we last updated the led
31
    uint32_t led1_time;
32
    uint32_t led2_time;
33
34
    // state of the led
35
    char led1_state;
36
    char led2_state;
37
38
    void setup()
39
    {
40
        pinMode(LED1, OUTPUT);
41
        pinMode(LED2, OUTPUT);
42
        
43
        // calculate here the period, so we don't have to do it on each loop() run.
44
        led1_period = 1000000/LED1_CYCLES;
45
        led2_period = 1000000/LED2_CYCLES;
46
        
47
        // initialize the system
48
        led1_state = LOW;
49
        led2_state = LOW;
50
        
51
        digitalWrite(LED1,LOW);
52
        digitalWrite(LED2,LOW);
53
        
54
        led1_time = 0;
55
        led2_time = 0;
56
    } // end function setup
57
58
    void loop()
59
    {
60
        /*
61
            How many microseconds since the board was powered up.
62
            Arduino's internal timer will reset at some point and
63
            make our sketch miss a beat or two. If better timing 
64
            is required, go on and use a RTC. 
65
        */
66
        uint32_t current_time = micros();
67
        
68
        /*
69
            do we need to do anything to the first led?
70
        */
71
        if ( current_time - led1_time > led1_period ) {
72
            // switch the first led
73
            led1_time = current_time;
74
            if ( led1_state == LOW ) {
75
                led1_state = HIGH;
76
                digitalWrite(LED1, led1_state);    
77
            } else {
78
                led1_state = LOW;
79
                digitalWrite(LED1, led1_state);
80
            } // end if
81
        } // end if
82
        
83
        /*
84
            do we need to do anything to the second led?
85
        */
86
        if ( current_time - led2_time > led2_period ) {
87
            // switch the first led
88
            led2_time = current_time;
89
            if ( led2_state == LOW ) {
90
                led2_state = HIGH;
91
                digitalWrite(LED2, led2_state);
92
            } else {
93
                led2_state = LOW;
94
                digitalWrite(LED2, led2_state);
95
            } // end if
96
        } // end if
97
    } // end function loop