Could someone please explain why would Timer1 affect Serial communication? (using Arduino Mega 2560)
There are many lines in this project so I'll give only relevant codes.
void setup() {
...
Timer1.initialize(100);
Timer1.attachInterrupt(update,100);
...
}
void loop() {
checkTouch();
...
}
Update function calls functions to read current state of 2 quadrature encoders. It usually takes 30 microseconds to run. So an interval of 100 microseconds usually do not lock the processor.
checkTouch calls following functions. Puts a circle on the touchscreen if there is a touch. Resets the screen if the home icon of SMARTGPU is pressed.
uint8_t SMARTGPU::touchScreen(int buffer[]){ //Ask for a touch on the screen, if return=1, touch coordinates are stored on the buffer[]
Serial2.write('G');
while(Serial2.available() < 5);
buffer[0]=Serial2.read();
buffer[0]=buffer[0]<<8;
buffer[0]|=Serial2.read();
buffer[1]=Serial2.read();
buffer[1]=buffer[1]<<8;
buffer[1]|=Serial2.read();
Serial2.read();
if(buffer[0]<0x0200){
return 1;
}else{
return 0;
}
}
uint8_t SMARTGPU::touchIcon(char buffer[]){ //Ask for a touch on the icons of the screen, if return=1, icon name is stored on the buffer[]
Serial2.write('G');
while(Serial2.available() < 5);
buffer[0]=Serial2.read();
buffer[1]=Serial2.read();
buffer[2]=Serial2.read();
buffer[3]=Serial2.read();
Serial2.read();
if(!(buffer[0]<0x02) & (buffer[0]!=0x4E)){
return 1;
}else{
return 0;
}
}
This works fine if I comment out the Timer1 lines in setup().
Thanks in advance.