Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <Arduino.h>
  2.  
  3. #define fixture4Button 0
  4. #define fixture5Button A3
  5.  
  6. #define fixture1 3
  7. #define fixture2 4
  8. #define fixture3 5
  9. #define fixture4 6
  10. #define fixture5 7
  11. #define fixture6 8
  12. #define fixture7 9
  13. #define fixture8 10
  14.  
  15. unsigned char fixtureArray[8] = {fixture1, fixture2, fixture3, fixture4,
  16.                                  fixture5, fixture6, fixture7, fixture8};
  17.  
  18. class PushButton {
  19.  public:
  20.   PushButton(uint8_t pin)  // Constructor (executes when a PushButton object is
  21.                            // created)
  22.       : pin(pin) {         // remember the push button pin
  23.     pinMode(pin, INPUT_PULLUP);  // enable the internal pull-up resistor
  24.   };
  25.   bool isPressed()  // read the button state check if the button has been
  26.                     // pressed, debounce the button as well
  27.   {
  28.     bool pressed = false;
  29.     bool state = digitalRead(pin);  // read the button's state
  30.     int8_t stateChange =
  31.         state - previousState;  // calculate the state change since last time
  32.  
  33.     if (stateChange ==
  34.         falling) {  // If the button is pressed (went from high to low)
  35.       if (millis() - previousBounceTime >
  36.           debounceTime) {  // check if the time since the last bounce is higher
  37.                            // than the threshold
  38.         pressed = true;    // the button is pressed
  39.       }
  40.     }
  41.     if (stateChange == rising) {      // if the button is released or bounces
  42.       previousBounceTime = millis();  // remember when this happened
  43.     }
  44.  
  45.     previousState = state;  // remember the current state
  46.     // return pressed;  // return true if the button was pressed and didn't
  47.     // bounce
  48.     if (state == LOW) {
  49.       return true;
  50.     } else {
  51.       return false;
  52.     }
  53.   };
  54.  
  55.  private:
  56.   uint8_t pin;
  57.   bool previousState = HIGH;
  58.   unsigned long previousBounceTime = 0;
  59.  
  60.   const static unsigned long debounceTime = 25;
  61.   const static int8_t rising = HIGH - LOW;
  62.   const static int8_t falling = LOW - HIGH;
  63. };
  64.  
  65. void massPinMode(unsigned char pins[], unsigned char state) {
  66.   unsigned char i;
  67.   for (i = 0; i < sizeof fixtureArray; i++) pinMode(pins[i], state);
  68. }
  69.  
  70. void massDigitalWrite(unsigned char pins[], unsigned char length,
  71.                       unsigned char state) {
  72.   unsigned char i;
  73.   for (i = 0; i < length; i++) digitalWrite(pins[i], state);
  74. }
  75.  
  76. PushButton Button4(fixture4Button);
  77. PushButton Button5(fixture5Button);
  78.  
  79. void setup() {
  80.   Serial.begin(9600);
  81.   Serial.println("ready!");
  82.   massPinMode(fixtureArray, OUTPUT);
  83.   massDigitalWrite(fixtureArray, sizeof fixtureArray, HIGH);
  84. }
  85.  
  86. void loop() {
  87.   if (Button4.isPressed()) {
  88.     digitalWrite(fixture4, LOW);
  89.   }
  90.   if (!Button4.isPressed()) {
  91.     digitalWrite(fixture4, HIGH);
  92.   }
  93.   if (Button5.isPressed()) {
  94.     digitalWrite(fixture5, LOW);
  95.   }
  96.   if (!Button5.isPressed()) {
  97.     digitalWrite(fixture5, HIGH);
  98.   }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement