2016-05-20 08:33:42 -04:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @Author Septillion (https://github.com/sseptillion)
|
|
|
|
* @date 2016-05-20
|
|
|
|
* @brief Example how to use buttonRead8()
|
|
|
|
*
|
|
|
|
* buttonRead8() works the same as buttonRead() but works on all
|
|
|
|
* pins simultaneous. You can give a mask as parameter to decide which
|
|
|
|
* pins are affected (aka, made high for a short periode) during
|
|
|
|
* a buttonRead8().
|
|
|
|
*
|
|
|
|
* The mask can also be set with setButtonMask(mask). Calling buttonRead8()
|
|
|
|
* after that will be the same as calling buttonRead8(mask).
|
|
|
|
*
|
|
|
|
* ButtonRead8() can also be used for pins that are input only.
|
|
|
|
*
|
|
|
|
* In this example pin 0 of the PCF8574 (address 0x20) is used to blink a
|
|
|
|
* LED but at the same time it can be used to toggle the onboard LED of
|
|
|
|
* the Arduino.
|
|
|
|
*
|
|
|
|
* Pin 1 is also toggle but isn't affected by the buttonRead8();
|
|
|
|
*
|
|
|
|
* NOTE: The button will affect the LED on the same pin. The led will light up
|
|
|
|
* no matter the set output state when you press the button.
|
|
|
|
*/
|
|
|
|
|
2021-12-23 06:51:26 -05:00
|
|
|
|
2016-05-20 08:33:42 -04:00
|
|
|
#include <PCF8574.h>
|
|
|
|
#include <Wire.h>
|
|
|
|
|
2021-12-23 06:51:26 -05:00
|
|
|
|
2016-05-20 08:33:42 -04:00
|
|
|
PCF8574 pcf20(0x20);
|
|
|
|
|
|
|
|
const byte onboardLed = 13;
|
|
|
|
const byte PcfButtonLedPin = 0;
|
|
|
|
const byte PcfLedPin = 1;
|
|
|
|
|
|
|
|
unsigned int blinkMillis;
|
|
|
|
unsigned int buttonMillis;
|
|
|
|
|
2021-12-23 06:51:26 -05:00
|
|
|
|
2016-05-20 08:33:42 -04:00
|
|
|
void setup() {
|
|
|
|
Serial.begin(115200);
|
|
|
|
pcf20.begin();
|
|
|
|
|
|
|
|
pinMode(onboardLed, OUTPUT);
|
|
|
|
|
2021-12-23 06:51:26 -05:00
|
|
|
// As alternative to adding the mask to buttonRead8() every time
|
|
|
|
// you can set it once.
|
|
|
|
// Without setting a mask buttonRead8() will effect ALL pins.
|
|
|
|
// Not a problem when using things like LEDs.
|
|
|
|
// pcf20.setButtonMask(_BV(PcfButtonLedPin));
|
2016-05-20 08:33:42 -04:00
|
|
|
}
|
|
|
|
|
2021-12-23 06:51:26 -05:00
|
|
|
|
2016-05-20 08:33:42 -04:00
|
|
|
void loop() {
|
|
|
|
static bool state;
|
|
|
|
unsigned int currentMillis = millis();
|
|
|
|
|
2021-12-23 06:51:26 -05:00
|
|
|
// Limit button read to 20 times a second
|
|
|
|
// Fast enough for most buttons
|
|
|
|
// but this way you don't have a dimmer output because it's blanked during button read
|
|
|
|
// a read takes 460us t 16Mhz Arduino and normal I2C speed.
|
2016-05-20 08:33:42 -04:00
|
|
|
if(currentMillis - buttonMillis >= 50){
|
|
|
|
buttonMillis = currentMillis;
|
|
|
|
|
2021-12-23 06:51:26 -05:00
|
|
|
// read all states but only force PcfButtonLedPin HIGH during the
|
|
|
|
// buttonRead8()
|
|
|
|
// Alternatively the mask could have been set with setButtonMask().
|
|
|
|
// Then the mask can be omitted here. See setup()
|
|
|
|
// byte inputStates = pcf20.readButton8(_BV(PcfButtonLedPin));
|
2021-01-29 06:31:58 -05:00
|
|
|
byte inputStates = pcf20.readButton8(1 << PcfButtonLedPin); // Keep Arduino-CI happy
|
2016-05-20 08:33:42 -04:00
|
|
|
|
2021-12-23 06:51:26 -05:00
|
|
|
// check the bit of PcfButtonLedPin
|
2016-05-20 08:33:42 -04:00
|
|
|
if(state != bitRead(inputStates, PcfButtonLedPin)){
|
|
|
|
if(state){
|
2021-12-23 06:51:26 -05:00
|
|
|
// toggle the LED
|
2016-05-20 08:33:42 -04:00
|
|
|
digitalWrite(onboardLed, !digitalRead(onboardLed));
|
|
|
|
}
|
|
|
|
state = !state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 06:51:26 -05:00
|
|
|
// Lets blink the same output
|
2016-05-20 08:33:42 -04:00
|
|
|
if(currentMillis - blinkMillis >= 500){
|
2021-12-23 06:51:26 -05:00
|
|
|
// Update time
|
2016-05-20 08:33:42 -04:00
|
|
|
blinkMillis = currentMillis;
|
|
|
|
|
|
|
|
pcf20.toggle(PcfButtonLedPin);
|
|
|
|
pcf20.toggle(PcfLedPin);
|
|
|
|
Serial.println(pcf20.read8(), BIN);
|
|
|
|
}
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
2021-12-23 06:51:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|