Hello,
We are making a Domotica project. For my Arduino (with xbee) i have to read what I press on a keypad through I2C and check if I receive a package from the server. These 2 things are in my loop() method. It works fine at very low server speeds, but not if the server is going fast. My module then acknoladges too late. We think this is because my module is waiting too long in the I2C for the keypad.
Here is my code:
void setup()
{
//Startconfiguratie
delay(8000);
Serial.begin(19200);
Serial.setTimeout(1500);
Wire.begin();
//Initialisatie voor alarm en segmentdisplay
clearCode();
pinMode(pinLed, OUTPUT);
pinMode(pinData, OUTPUT);
pinMode(pinClock, OUTPUT);
pinMode(pinClear, OUTPUT);
digitalWrite(pinClear, LOW);
digitalWrite(pinClear, HIGH);
// Timer1.initialize(8000);
// Timer1.attachInterrupt(receiveandsend);
//Initialisatie voor keypad
pcf8574_write(pcf8574_i2c_addr, 0xff);
row_select = 0;
setAlarm(false);
}
void loop()
{
if(getAlarm())
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
{
ledState = HIGH;
}
else
{
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(pinLed, ledState);
}
}
if(receivePackage())
{
if(TADDRESS == 0x01)
{
if((int)rData[7] == 0x00)
{
makePacket();
sendPackage(0x01, data, true);
if(readyToSend)
{
clearCode();
digitalWrite(pinClear, LOW);
digitalWrite(pinClear, HIGH);
writeNumber(10);
write_high();
writeNumber(10);
write_high();
writeNumber(10);
write_high();
writeNumber(10);
}
clearData();
}
else
{
//Doe niks
}
}
else if(TADDRESS == 0x03)
{
if((int)rData[7] == 0x01)
{
setAlarm(true);
}
else if((int)rData[7] == 0x00)
{
setAlarm(false);
}
}
}
key = get_key();
if(key != '\0')
{
enterCode(key);
}
}
boolean receivePackage()
{
while(Serial.read() != START_BYTE)
{
delayMicroseconds(5);
}
boolean valid = true;
while(Serial.available() < 11)
{
delayMicroseconds(3);
}
TADDRESS = Serial.read();
if(Serial.read() == MY_ADDRESS)
{
//Pakketje voor mij
int i;
for(i = 0; i < 8; i++)
{
rData[i] = Serial.read();
}
if(Serial.read() != STOP_BYTE)
{
valid = false;
notAcknowledged();
}
else
{
acknowledged();
}
}
else
{
//Pakketje niet voor mij
valid = false;
int i;
for(i = 0; i < 8; i++)
{
rData[i] = Serial.read();
}
Serial.read();
int tmp = 0;
tmp = Serial.readBytes(rData, 1);
if((tmp == 0) && (MY_ADDRESS == 0x01))
{
notAcknowledged();
}
}
return valid;
}
char get_key()
{
static int temp_key;
int tmp_data;
int r;
int key = '\0';
// Zoeken naar LAGE row
pcf8574_write(pcf8574_i2c_addr, pcf8574_row_data[row_select]);
for(r=0;r<num_cols;r++)
{
// Port data uitlezen
tmp_data = pcf8574_byte_read(pcf8574_i2c_addr);
// XOR om de ontvangen data met de huidige data te vergelijken
tmp_data ^= current_data;
// Key is ingedrukt
if(col[r] == tmp_data)
{
temp_key = keymap[row_select][r];
return '\0';
}
}
// Key is ingedrukt en dan losgelaten
if((key == '\0') && (temp_key != '\0'))
{
key = temp_key;
temp_key = '\0';
return key;
}
// Alle ports weer hoog zetten
pcf8574_write(pcf8574_i2c_addr, 0xff);
// Volgende rij...
row_select++;
if(row_select == num_rows)
{
row_select = 0;
}
return key;
}
I have shown only the relevant methods which I call in my loop() method. The method get_key() uses I2C for the keypad and the method receivePackage() uses the communication with xbee and server. As you can see I am NOT working with interrupts. Should I do this, if so, how should I do this? What needs to be in an interrupt?