Advertisement
pseud0_

Untitled

Jan 11th, 2022
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <LiquidCrystal_I2C.h>
  3.  
  4. LiquidCrystal_I2C lcd(0x27, 16, 2);
  5. int green = 5;
  6. int red = 6;
  7. int yellow = 7;
  8. int pass = 2;
  9. int fail = 3;
  10. int Emergency = 4;
  11. int buzzer = 8;
  12. int B = 0; // batch counter
  13. int p = 0; // pass counter
  14. int f = 0; // fail counter
  15. bool isInEmergency = false;
  16.  
  17. void setup()
  18. {
  19.   pinMode(green, OUTPUT); // here i am declaring the pinMode for each componant
  20.   pinMode(red, OUTPUT);
  21.   pinMode(yellow, OUTPUT);
  22.   pinMode(pass, INPUT);
  23.   pinMode(fail, INPUT);
  24.   pinMode(Emergency, INPUT);
  25.   pinMode(buzzer, OUTPUT);
  26.   lcd.init(); // I am now coding what the LCD should display in order for the Counter to work
  27.   lcd.backlight();
  28.   lcd.setCursor(0, 0);
  29.   lcd.print("Pass: ");
  30.   lcd.setCursor(9, 0);
  31.   lcd.print("Fail: ");
  32.   lcd.setCursor(4, 1);
  33.   lcd.print("Batch: ");
  34. }
  35.  
  36. bool lastButtonState = false;
  37.  
  38. void loop()
  39. {
  40.   bool newButtonState = digitalRead(Emergency);
  41.   if (newButtonState != lastButtonState)
  42.   {
  43.     // react to the initial pressing of the button, which is
  44.     // when it goes HIGH
  45.     if (newButtonState == HIGH)
  46.     {
  47.       // the emergency button has been pressed, so we need to flip its state.
  48.       // check if we're transitioning from a no-emergency to emergency or reverse
  49.       // check previous state
  50.       if (isInEmergency)
  51.       {
  52.         // new state after flipping
  53.         isInEmergency = false;
  54.         // emergency has been removed, reprint counters
  55.         lcd.clear();
  56.         lcd.setCursor(0, 0);
  57.         lcd.print("Pass: ");
  58.         lcd.print(p);
  59.         lcd.setCursor(9, 0);
  60.         lcd.print("Fail: ");
  61.         lcd.print(f);
  62.         lcd.setCursor(4, 1);
  63.         lcd.print("Batch: ");
  64.         lcd.print(B);
  65.       }
  66.       else
  67.       {
  68.         // we just entered an emergency.
  69.         isInEmergency = true;
  70.         lcd.clear(); // clear everything off the screen
  71.         lcd.setCursor(0, 0);
  72.         lcd.println("EMERGENCY");
  73.       }
  74.       // debounce button
  75.       delay(250);
  76.     }
  77.   }
  78.   lastButtonState = newButtonState;
  79.  
  80.   // stop further execution of this function if we're in an emergency.
  81.   // will prompt to re-check the emergency button in the next loop.
  82.   if (isInEmergency)
  83.     return;
  84.  
  85.   // check if the pass button has been pressed (HIGH)
  86.   if (digitalRead(pass))
  87.   {
  88.     // increase global pass counter
  89.     p++;
  90.     // update LCD screen
  91.     lcd.setCursor(5, 0);
  92.     lcd.print(p);
  93.     // have we reached 10 passes => 1 batch?
  94.     if (p == 10)
  95.     {
  96.       // increase number of batches
  97.       B++;
  98.       // reset number of passes
  99.       p = 0;
  100.       // update LCD regarding batches and passes
  101.       lcd.setCursor(10, 1);
  102.       lcd.print(B);
  103.       lcd.setCursor(5, 0);
  104.       lcd.print(p);
  105.     }
  106.     // debounce
  107.     delay(250);
  108.   }
  109.   // check if the "fail" button was pressed
  110.   if (digitalRead(fail))
  111.   {
  112.     f++; // increase global variable for fail count
  113.     // update LCD
  114.     lcd.setCursor(14, 0);
  115.     lcd.print(f);
  116.     // debounce
  117.     delay(250);
  118.   }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement