123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #include "Arduino.h"
- #include "PCF8574.h"
- int encoderPinA = P0;
- int encoderPinB = P1;
- #define INTERRUPTED_PIN D7
- void ICACHE_RAM_ATTR updateEncoder();
- PCF8574 pcf8574(0x38, INTERRUPTED_PIN, updateEncoder);
- volatile long encoderValue = 0;
- uint8_t encoderButtonVal = HIGH;
- void setup()
- {
- Serial.begin (9600);
- delay(500);
-
- pcf8574.pinMode(encoderPinA, INPUT_PULLUP);
- pcf8574.pinMode(encoderPinB, INPUT_PULLUP);
-
- pcf8574.pinMode(P2, INPUT_PULLUP);
-
-
- pcf8574.setLatency(0);
-
- Serial.print("Init pcf8574...");
- if (pcf8574.begin()){
- Serial.println("OK");
- }else{
- Serial.println("KO");
- }
- }
- bool changed = false;
- void loop()
- {
- if (changed){
- Serial.print("ENCODER --> ");
- Serial.print(encoderValue);
- Serial.print(" - BUTTON --> ");
- Serial.println(encoderButtonVal?"HIGH":"LOW");
- changed = false;
- }
- }
- uint8_t encoderPinALast = LOW;
- uint8_t valPrecEncoderButton = LOW;
- void updateEncoder(){
-
- uint8_t n = pcf8574.digitalRead(encoderPinA);
- if ((encoderPinALast == LOW) && (n == HIGH)) {
- if (pcf8574.digitalRead(encoderPinB) == LOW) {
- encoderValue--;
- changed = true;
- } else {
- encoderValue++;
- changed = true;
- }
- }
- encoderPinALast = n;
-
- encoderButtonVal = pcf8574.digitalRead(P2);
- if (encoderButtonVal!=valPrecEncoderButton){
- changed = true;
- valPrecEncoderButton = encoderButtonVal;
- }
- }
|