CSCI 1600 Embedded and Real Time Software Lecture
CSCI 1600: Embedded and Real Time Software Lecture 5: Arduino Programming Steven Reiss, Fall 2015
Arduino Programming C (Actually somewhat C++) Types void, boolean, char, unsigned char, byte int, unsigned int, word, short (16 bit) long, unsigned long (32 bit) float, double 2 Lecture 5: Arduino Programming 9/4/2021
Arduino Library Functions Conversion, Character, Bit/Byte Access I/O pin. Mode, digital. Write, digital. Read analog. Reference, analog. Read, analog. Write Time millis, micros, delay. Microseconds Math min, max, abs, constrain, map, pow, sqrt, sin, cos, tan Interrupts; Serial communication; … 3 Lecture 5: Arduino Programming 9/4/2021
Arduino Programs setup() Handle initialization Setup I/O pins, clocks, interrupts loop() Handle tasks 4 Lecture 5: Arduino Programming 9/4/2021
Arduino Program Example 5 Lecture 5: Arduino Programming 9/4/2021
Clapper Code (1) const int SOUND_PIN = 0; const int STATE_FLASH 0 = 4; const int LIGHT 0_PIN = 13; const int STATE_FLASH 1 = 8; const int LIGHT 1_PIN = 12; const int THRESHOLD = 800; int clap_count = 0; const int FLASH 0_TIME = 1000; long clap_done = 0; const int FLASH 1_TIME = 500; int last_clap = 0; const int SETTLE_TIME = 10; int cur_state = 0; const int CLAP_TIME = 1000; int orig_state = 0; int do_clap = 0; 6 const int STATE_OFF = 0; long flash 0_time = 0; const int STATE_ON 0 = 1; long flash 1_time = 0; const int STATE_ON 1 = 2; long check_time = 0 ; Lecture 5: Arduino Programming 9/4/2021
Light Logic Lights are independent Can be off, on, flashing off Light structure Tells us flash rate, output pin, state Need to know when to change state 7 Lecture 5: Arduino Programming 9/4/2021
Clapper Code (2) typedef struct Light { int pin_number; int on_state; int flash_time; long update_time; } Light; struct Light light_0 = { LIGHT 0_PIN, STATE_ON 0, STATE_FLASH 0, FLASH 0_TIME, 0 }; struct Light light_1 = { LIGHT 1_PIN, STATE_ON 1, STATE_FLASH 1, FLASH 1_TIME, 0 }; 8 Lecture 5: Arduino Programming 9/4/2021
Clapper Code (3) } void set. Light(long time, struct Light * l) } { else { int l 0 = LOW; if ((cur_state & l->on_state) != 0) l 0 = HIGH; if (time < -(l->update_time)) ; else if ((cur_state & l->flash_state) != 0) { else { l->update_time = time + l->flash_time; if (l->update_time == 0) { l 0 = HIGH; } l->update_time = time + l->flash_time; } } else if (l->update_time > 0) { } if (time < l->update_time) l 0 = HIGH; else { digital. Write(l->pin_number, l 0); l->update_time = -(time + l->flash_time); } 9 Lecture 5: Arduino Programming 9/4/2021
Clapper Code (4) void handle. Lights(long time) { set. Light(time, &light_0); set. Light(time, &light_1); } 10 Lecture 5: Arduino Programming 9/4/2021
Clap Detection Logic Problems Clap may be longer than one cycle Input might be bouncy Need a gap between claps Minimum and maximum 11 Lecture 5: Arduino Programming 9/4/2021
What Does a Clap Look Like SHOW CLAP 1 CODE 12 Lecture 5: Arduino Programming 9/4/2021
Clapper Code (5) void handle. Sound(long time) do_clap = 1; { last_clap = 1; } if (check_time != 0 && time < check_time) return; check_time = 0; int val = analog. Read(SOUND_PIN); if (val >= THRESHOLD) { if (last_clap == 0 && clap_count < 3) { if (clap_done == 0) orig_state = cur_state; } else last_clap = 0; if (!do_clap && clap_done > 0 && time > clap_done) { else cur_state = orig_state; clap_count = 0; ++clap_count; clap_done = 0; clap_done = time + CLAP_TIME; 13 check_time = time + SETTLE_TIME; } } Lecture 5: Arduino Programming 9/4/2021
Clapper Code (6) void handle. Clap() else if (cur_state == STATE_ON 0) cur_state = STATE_ON 1; { else if (cur_state == STATE_ON 1) cur_state = STATE_ON 0; else cur_state = STATE_OFF; if (do_clap) { break; do_clap = 0; case 3 : switch (clap_count) { if (cur_state == STATE_OFF) case 0 : cur_state = STATE_FLASH 0 | STATE_FLASH 1; break; else if (cur_state == STATE_ON 0) cur_state = STATE_FLASH 0; case 1 : if (cur_state == STATE_OFF) cur_state = STATE_ON 0; else if (cur_state == STATE_ON 1) cur_state = STATE_FLASH 1; else cur_state = STATE_OFF; break; } case 2 : } if (cur_state == STATE_OFF) cur_state = STATE_ON 0 | STATE_ON 1; 14 Lecture 5: Arduino Programming } 9/4/2021
Arduino Programming Setup routine Called to initialize things Set direction of each pin Initialize state and other variables Set initial output values 15 Lecture 5: Arduino Programming 9/4/2021
Clapper Code (7) void setup() { // Serial. begin(9600); cur_state = STATE_OFF; clap_done = 0; last_clap = 0; clap_count = 0; do_clap = 0; check_time = 0; pin. Mode(LIGHT 0_PIN, OUTPUT); pin. Mode(LIGHT 1_PIN, OUTPUT); pin. Mode(SOUND_PIN, INPUT); } 16 Lecture 5: Arduino Programming 9/4/2021
Arduino Programming Loop routine Called continually by the OS Does the various tasks Handles frequency of tasks Do some all the time Do others every k times Do others after elapsed time k Should have a fixed max time 17 Lecture 5: Arduino Programming 9/4/2021
Clapper Code (8) void loop() { long time = millis(); handle. Lights(time); handle. Sound(time); handle. Clap(); } 18 Lecture 5: Arduino Programming 9/4/2021
Arduino Environment Basic program = sketch Can be multiple files Save, check, download 19 Lecture 5: Arduino Programming 9/4/2021
Arduino Programming Suggest a change to the clapper 20 Lecture 5: Arduino Programming 9/4/2021
Homework Read Lab handout Come prepared for lab Initial project proposal and budget Make sure its something you are really interested in Its okay to be ambitious Its easier to scale down than to scale up 21 Lecture 5: Arduino Programming 9/4/2021
- Slides: 21