I've set up a master slave TTL network with 2 megas. The master mega turns a pin high which triggers an interrupt on the rising edge of the signal that the slave catches. The slave sends data back through the TX RX lines. This works well in theory but applying it is difficult.
So far I've had to physically apply 5V to the interrupt to get it to trigger. Once triggered data is sent to the master and the screen just like it should be.
The pin I programmed on the master to trigger the slave works fine according to the oscilloscope. It rises to 5V then drops to ground. However once I connect it to the slave it doesn't do it's job.
Anybody have any suggestions on what to improve or why the trigger pin and interrupt method isn't working?
master code
void loop(){
readEncoder(Ein);
intParse(encoderOutput, 3, Ein);
for (int itin = 0; itin < 3; itin++) {
Serial.print(encoderOutput[i]);
Serial.print(",");
}
Serial.println("");
Serial1.println("");
}
readEncoder function (intParse works well)
void readEncoder(char *buf) {
digitalWrite(encoderPin, HIGH); //send signal to the encoder board
delay(1);
digitalWrite(encoderPin, LOW);
delay(1);
int bytesRecieved = 0;
int count = 1;
char startMarker = '<';
char endMarker = '>';
bool quit = false;
bool readInProgress = false;
char x;
while (quit == false) {
if (Serial3.available()) {
// the order of these IF clauses is significant
x = Serial3.read();
if (x == endMarker) {
readInProgress = false;
quit = true;
}
if (readInProgress) {
buf[bytesRecieved] = x;
bytesRecieved ++;
}
if (x == startMarker) {
bytesRecieved = 0;
readInProgress = true;
}
}
}
}
Slave code, readEncoder returns long int and has no issues
// These hold the current encoder count.
signed long encoder1count = 0;
signed long encoder2count = 0;
signed long encoder3count = 0;
volatile bool sendstate = false;
void setup() {
Serial.begin(115200); // Serial com for data output to USB
Serial1.begin(115200); // Serial com for data output to control box
pinMode(comPin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(comPin), dataSend, RISING);
initEncoders();
clearEncoderCount();
}
void loop() {
// Retrieve current encoder counters
encoder1count = readEncoder(1);
encoder2count = readEncoder(2);
encoder3count = readEncoder(3);
if (sendstate == true) {
Serial.print('<');
Serial.print(encoder1count); Serial.print(',');
Serial.print(encoder2count); Serial.print(',');
Serial.print(encoder3count); Serial.print(',');
Serial.print(">");
Serial1.print('<');
Serial1.print(encoder1count); Serial1.print(',');
Serial1.print(encoder2count); Serial1.print(',');
Serial1.print(encoder3count); Serial1.print(',');
Serial1.print(">");
sendstate = false;
}
}
void dataSend() {
sendstate = true;
}