Go Down

Topic: (solved)Missing interupt with Mega 2560 and IDE V1.0.3 (Read 439 times) previous topic - next topic

dougiel

Feb 14, 2013, 01:33 pm Last Edit: Mar 06, 2013, 04:49 pm by dougiel Reason: 1
Hello,
  ive just updated one of my sketches on a Mega 2560 from 0023 to 1.0.3 and all works as it should apart from the interupt. I tried a second new Mega but it also did not work. I checked the wiring and sensor and these were working as they should be. Moving the sketch back to 0023 and it works OK again.
Interupt is attached as below:

Code: [Select]

 Serial.begin(9600);
 Serial1.begin(9600);
 Serial3.begin(9600);
 //set interupt for token
 attachInterrupt(0, TokenChange, FALLING);
 pinMode (nosig, INPUT_PULLUP);


I obviuosly change the line: pinmode (nosig, INPUT_PULLUP); back to the older version for 0023.
This calls a simple serial write to two Polulu Maestro servo controllers. The code is as below.

Code: [Select]

void TokenChange() {
 if (token == 1) {
   token = 0;
   //reset tunnel signal
   Serial1.write(170);
   Serial1.write(12);
   Serial1.write(4);
   Serial1.write(5);
   Serial1.write(112);
   Serial1.write(45);
   Serial1.flush();
   Serial1.write(170);
   Serial1.write(12);
   Serial1.write(4);
   Serial1.write(9);
   Serial1.write(64);
   Serial1.write(62);
   //tell controller
   delay(50);
   Serial.write(111);
 }
}


Im pressuming that its the V1.0.3 that is at fault but if anyone can see an obvious mistake I will be very happy as I need some of the newer code from V1.0.3.
MTIA
Dougie L

SurferTim

As I recall, the change in the newer versions is the serial buffers are interrupt driven. You will have problems calling an interrupt driven function in an interrupt. Set a flag in the interrupt, then monitor and clear the flag in the loop() function.

dougiel

SurferTim,
   gotcha !. I was wondering after I looked at my question if doing a serial write in the interupt handler was "bad". I rewrote the sketch with a flag to be set in the handler and moved the serial write to the main sketch. The interupt then worked.
   Unfortunately it broke some of the other parts of the sketch so I may have to stick with 0023. Its part of a token signal system for a garden railway with a single track with the token handed back by the interupt. While the token is issued the Mega can be used for other tasks in the terminus but restricting access to that single line. Ill have to try and see whats broke what and see if I can fix it while using 0023 for now.
Many thanks for your help.
Dougie L

dougiel

Found my problem. The serial on the Mega asks a Pololu Maestro if the script it is running is still running. This returns 0 if it is and 1 if it has completed. The initial problem was seen as reading the last input into the buffer which was usually a 1. It would seem this initial problem was down to bad timming of the read and hence my need to flush the serial buffer to get the right reply.
In 1.0.3 I had tried to replace the Serial1.flush() with this line;
while(Serial1.available() >0) Serial.read();
This still gave problems as though the sketch was reading the previous input. Not sure why. I then removed the flush altogether and increased the delay between the request for input and reading the input. This means I always get the last input in the buffer as the Pololu has enough time to send its reply and the Mega to store in the buffer. Made the program a bit slower but it now works 100%.
This project sure is keeping my 66 year old mind active !!!.
Dougie L
:D

Go Up