Day 6: (Mon Sep 16) Event Loop Programming

Notes for 2019-09-16. See also the Fall 2019 Calendar.

Notes from Day 5

Thoughts on demo 2: it is easier to make things more complicated than simple. But I recommend striving for simplicity and ease of implementation.

Agenda

  1. Administrative

    • Small note on cleanup protocols: re-usable solder shouldn’t go in the waste bins. And let’s try to clean up detritus after using the soldering stations.

    • I’ve received just a couple of clearance documents. If you have them, please forward me scans of your PACA, PATCH, and FBI results. HR/Act 153 will not supply them to me directly, but I will need to give them to the museum.

  2. Assignments

  3. In-class

Lecture code samples

(See also Lecture Sample Code).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 1. Blink the onboard Arduino LED on pin 13.
// 2. Demonstrate a non-blocking event loop.
// 3. Demonstrate micros() and timing calculations.
// 4. Note the absence of delay().  For comparison, see blink.ino.

void setup(void)
{
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop(void)
{
  static unsigned long last_update_clock = 0;

  unsigned long now = micros();
  unsigned long interval = now - last_update_clock;
  last_update_clock = now;

  static long led_blink_timer = 0;
  static bool led_blink_state = false;
  const long led_blink_interval = 500000;
 
  led_blink_timer -= interval;
  if (led_blink_timer <= 0) {
    led_blink_timer += led_blink_interval;

    led_blink_state = !led_blink_state;
    digitalWrite(LED_BUILTIN, led_blink_state);
  }
}