Below is the source code for the Arduino Mega. It works as a slave to the Espduino, basically just passing any pins that are detected to Serial3.
bool triggerpins[100];
bool triggerpinvalues[100];
unsigned long triggerlast[100];
int PinCount=53;
bool DoDebug=1;
int NotifyOnceEvery=1000; //only one notification per second max to stop flooding the serial line
int HelloEvery=5000; //send a keepalive hello on this interval
unsigned long LastHello;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial3.begin(115200);
SerialDebugLn("");
SerialDebugLn("Rail Mega Sensor v2");
SerialDebugLn("Getting Initial Pin Values");
LastHello=millis();
for (int pin = 0; pin <= PinCount; pin++) {
triggerpins[pin]=0;
switch (pin) {
case 0:break; //serial
case 1:break; //serial
case 14:break; //Serial 2
case 15:break; //Serial 2
case 16:break; //Serial 3
case 17:break; //Serial 3
case 18:break; //Serial 1
case 19:break; //Serial 1
default:
pinMode(pin, INPUT_PULLUP);
digitalWrite(pin,1);
int state = digitalRead(pin);
triggerpins[pin]=1;
triggerpinvalues[pin]=state;
SerialDebug("Starting with Pin ");
SerialDebug(pin);
SerialDebug(" value ");
SerialDebugLn(triggerpinvalues[pin]);
}
}
for (int pin = 0; pin <= PinCount; pin++) {
if (triggerpins[pin]==1) {
SerialDebug("For Pin ");
SerialDebug(pin);
SerialDebug(" monitoring? ");
SerialDebug(triggerpins[pin]);
SerialDebug(" value ");
SerialDebugLn(triggerpinvalues[pin]);
}
}
SerialDebugLn("Done!");
}
void loop() {
for (int pin = 0; pin <= PinCount; pin++) {
if (triggerpins[pin]==1) {
int state = digitalRead(pin);
if ( (state!=triggerpinvalues[pin]) && ((millis()-triggerlast[pin])>NotifyOnceEvery) ) {
Serial3.print("PINVALUE\t");
Serial3.print(pin+100); //add 100 for mega pins
Serial3.print("\t");
Serial3.print(state);
Serial3.print("\r");
SerialDebug("Pin ");
SerialDebug(pin);
SerialDebug(" has changed from ");
SerialDebug(triggerpinvalues[pin]);
SerialDebug(" to ");
SerialDebugLn(state);
triggerlast[pin]=millis();
}
triggerpinvalues[pin]=state;
}
}
if (millis()-LastHello>=HelloEvery) {
Serial3.print("MEGAHELLO\r");
LastHello=millis();
}
}
void SerialDebugLn(String data) {
if (DoDebug) {
Serial.println(data);
}
}
void SerialDebugLn(int data) {
if (DoDebug) {
Serial.println(data);
}
}
void SerialDebug(String data) {
if (DoDebug) {
Serial.print(data);
}
}
void SerialDebug(int data) {
if (DoDebug) {
Serial.print(data);
}
}
bool triggerpins[100];
bool triggerpinvalues[100];
unsigned long triggerlast[100];
int PinCount=53;
bool DoDebug=1;
int NotifyOnceEvery=1000; //only one notification per second max to stop flooding the serial line
int HelloEvery=5000; //send a keepalive hello on this interval
unsigned long LastHello;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial3.begin(115200);
SerialDebugLn("");
SerialDebugLn("Rail Mega Sensor v2");
SerialDebugLn("Getting Initial Pin Values");
LastHello=millis();
for (int pin = 0; pin <= PinCount; pin++) {
triggerpins[pin]=0;
switch (pin) {
case 0:break; //serial
case 1:break; //serial
case 14:break; //Serial 2
case 15:break; //Serial 2
case 16:break; //Serial 3
case 17:break; //Serial 3
case 18:break; //Serial 1
case 19:break; //Serial 1
default:
pinMode(pin, INPUT_PULLUP);
digitalWrite(pin,1);
int state = digitalRead(pin);
triggerpins[pin]=1;
triggerpinvalues[pin]=state;
SerialDebug("Starting with Pin ");
SerialDebug(pin);
SerialDebug(" value ");
SerialDebugLn(triggerpinvalues[pin]);
}
}
for (int pin = 0; pin <= PinCount; pin++) {
if (triggerpins[pin]==1) {
SerialDebug("For Pin ");
SerialDebug(pin);
SerialDebug(" monitoring? ");
SerialDebug(triggerpins[pin]);
SerialDebug(" value ");
SerialDebugLn(triggerpinvalues[pin]);
}
}
SerialDebugLn("Done!");
}
void loop() {
for (int pin = 0; pin <= PinCount; pin++) {
if (triggerpins[pin]==1) {
int state = digitalRead(pin);
if ( (state!=triggerpinvalues[pin]) && ((millis()-triggerlast[pin])>NotifyOnceEvery) ) {
Serial3.print("PINVALUE\t");
Serial3.print(pin+100); //add 100 for mega pins
Serial3.print("\t");
Serial3.print(state);
Serial3.print("\r");
SerialDebug("Pin ");
SerialDebug(pin);
SerialDebug(" has changed from ");
SerialDebug(triggerpinvalues[pin]);
SerialDebug(" to ");
SerialDebugLn(state);
triggerlast[pin]=millis();
}
triggerpinvalues[pin]=state;
}
}
if (millis()-LastHello>=HelloEvery) {
Serial3.print("MEGAHELLO\r");
LastHello=millis();
}
}
void SerialDebugLn(String data) {
if (DoDebug) {
Serial.println(data);
}
}
void SerialDebugLn(int data) {
if (DoDebug) {
Serial.println(data);
}
}
void SerialDebug(String data) {
if (DoDebug) {
Serial.print(data);
}
}
void SerialDebug(int data) {
if (DoDebug) {
Serial.print(data);
}
}
Comments
Post a Comment