Arduino Uno RX pin dont works with timer2 ?

Hello folks
I am doing a project where every time I receive a byte I need to check if the byte is for example ‘g’ to increment a value that is shown on a 7 Segment Display.
I am using two timers (Timer libraries):
Timer 1 (library -> TIMERONE)
Timer 2 (library -> MSTIMER2)
My timer 1 Is used to control the 7 segment Display. That part works fine.
Now I use my timer 2 to every time I receive a byte (‘g’) he should increment the value of the 7seg display. But the problem is since I wrote this code part my Arduino RX pin does not receive anything at all….
Here is the code of my timer 2:

setup:

Serial.begin(9600);

//timer 2 Init (via lib)
  MsTimer2::set(1000, todo);         // 1s period
  MsTimer2::start();

main part:

void todo() {
  if (Serial.available()) {
    incomingByte = Serial.read();
    Serial.write(incomingByte);       //Serial monitor test
    Serial.print(PLAYERONE_1);        //Serial monitor test
    Serial.print(PLAYERONE_10);       //Serial monitor test
    if (incomingByte == 'g' ) {
      PLAYERONE_1 = ++PLAYERONE_1;    //increment PLAYERONE
    }
  }
}

best regards.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your complete code.

It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

Why do you need to use a timer to check whether you have received 'g' ?
Have you got blocking code in your program that prevents loop() running freely ?

      PLAYERONE_1 = ++PLAYERONE_1;    //increment PLAYERONE

Are you sure that this line of code does what the comment says ? There has been discussion in other threads here and my understanding is that the result of this line of code is undefined.

      ++PLAYERONE_1;    //increment PLAYERONE

is the correct way to do this.

As a matter of interest, do you understand the difference between

++PLAYERONE_1;

and

PLAYERONE_1++;

As to your problem with the timer, please post your complete program of a smaller but complete program that illustrates the problem.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

I can't think of any need to use a timer in the process of receiving data.

...R

I will try to explain you why I wanted to use an Timer to receive a Byte:
Basically, it’s a double project where a friend is programming PONG (the old school ping pong game) and whenever someone scores a goal he sends me a byte (for example ‘g’) and then I need to react to his byte to increment the score.
So, I wanted to use this timer an “interrupt” so whenever a byte arrives the program would jump to an ISR where he then increments the score.
But using the timer did not worked the way I thought it would, now I m struggling to find an alternative …
(Sorry for replying so late I had some other work to do :frowning: )

But why can't you check for that 'g' in the loop()???

Rayden:
whenever someone scores a goal he sends me a byte (for example ‘g’) and then I need to react to his byte to increment the score.
So, I wanted to use this timer an “interrupt” so whenever a byte arrives

A timer would not be suitable for that because the timer has no means to know when a byte arrives.

And, actually, when a byte arrives an interrupt is triggered and that causes the byte to be added to the Serial Input Buffer. Your code can check for a byte in the buffer using Serial.available(). That will be plenty fast enough to detect data that arrives every second, or even if 1 byte arrives every millisecond.

...R

Rayden:
So, I wanted to use this timer an “interrupt” so whenever a byte arrives the program would jump to an ISR where he then increments the score.

Anything sent on the serial line will be received and stored in a UART until you read it.

Serial.available()

Tells you when there is something in the UART.

Why is it time critical that the instant a "g" is sent it has to be updated, the code executes and loops so fast you won't notice the delay between the "g" arriving and the code reading and displaying an updated value using the "available" statement.

How are you outputting to your 7 segment display?

Please read post #1

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What model Arduino are you using?

Thanks.. Tom... :slight_smile: