Hi!
I'm trying to make a sketch that outputs a serial code containing the status of 8 buttons connected via 2x PCF8574 chips.
The idea is that, when nothing has changed on the state of the buttons, the code gets send every second. However, if there is a change, I want it to output the code immediately.
The code gets constructed from input 2-8 from the first expander, and if expander 2 has at least one button pressed, I simply add 128 to the calculated value. This is obviously a bit strange, but this is just so for testing purposes as I don't care on which pin of the second expander the value has changed.
So: button 1 = 2^0
button 2 = 2^1
button 3 = 2^2
... and so on
This is not even the problem, it's just to give you an explanation of what I'm trying to do.
The problem lies in the interrupt procedure. When a state has been changed, the program just stops working (the led stops flashing). As long as the buttons states remain the same, everything works fine.
I've installed a pull-up resistor to the interrupt line of 100k.
This is the sketch:
#include <Wire.h>
#include <math.h>
#define expander1 B0100000 //tally 1 - 8
#define expander2 B0100001 //tally 9 - 16
int DecVal = 0;
int DecVal2 = 0;
int Start1 = 240;
int Start2 = 15;
int led = 0;
byte returnByte = 0;
byte returnByte2 = 0;
unsigned long marker = millis();
void setup(){
Serial.begin(9600);
Wire.begin();
attachInterrupt(0,plots,LOW);
pinMode(8,OUTPUT);
}
void loop(){
if(millis() - marker > 1000){
verandering(); }
}
void plots(){
returnByte = expanderRead(1);
returnByte2 = expanderRead(2);
returnByte = ~returnByte;
returnByte2 =~returnByte2;
DecVal = 0;
DecVal2 = 0;
for(int i=1;i<8;i++){
if(bitRead(returnByte,i)==1){
DecVal = DecVal + pow(2,i-1);}
if(returnByte2>0){
DecVal = DecVal + 128; }
}
Serial.write(Start1);
Serial.write(Start2);
Serial.write(DecVal);
Serial.write(DecVal);
led = 1 - led;
if(led==0){digitalWrite(8,LOW); }else { digitalWrite(8,HIGH);}
}
void verandering(){
plots();
marker = millis();
}
byte expanderRead(int id) {
byte _data;
switch(id){
case 1:
Wire.requestFrom(expander1, 1);
if(Wire.available()) {
_data = Wire.read();
}
return _data;
case 2:
Wire.requestFrom(expander2, 1);
if(Wire.available()) {
_data = Wire.read();
}
return _data;
}
}
I don't see what I'm missing, but I'm sure it's something silly. Any help is greatly appreciated!
Thanks a lot!
Jens