Advertisement
DrAungWinHtut

weldingmachineSLO.ino

Nov 10th, 2022
1,276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Programmer Dr. Aung Win Htut
  2. // Founder of Green Hackers
  3. #include <Adafruit_LiquidCrystal.h>
  4.  
  5. class DebouncedButton {
  6. public:
  7.   DebounceButton(uint_fast8_t pin, unsigned long debounceDelay) {
  8.     pinMode(pin, INPUT);
  9.     this->_debounceDelay = debounceDelay;
  10.     this->_pin = pin;
  11.   };
  12.  
  13.   void loop() {
  14.     bool currentButtonState = digitalRead(this->_pin);
  15.     auto currentTime = millis();
  16.  
  17.     if (currentButtonState == this->_state)
  18.       this->_lastSameState = currentTime;
  19.     else if ((this->_lastSameState + this->_debounceDelay) <= currentTime)
  20.       this->_state = currentButtonState;
  21.   };
  22.  
  23.   bool getState() {
  24.     return this->_state;
  25.   };
  26.  
  27. private:
  28.   unsigned long _debounceDelay;
  29.   uint8_t _pin;
  30.  
  31.   unsigned long _lastSameState = 0;
  32.   bool _state = LOW;
  33. };
  34.  
  35. const uint_fast8_t PUSH_BUTTON_PIN = 11;
  36. const uint_fast8_t UP_BUTTONPIN = 12;
  37. const uint_fast8_t DOWN_BUTTON_PIN = 13;
  38. const uint_fast8_t BUZZER_PIN = 10;
  39. const uint_fast8_t RELAY_PIN = 2;
  40.  
  41. const unsigned long DEBOUNCE_DELAY = 100;  // the debounce time; increase if the output flickers
  42. DebouncedButton push_button(PUSH_BUTTON_PIN, DEBOUNCE_DELAY);
  43. DebouncedButton up_button(UP_BUTTONPIN, DEBOUNCE_DELAY);
  44. DebouncedButton down_button(DOWN_BUTTON_PIN, DEBOUNCE_DELAY);
  45.  
  46. bool push_button_last_state = LOW;
  47. bool up_button_last_state = LOW;
  48. bool down_button_last_state = LOW;
  49.  
  50. int seconds = 0;
  51. int time_delay = 0;  //0.3 ms
  52.  
  53. Adafruit_LiquidCrystal lcd_1(0);
  54.  
  55. void setup() {
  56.   pinMode(RELAY_PIN, OUTPUT);
  57.   pinMode(BUZZER_PIN, OUTPUT);
  58.  
  59.   lcd_1.begin(16, 2);
  60.   lcd_1.setCursor(0, 0);
  61.   lcd_1.print("    Kyaw Electronics");
  62.   lcd_1.setCursor(1, 1);
  63.   lcd_1.print("Spot Time ");
  64.   Serial.begin(9600);
  65. }
  66.  
  67. void loop() {
  68.   push_button.loop();
  69.   up_button.loop();
  70.   down_button.loop();
  71.  
  72.   bool push_button_state = push_button.getState();
  73.   if (push_button_state != push_button_last_state) {
  74.     push_button_last_state = push_button_state;
  75.     if (push_button_state == HIGH) {
  76.       //relay on after timer
  77.       digitalWrite(RELAY_PIN, HIGH);
  78.       digitalWrite(BUZZER_PIN, HIGH);
  79.       delay(time_delay);
  80.       digitalWrite(BUZZER_PIN, LOW);
  81.       digitalWrite(RELAY_PIN, LOW);
  82.       delay(3000);
  83.     }
  84.   }
  85.  
  86.   bool up_button_state = up_button.getState();
  87.   if (up_button_state != up_button_last_state) {
  88.     up_button_last_state = up_button_state;
  89.  
  90.     if (up_button_state == HIGH) {
  91.       //Increace delay time by 1 ms
  92.       time_delay++;
  93.       Serial.print(F("Time Delay: "));
  94.       Serial.println(time_delay);
  95.     }
  96.   }
  97.  
  98.   bool down_button_state = down_button.getState();
  99.   if (down_button_state != down_button_last_state) {
  100.     down_button_last_state = down_button_state;
  101.  
  102.     if (down_button_state == HIGH) {
  103.       //Decreace delay time by 1 ms
  104.       time_delay--;
  105.       Serial.print(F("Time Delay: "));
  106.       Serial.println(time_delay);
  107.     }
  108.   }
  109.  
  110.   lcd_1.setCursor(11, 1);
  111.   lcd_1.print(time_delay);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement