Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #define fixture4Button 0
- #define fixture5Button A3
- #define fixture1 3
- #define fixture2 4
- #define fixture3 5
- #define fixture4 6
- #define fixture5 7
- #define fixture6 8
- #define fixture7 9
- #define fixture8 10
- unsigned char fixtureArray[8] = {fixture1, fixture2, fixture3, fixture4,
- fixture5, fixture6, fixture7, fixture8};
- class PushButton {
- public:
- PushButton(uint8_t pin) // Constructor (executes when a PushButton object is
- // created)
- : pin(pin) { // remember the push button pin
- pinMode(pin, INPUT_PULLUP); // enable the internal pull-up resistor
- };
- bool isPressed() // read the button state check if the button has been
- // pressed, debounce the button as well
- {
- bool pressed = false;
- bool state = digitalRead(pin); // read the button's state
- int8_t stateChange =
- state - previousState; // calculate the state change since last time
- if (stateChange ==
- falling) { // If the button is pressed (went from high to low)
- if (millis() - previousBounceTime >
- debounceTime) { // check if the time since the last bounce is higher
- // than the threshold
- pressed = true; // the button is pressed
- }
- }
- if (stateChange == rising) { // if the button is released or bounces
- previousBounceTime = millis(); // remember when this happened
- }
- previousState = state; // remember the current state
- // return pressed; // return true if the button was pressed and didn't
- // bounce
- if (state == LOW) {
- return true;
- } else {
- return false;
- }
- };
- private:
- uint8_t pin;
- bool previousState = HIGH;
- unsigned long previousBounceTime = 0;
- const static unsigned long debounceTime = 25;
- const static int8_t rising = HIGH - LOW;
- const static int8_t falling = LOW - HIGH;
- };
- void massPinMode(unsigned char pins[], unsigned char state) {
- unsigned char i;
- for (i = 0; i < sizeof fixtureArray; i++) pinMode(pins[i], state);
- }
- void massDigitalWrite(unsigned char pins[], unsigned char length,
- unsigned char state) {
- unsigned char i;
- for (i = 0; i < length; i++) digitalWrite(pins[i], state);
- }
- PushButton Button4(fixture4Button);
- PushButton Button5(fixture5Button);
- void setup() {
- Serial.begin(9600);
- Serial.println("ready!");
- massPinMode(fixtureArray, OUTPUT);
- massDigitalWrite(fixtureArray, sizeof fixtureArray, HIGH);
- }
- void loop() {
- if (Button4.isPressed()) {
- digitalWrite(fixture4, LOW);
- }
- if (!Button4.isPressed()) {
- digitalWrite(fixture4, HIGH);
- }
- if (Button5.isPressed()) {
- digitalWrite(fixture5, LOW);
- }
- if (!Button5.isPressed()) {
- digitalWrite(fixture5, HIGH);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement