CSCI 1600 Embedded and Real Time Software Lecture

CSCI 1600: Embedded and Real Time Software Lecture 4: Introduction to the Arduino Jasmine Liu, Fall 2019

What is an Arduino 2 Lecture 4: Introduction to the Arduino 12/15/2021

What you need to know Microprocessor Power/USB Input Ground and power outputs Digital input/outputs Analog input/outputs Reset button 3 Lecture 4: Introduction to the Arduino 12/15/2021

Arduino Design: USB 4 Lecture 4: Introduction to the Arduino 12/15/2021

Arduino Design: CPU 5 Lecture 4: Introduction to the Arduino 12/15/2021

Embedded Processors Arduino: a microcontroller Others exist as well General purpose processors ARM processor (low power) Raspberry Pi Special purpose processors DSP for sound, video processing Graphics processors 6 Lecture 4: Introduction to the Arduino 12/15/2021

Other alternatives Field-Programmable Gate Array (FPGA) Less expensive than creating chip Like having a bucket of parts to wire together Except the wiring is done programmatically And the whole thing fits on a chip Easy and cheap to make in quantity Programmed in a high-level language Event-based, parallel 7 Lecture 4: Introduction to the Arduino 12/15/2021

Arduino Restrictions Simple CPU Single core, single threaded No pipelining No floating point processor How is floating point done 8 Lecture 4: Introduction to the Arduino 12/15/2021

Arduino CPU Clock Speed: 16 MHz 32 KB flash, 2 KB SRAM, 1 KB EEPROM 16 bit processing 9 Lecture 4: Introduction to the Arduino 12/15/2021

Digital Input/Outputs Inputs: Must be declared (default is output) Should have value connected Should provide input Pull-up input might be enabled Outputs Provide up to 40 ma Connect LEDs through 470 or 1 K ohm resistor Pins 0, 1 shared with USB; Pin 13 with LED 10 Lecture 4: Introduction to the Arduino 12/15/2021

Input pull-up 11 Lecture 4: Introduction to the Arduino 12/15/2021

Analog Inputs/Outputs Can also be used as digital inputs/outputs Must be declared Inputs (A 1 -A 6) Can measure 0 -3, 0 -5 volts 10 -bit A/D converter (0 -1023) Outputs (on pins 3, 5, 6, 9, 10, 11) Uses pulse-width modulation Values 0 -255 (0 -100% of 5 v during cycle) Pins 5, 6 shared for clocks 12 Lecture 4: Introduction to the Arduino 12/15/2021

Pulse Width Modulation 13 Lecture 4: Introduction to the Arduino 12/15/2021

Clapper Input circuit Analog based on sound intensity Output Circuit Two lights Digital (on or off) 14 Lecture 4: Introduction to the Arduino 12/15/2021

Clapper OFF state Clap once: one light on Clap twice: two lights switch being on Clap three times: the light flashes 15 Lecture 4: Introduction to the Arduino 12/15/2021

Clapper Input Circuit 16 Lecture 4: Introduction to the Arduino 12/15/2021

Output circuit Connect Digital Output Pin to LED Through a 1 K resistor (why) Other side of LED is grounded 17 Lecture 4: Introduction to the Arduino 12/15/2021

How should the clapper work? Read the input How often Check if we have a clap What does a clap look like How would you find out Check if we have a clap sequence Turn on lights based on inputs 18 Lecture 4: Introduction to the Arduino 12/15/2021

Clapper Program Each task is broken up Initialization for the task Set up input pins, precompute tables, … What is done repeatedly The real work of the task First part goes in (or called from) setup() Second part called from loop() 19 Lecture 4: Introduction to the Arduino 12/15/2021

Arduino Programming Setup routine Called to initialize things Set direction of each pin Initialize state and other variables Set initial output values 20 Lecture 5: Arduino Programming 12/15/2021

Scheduling Tasks loop() needs to invoke all tasks Tasks that should run every k ms Tasks that should run on event Typical loop code: Get time For each timed task Check if it should run, call routine for it if so For each event-based task Check flag, call routine for it if set Checks can be done in the task code itself 21 Lecture 4: Introduction to the Arduino 12/15/2021

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 22 Lecture 5: Arduino Programming 12/15/2021

Arduino Library Functions I/O pin. Mode, digital. Write, digital. Read analog. Reference, analog. Read, analog. Write Time millis, micros, delay. Microseconds Standard C/C++ libraries (math. h, etc. ) min, max, abs, constrain, map, pow, sqrt, sin, cos, tan For more info: https: //www. arduino. cc/en/Reference/Home. Page 23 Lecture 5: Arduino Programming 12/15/2021

Clapper Code: setup() void setup() { cur_state = STATE_OFF; // Initialize program state clap_done = 0; last_clap = 0; clap_count = 0; do_clap = 0; check_time = 0; pin. Mode(LIGHT 0_PIN, OUTPUT); // Configure pins pin. Mode(LIGHT 1_PIN, OUTPUT); pin. Mode(SOUND_PIN, INPUT); } 24 Lecture 5: Arduino Programming 12/15/2021

Clapper Code: loop() void loop() { long time = millis(); // Handle tasks handle. Lights(time); // Update LEDs handle. Sound(time); // Do clap detection handle. Clap(); // Change program state } 25 Lecture 5: Arduino Programming 12/15/2021

Clapper Code: Program State 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; 26 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 12/15/2021

Clapper Code: Light Definitions 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 }; 27 Lecture 5: Arduino Programming 12/15/2021

Clapper Code: handle. Lights() void handle. Lights(long time) { set. Light(time, &light_0); set. Light(time, &light_1); } 28 Lecture 5: Arduino Programming 12/15/2021

Clapper Code: set. Light() } 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); } 29 Lecture 5: Arduino Programming 12/15/2021

Clapper Code: Clap detection 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; 30 check_time = time + SETTLE_TIME; } } Lecture 5: Arduino Programming 12/15/2021

Clapper Code: Clapper logic 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; 31 Lecture 5: Arduino Programming } 12/15/2021

Arduino Programming: Common pitfalls Handling multiple tasks can be tricky Must avoid blocking, delay(), etc. No OS to help you Limited memory Some C++ libraries available, but have high cost Can easily overflow stack 32 Lecture 5: Arduino Programming 12/15/2021
- Slides: 32