Advertisement
granteeric

adam exemple code

Apr 18th, 2022
1,234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #define heatPin     5
  3. #define buttonPin1  2
  4. #define buttonPin2  3
  5. #define buttonPin3  3
  6. #define heaterTime1 1000      //en millisecond
  7. #define heaterTime2 1000      //en millisecond
  8. #define heaterTime3 1000      //en millisecond
  9. #define loadTime    1000      //en millisecond
  10.  
  11. unsigned long heaterTime1Millis = 0;
  12. unsigned long heaterTime2Millis = 0;
  13. unsigned long heaterTime3Millis = 0;
  14. uint8_t buttonPress = 0 ;               //button pressed
  15.  
  16. //action for btn 1 pressed
  17. void actionBtn1Pressed(){
  18.     buttonPress = 1;
  19.     heaterTime1Millis = millis();
  20.     digitalWrite(heatPin, HIGH);
  21. }
  22.  
  23. void actionBtn2Pressed(){
  24.     buttonPress = 2;
  25.     heaterTime2Millis = millis();
  26.     digitalWrite(heatPin, HIGH);
  27. }
  28.  
  29. void actionBtn3Pressed(){
  30.     buttonPress = 3;
  31.     heaterTime3Millis = millis();
  32.     digitalWrite(heatPin, HIGH);
  33. }
  34.  
  35. void endActionButton(){
  36.           buttonPress = 0 ;
  37.           digitalWrite(heatPin, LOW);
  38.           delay(loadTime);
  39. }
  40.  
  41.  
  42. void setup(){
  43.   pinMode(buttonPin1, INPUT_PULLUP);
  44.   pinMode(buttonPin2, INPUT_PULLUP);
  45.   pinMode(buttonPin3, INPUT_PULLUP);
  46.   pinMode(heatPin, OUTPUT);
  47. }
  48.  
  49. void loop(){
  50.  
  51.   //!buttonPress ->  buttonPress == 0 or buttonPress == false  
  52.   if( !digitalRead(buttonPin1) && !buttonPress) actionBtn1Pressed();
  53.  
  54.   else if( !digitalRead(buttonPin2) && !buttonPress) actionBtn2Pressed();
  55.  
  56.   else if( !digitalRead(buttonPin3) && !buttonPress) actionBtn3Pressed();
  57.  
  58.   switch (buttonPress)
  59.   {
  60.   case 1:
  61.       if( (millis() - heaterTime1Millis ) >= heaterTime1){
  62.           //end actionBtn1
  63.           endActionButton();
  64.       }
  65.     break;
  66.   case 2:
  67.       if( (millis() - heaterTime2Millis ) >= heaterTime2){
  68.           //end actionBtn2
  69.           endActionButton();
  70.       }
  71.     break;
  72.   case 3:
  73.       if( (millis() - heaterTime3Millis ) >= heaterTime3){
  74.           //end actionBtn3
  75.           endActionButton();
  76.       }
  77.     break;
  78.  
  79.   default:
  80.     break;
  81.   }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement