Advertisement
ripred

CompileTimeDebug.ino

Nov 28th, 2023 (edited)
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | Source Code | 0 0
  1. /*
  2.  * Example program to count the number of times a door is
  3.  * opened and closed using an ultrasonic sensor on the door.
  4.  * Also includes a 7-segment display counter and status LEDs
  5.  * to show the status of the door.
  6.  *
  7.  */
  8. #include <NewPing.h>
  9. #include <CompileTime.h>
  10. using namespace CompileTime;
  11.  
  12. #define   TRIGGER_PIN   11
  13. #define   ECHO_PIN      10
  14. #define   RED_LED_PIN   A0
  15. #define   GRN_LED_PIN   A1
  16. #define   MAX_DISTANCE  50
  17. #define   TRG_DISTANCE  20
  18.  
  19. // Global variables
  20. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
  21.  
  22. // segment pin numbers
  23. int const sevseg_pins[7] = {
  24. //a, b, c, d, e, f, g
  25.   2, 3, 4, 5, 6, 7, 8
  26. };
  27.  
  28. // 7 segment bit patterns for digits 0-9
  29. uint8_t const digits[10] = {
  30. //   abcdefg
  31.   0b01111110, // '0'
  32.   0b00110000, // '1'
  33.   0b01101101, // '2'
  34.   0b01111001, // '3'
  35.   0b00110011, // '4'
  36.   0b01011011, // '5'
  37.   0b01011111, // '6'
  38.   0b01110000, // '7'
  39.   0b01111111, // '8'
  40.   0b01111011  // '9'
  41. };
  42.  
  43. int lstState = 0;
  44. int lstPpl = 0;
  45. int count = 0;
  46. int ppl = 0;
  47.  
  48. // update the 7-segment display with the given number
  49. void update_display(uint8_t const value) {
  50.     // get the but pattern for this number from the digits[] array
  51.     uint8_t bits = digits[value];
  52.  
  53.     // for each segment, set it to the next bit in the pattern
  54.     for (int segment=0; segment < 7; segment++) {
  55.         digitalWrite(sevseg_pins[segment], bits & 0x40);
  56.  
  57.         // shift the pattern one bit to the left to get the next
  58.         // segment in position 0b01000000 (0x40 in hex)
  59.         bits <<= 1;
  60.     }
  61. }
  62.  
  63. void setup() {
  64.   // sync our time with the PC
  65.   setCompileTime(4);
  66.  
  67.   // make the segment pins all outputs
  68.   for (int pin : sevseg_pins) {
  69.     pinMode(pin, OUTPUT);
  70.   }
  71.  
  72.   // make the LED pins outputs
  73.   pinMode(RED_LED_PIN, OUTPUT);
  74.   pinMode(GRN_LED_PIN, OUTPUT);
  75.  
  76.   // start the serial output
  77.   Serial.begin(115200);
  78. }
  79.  
  80. void loop() {
  81.   updateTime(micros());
  82.  
  83.   int dist = sonar.ping_cm();
  84.   delay(100);
  85.  
  86.   // decide if state is true or false
  87.   int crtState = (dist < TRG_DISTANCE && dist != 0);
  88.  
  89.   // update LED status's
  90.   digitalWrite(RED_LED_PIN,  crtState);
  91.   digitalWrite(GRN_LED_PIN, !crtState);
  92.  
  93.   // check for trigger state change
  94.   if (crtState != lstState) {
  95.     lstState = crtState;
  96.  
  97.     // if set, increase the people count
  98.     if (crtState) {
  99.       ppl++;
  100.     }
  101.   }
  102.  
  103.   // see if ppl count has changed
  104.   if (lstPpl != ppl) {
  105.     // keep the last value in sync
  106.     lstPpl = ppl;
  107.  
  108.   // format a string with the people count and the current time:
  109.     char buff[32] = "";
  110.     sprintf(buff, "%d people at %d:%02d:%02d",
  111.             ppl, hour, minute, second);
  112.  
  113.     // output the string to the monitor window:
  114.     Serial.println(buff);
  115.  
  116.     // update the 7-segment display with the people count:
  117.     update_display(ppl % 10);
  118.   }
  119. }
Tags: Bugs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement