![](/media/cache/profiles/Headshot_Fall2023_Square.jpg.50x50_q85.jpg)
Count patients as the enter a waiting room.
Fork of ForLoopIteration by
main.cpp@1:3872cc92ceaf, 2017-10-17 (annotated)
- Committer:
- CSTritt
- Date:
- Tue Oct 17 15:26:16 2017 +0000
- Revision:
- 1:3872cc92ceaf
- Parent:
- 0:9475544275a6
Initial Version. Working.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 0:9475544275a6 | 1 | /* |
CSTritt | 1:3872cc92ceaf | 2 | Project: PatientCounterMain |
CSTritt | 0:9475544275a6 | 3 | File: main.cpp |
CSTritt | 0:9475544275a6 | 4 | |
CSTritt | 1:3872cc92ceaf | 5 | Counts patients entering a leaving a waiting room. This version uses fuctions |
CSTritt | 1:3872cc92ceaf | 6 | that would otherwise be linked to interrupts. |
CSTritt | 0:9475544275a6 | 7 | |
CSTritt | 0:9475544275a6 | 8 | The circuit: |
CSTritt | 0:9475544275a6 | 9 | |
CSTritt | 0:9475544275a6 | 10 | Bargraph LEDs from pins 2 through 11 to ground via 330 Ohm resistors. |
CSTritt | 1:3872cc92ceaf | 11 | Pushbuttons between pins D14 & 15 and ground (internal pullup resistors |
CSTritt | 1:3872cc92ceaf | 12 | used. |
CSTritt | 0:9475544275a6 | 13 | |
CSTritt | 1:3872cc92ceaf | 14 | Created by C. S. Tritt 9/21/17 (v. 1.0) |
CSTritt | 0:9475544275a6 | 15 | |
CSTritt | 0:9475544275a6 | 16 | This example code is in the public domain. |
CSTritt | 0:9475544275a6 | 17 | */ |
CSTritt | 0:9475544275a6 | 18 | |
CSTritt | 0:9475544275a6 | 19 | #include "mbed.h" |
CSTritt | 0:9475544275a6 | 20 | |
CSTritt | 0:9475544275a6 | 21 | BusOut bar_graph(D2,D3,D4,D5,D6,D7,D8,D9,D10,D11); |
CSTritt | 1:3872cc92ceaf | 22 | |
CSTritt | 1:3872cc92ceaf | 23 | DigitalIn entered(D12, PullUp); |
CSTritt | 1:3872cc92ceaf | 24 | DigitalIn left(D13, PullUp); |
CSTritt | 1:3872cc92ceaf | 25 | |
CSTritt | 1:3872cc92ceaf | 26 | int count = 0; |
CSTritt | 1:3872cc92ceaf | 27 | |
CSTritt | 1:3872cc92ceaf | 28 | void countUp(void) { |
CSTritt | 1:3872cc92ceaf | 29 | count++; |
CSTritt | 1:3872cc92ceaf | 30 | printf("Counting up: %d.\n", count); |
CSTritt | 1:3872cc92ceaf | 31 | } |
CSTritt | 1:3872cc92ceaf | 32 | |
CSTritt | 1:3872cc92ceaf | 33 | void countDown(void) { |
CSTritt | 1:3872cc92ceaf | 34 | if (count > 0) count--; |
CSTritt | 1:3872cc92ceaf | 35 | printf("Counting up: %d.\n", count); |
CSTritt | 1:3872cc92ceaf | 36 | } |
CSTritt | 0:9475544275a6 | 37 | |
CSTritt | 0:9475544275a6 | 38 | int main() { |
CSTritt | 0:9475544275a6 | 39 | while(true) { // Keep the lights going back and forth forever. |
CSTritt | 0:9475544275a6 | 40 | |
CSTritt | 1:3872cc92ceaf | 41 | if (!entered) { // Account for entries. |
CSTritt | 1:3872cc92ceaf | 42 | while (!entered); // Wait for button release. |
CSTritt | 1:3872cc92ceaf | 43 | countUp(); |
CSTritt | 0:9475544275a6 | 44 | } |
CSTritt | 1:3872cc92ceaf | 45 | if (!left) { // Account for departures. |
CSTritt | 1:3872cc92ceaf | 46 | while (!left); // Wait for button release. |
CSTritt | 1:3872cc92ceaf | 47 | countDown(); |
CSTritt | 0:9475544275a6 | 48 | } |
CSTritt | 1:3872cc92ceaf | 49 | // Send count to console. |
CSTritt | 1:3872cc92ceaf | 50 | printf("Count: %d, Entered: %d, Left: %d.\n", count, (int) entered, |
CSTritt | 1:3872cc92ceaf | 51 | (int) left); |
CSTritt | 1:3872cc92ceaf | 52 | bar_graph = count; // Show count on bar. |
CSTritt | 1:3872cc92ceaf | 53 | wait(0.05); // Pause. |
CSTritt | 0:9475544275a6 | 54 | } |
CSTritt | 0:9475544275a6 | 55 | } |