/* This demo code demonstrates the bidirectional possibilities
of the PCF8574 with the Wire.h -library. R.Schneider
*/
#include <Wire.h> // Required for I2C communication
#define PCF8574adr0 0x20
volatile byte PCFmerker = 0 ;
void setup() {
Serial.begin(9600);
Wire.begin( PCF8574adr0 );
pinMode( 2, INPUT_PULLUP ) ;
attachInterrupt( digitalPinToInterrupt( 2 ), isrpcf, CHANGE );
}
void isrpcf()
{ // Wenn LOW-Pegel, dann merken!
if( digitalRead(2) == LOW ) PCFmerker = 1;
if( digitalRead(2) == HIGH ) PCFmerker = 2;
}
byte mirror( byte input )
{
static String col[4] = {"blau ", "gr\365n ", "gelb ", "rot " };
byte output = 0xFF, i = 0;
input = (~input) & 0x0F ;
if( input & 0x01 ) { output &= 0x7F; i=0; }
if( input & 0x02 ) { output &= 0xBF; i=1; }
if( input & 0x04 ) { output &= 0xDF; i=2; }
if( input & 0x08 ) { output &= 0xEF; i=3; }
if( input > 0 )
{ Serial.print( String(input) + " " + col[i] );
Wire.beginTransmission(PCF8574adr0);
Wire.write( output );
Wire.endTransmission();
//delay(100);
}
return( output);
}
void loop() {
static byte input =0, output=0, isrZaehler = 0;
if( PCFmerker == 1 )
{
Wire.requestFrom( PCF8574adr0 , 1);
if (Wire.available()) {
input = Wire.read();
}
Wire.endTransmission();
output = mirror( input );
PCFmerker = 0 ;
isrZaehler++;
if( isrZaehler % 10 == 0 ) Serial.println();
}
if( PCFmerker > 1 )
{
Wire.beginTransmission(PCF8574adr0);
Wire.write( 0xFF ); // 4 LEDs löschen
Wire.endTransmission();
PCFmerker = 0;
}
// M8 etwas anderes Sinnvolles!
}
Address bits 2,1,0