Advertisement
DrAungWinHtut

WeldingMachine.ino

Nov 3rd, 2022
1,150
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.  
  6. const int push_button = 10;
  7. const int up_button = 9;
  8. const int down_button = 8;
  9. const int buzzer = 11;
  10. const int relay = 2;
  11.  
  12. int push_button_state = LOW;
  13. int push_button_last_state = LOW;
  14. int up_button_state = LOW;
  15. int up_button_last_state = LOW;
  16. int down_button_state = LOW;
  17. int down_button_last_state = LOW;
  18.  
  19. unsigned long lastDebounceTime_push_button = 0;  // the last time the output pin was toggled
  20. unsigned long lastDebounceTime_up_button = 0;
  21. unsigned long lastDebounceTime_down_button = 0;
  22. unsigned long debounceDelay = 500;  // the debounce time; increase if the output flickers
  23.  
  24. int seconds = 0;
  25. int time_delay = 0;  //0.3 ms
  26.  
  27. Adafruit_LiquidCrystal lcd_1(0);
  28.  
  29. void setup() {
  30.   pinMode(push_button, INPUT);
  31.   pinMode(up_button, INPUT);
  32.   pinMode(down_button, INPUT);
  33.   pinMode(relay, OUTPUT);   //relay
  34.   pinMode(buzzer, OUTPUT);  //buzzer
  35.  
  36.   lcd_1.begin(16, 2);
  37.   lcd_1.setCursor(0, 0);
  38.   lcd_1.print("    Kyaw Electronics");
  39.   lcd_1.setCursor(1, 1);
  40.   lcd_1.print("Spot Time ");
  41.   Serial.begin(9600);
  42. }
  43.  
  44. void loop() {
  45.   // read the state of the switch into a local variable:
  46.   int reading_push_button = digitalRead(push_button);
  47.   Serial.print("Push button state ");
  48.   Serial.println(reading_push_button);
  49.   int reading_up_button = digitalRead(up_button);
  50.   int reading_down_button = digitalRead(down_button);
  51.  
  52.   // check to see if you just pressed the button
  53.   // (i.e. the input went from LOW to HIGH), and you've waited long enough
  54.   // since the last press to ignore any noise:
  55.  
  56.   // If the switch changed, due to noise or pressing:
  57.   if (reading_push_button != push_button_last_state) {
  58.     // reset the debouncing timer
  59.     Serial.print("Push button state changed ");
  60.     Serial.println(reading_push_button);
  61.     lastDebounceTime_push_button = millis();
  62.   }
  63.  
  64.   if ((millis() - lastDebounceTime_push_button) > debounceDelay) {
  65.     // whatever the reading is at, it's been there for longer than the debounce
  66.     // delay, so take it as the actual current state:
  67.     Serial.print("Debounce over ");
  68.     Serial.println(reading_push_button);
  69.     // if the button state has changed:
  70.     if (reading_push_button != push_button_state) {
  71.       push_button_state = reading_push_button;
  72.  
  73.  
  74.       // only toggle the LED if the new button state is HIGH
  75.       if (push_button_state == HIGH) {
  76.         //relay on after timer
  77.         digitalWrite(relay, HIGH);
  78.         digitalWrite(buzzer, HIGH);
  79.         delay(time_delay);
  80.         digitalWrite(relay, LOW);
  81.         digitalWrite(buzzer, LOW);
  82.         delay(3000);
  83.       }
  84.     }
  85.   }
  86.  
  87.   // If the switch changed, due to noise or pressing:
  88.   if (reading_up_button != up_button_last_state) {
  89.     // reset the debouncing timer
  90.     lastDebounceTime_up_button = millis();
  91.   }
  92.  
  93.   if ((millis() - lastDebounceTime_up_button) > debounceDelay) {
  94.     // whatever the reading is at, it's been there for longer than the debounce
  95.     // delay, so take it as the actual current state:
  96.  
  97.     // if the button state has changed:
  98.     if (reading_up_button != up_button_state) {
  99.       up_button_state = reading_up_button;
  100.       lcd_1.setCursor(11, 1);
  101.       lcd_1.print("p");
  102.       // only toggle the LED if the new button state is HIGH
  103.       if (up_button_state == HIGH) {
  104.         //Increace delay time by 1 ms
  105.         time_delay++;
  106.         Serial.print("Time Delay: ");
  107.         Serial.println(time_delay);
  108.         lcd_1.setCursor(11, 1);
  109.         lcd_1.print(time_delay);
  110.       }
  111.     }
  112.   }
  113.  
  114.   // If the switch changed, due to noise or pressing:
  115.   if (reading_down_button != down_button_last_state) {
  116.     // reset the debouncing timer
  117.     lastDebounceTime_down_button = millis();
  118.   }
  119.  
  120.   if ((millis() - lastDebounceTime_down_button) > debounceDelay) {
  121.     // whatever the reading is at, it's been there for longer than the debounce
  122.     // delay, so take it as the actual current state:
  123.  
  124.     // if the button state has changed:
  125.     if (reading_down_button != down_button_state) {
  126.       down_button_state = reading_down_button;
  127.  
  128.       // only toggle the LED if the new button state is HIGH
  129.       if (reading_down_button == HIGH) {
  130.         //Decreace delay time by 1 ms
  131.         time_delay--;
  132.         Serial.print("Time Delay: ");
  133.         Serial.println(time_delay);
  134.         lcd_1.setCursor(11, 1);
  135.         lcd_1.print(time_delay);
  136.       }
  137.     }
  138.   }
  139.  
  140.  
  141.  
  142.   // save the reading. Next time through the loop, it'll be the lastButtonState:
  143.   push_button_state = reading_push_button;
  144.   up_button_state = reading_up_button;
  145.   down_button_state = reading_down_button;
  146.  
  147.   lcd_1.setCursor(11, 1);
  148.   lcd_1.print(time_delay);
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement