Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: Help with reading serial with timers

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Posts
    19

    Help with reading serial with timers

    Hey guys. Please reference https://github.com/AirmanEpic/teensy_dshot_simple which I'm using to code.

    the problem is that reading from serial will stop the "Packet sending" which breaks the system I'm devising. So instead, it was decided to use timer interrupts to handle the packet sending while the serial handling was done in the main loop.

    See line 49 through 52 in the code. This is where the serial commands are read.

    I also added interrupt handling where the code received enters the flow, line 109 and 111.

    However, when I run this, it will accept a single command before all subsequent commands are denied. I suspect this is because of the "Send single command" function on line 58. However, I've added a serial print in that code block and it will always print so it's not simply hanging there.

    Anyone have any idea? Feel free to make PRs to the github too.

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,871
    For testing at least change this line 8:: myTimer.begin(timerCommand, 20);

    To something over well 20us.

    There is a lot of code and delay in the timer _isr()

  3. #3
    Junior Member
    Join Date
    Nov 2020
    Posts
    19
    Sure, but something this speed or higher is required for the project.

    ok yes I see it's responding now at 100us. Hmmm.

  4. #4
    Junior Member
    Join Date
    Nov 2020
    Posts
    19
    I take that back, at 50us it is working exactly ideally and sending the correct packets. This is solved. Thank you for the help!

  5. #5
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,429
    Glad you have it working...

    Note: With your code:
    Code:
      while (Serial.available()){
        String a = Serial.readString();
        Serial.println("Received Signal: "+a);
        handleCommand(a);
      }
    }
    Personally I don't use this method nor most others like it...
    As if you look at how it is defined:
    Click image for larger version. 

Name:	screenshot.jpg 
Views:	6 
Size:	27.1 KB 
ID:	25810
    You will see this call will wait until it does not receive any more Serial data until a timeout happens.
    And by default the timeout is 1 second. (1000ms)

    Wonder how code might work if you added a call like: Serial.setTimeout(1);
    for 1ms timeout?

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,871
    Quote Originally Posted by StefanB View Post
    I take that back, at 50us it is working exactly ideally and sending the correct packets. This is solved. Thank you for the help!
    Great news!

    Quote Originally Posted by StefanB View Post
    Sure, but something this speed or higher is required for the project.

    ok yes I see it's responding now at 100us. Hmmm.
    As noted - that was a 'test' since in scanning the code 20us seemed low to allow completion before the next timer would want to execute!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •