Hello everyone,
I have been having these strange interrupt problems that i cant seem to solve. I am making an irrigation computer that has 64 valves that run on 24v ac connected to relayboards, and 4 water meters with hardware debouncing http://hackaday.com/2015/12/09/embed-with-elliot-debounce-your-noisy-buttons-part-i/ (http://hackaday.com/2015/12/09/embed-with-elliot-debounce-your-noisy-buttons-part-i/)
i have been having this problem for a while and have tried multiple fixes, first one i tried was making the relayboard optoisolated as explained in this artticle https://forum.arduino.cc/index.php?topic=365156.30 (https://forum.arduino.cc/index.php?topic=365156.30)
but no luck the problem is less but still not gone, in a different place where i have just one relay board i replaced them with solidstate relays and the problem is 100% gone but in this case i can not do that, so i know it is the relay boards causing the issue.
i also tested keeping the arduinos separate from each other where the water meter arduino was powered through my laptop and opening the relays still caused the interrupt to trigger.
to make things worse if i do not attach the debouncer but just have lose wires of about 10cm attached it still triggers.
I attached an image of what my system kind of looks like and here is my interrupt code should be fairly strait forward.
#include <Wire.h>
#include <Arduino.h>
//--------------------------------------------------------------
//I2C
uint8_t Address = 0x09; //I2C address
void ReceiveEvent(int DataAvailable);
void RequestCallback();
uint8_t Command = 0;
//--------------------------------------------------------------
//--------------------------------------------------------------
//clock interrupt stuff
const uint8_t Clock1Pin = 3;
const uint8_t Clock2Pin = 4;
const uint8_t Clock3Pin = 18;
const uint8_t Clock4Pin = 19;
volatile uint16_t values[4] = { 0,0,0,0 };
void Clock1() { values[0]++; };
void Clock2() { values[1]++; };
void Clock3() { values[2]++; };
void Clock4() { values[3]++; };
void InitInterupt();
void ResetClock(uint8_t clock) { values[clock] = 0; }
uint16_t GetClockData(uint8_t clock);
//--------------------------------------------------------------
void setup()
{
Wire.begin(Address); // join i2c bus with address #8
Wire.onReceive(ReceiveEvent); // MAster sends data
Wire.onRequest(RequestCallback); // Master Requests data
#ifdef _DEBUG
Serial.begin(9600);
#endif
InitInterupt();
}
// Interrupt initialization for the 4 clocks
void InitInterupt()
{
pinMode(Clock1Pin, INPUT);
pinMode(Clock2Pin, INPUT);
pinMode(Clock3Pin, INPUT);
pinMode(Clock4Pin, INPUT);
// Attach an interrupt to the ISR vector
attachInterrupt(digitalPinToInterrupt(Clock1Pin), Clock1, FALLING);
attachInterrupt(digitalPinToInterrupt(Clock2Pin), Clock2, FALLING);
attachInterrupt(digitalPinToInterrupt(Clock3Pin), Clock3, FALLING);
attachInterrupt(digitalPinToInterrupt(Clock4Pin), Clock4, FALLING);
}
uint16_t GetClockData(uint8_t clock)
{
uint16_t val = values[clock];
ResetClock(clock);
return val;
}
void loop()
{
#ifdef _DEBUG
Serial.print("Clock1: ");
Serial.println(values[0]);
Serial.print("Clock2: ");
Serial.println(values[1]);
Serial.print("Clock3: ");
Serial.println(values[2]);
Serial.print("Clock4: ");
Serial.println(values[3]);
#endif
}
/*I2C Commands
0x00 = do nothing
0x07 = send data back the values of all the clocks
0x01 = clock1
0x02 = clock1
0x03 = clock2
0x04 = clock3
*/
void ReceiveEvent(int DataAvailable)
{
#ifdef _DEBUG
Serial.print("Got data ");
Serial.println(DataAvailable);
#endif
// set requested data
if (DataAvailable == 1)
Command = Wire.read();
}
void RequestCallback()
{
if (Command == 0x07)
SendAllClockData();
else if (Command >= 0x01 && Command <= 0x04)
SendClockData();
}
void SendClockData()
{
#ifdef _DEBUG
Serial.print("Sending back clock data for");
Serial.println(Command);
#endif
uint8_t Buffer[sizeof(uint16_t)];
memset(Buffer, 0, sizeof(uint8_t));
uint8_t sensor = Command - 1;
*(uint16_t*)Buffer = GetClockData(sensor);
Wire.write(Buffer, sizeof(uint16_t));
Command = 0;
}
void SendAllClockData()
{
#ifdef _DEBUG
Serial.println("Sending all clock data");
#endif
const uint8_t BufferSize = 4;
const uint8_t intsize = sizeof(uint16_t);
uint8_t Buffer[BufferSize * intsize];
memset(Buffer, 0, BufferSize * intsize);
for (size_t i = 0; i < BufferSize; i++)
{
*((uint16_t*)(Buffer + (i * intsize))) = GetClockData(i);
}
Wire.write(Buffer, BufferSize * intsize);
#ifdef _DEBUG
for (size_t i = 0; i < BufferSize * intsize; i++)
{
Serial.print(Buffer[i]);
Serial.print(",");
}
Serial.println();
#endif
Command = 0;
}
tell me if you need any more information