Shop OBEX P1 Docs P2 Docs Learn Events
sirc and 2nd cog — Parallax Forums

sirc and 2nd cog

TalheaernTalheaern Posts: 7
edited 2014-06-08 19:35 in Learn with BlocklyProp
I am using sirc.h and a sony remote to control the the propeller. But sirc has some while loops that can suck up some processing time so I thought I would move it to a 2nd cog.

In the 2nd cog it didnt seem to work right, so I wrote the test code below, and it still does not work.

#include "simpletools.h"
#include "sirc.h"

void counts();

int *cog;
volatile int dt; // Declare var for both cogs
volatile int button;

int main() // Main function
{
dt = 0; // Set value of dt to 0

cog = cog_run(&counts, 100); // Run count in other cog
//sirc_setTimeout(1000); // -1 if no remote code after 1 second

while (1){
//button = sirc_button(10); // read IR receiver on pin 10
putChar(HOME);
print("dt = %d %c\n", dt, CLREOL);
print("button = %d %c\n", button, CLREOL);
}
}

void counts() // Function for other cog
{
sirc_setTimeout(100); // -1 if no remote code after 1 second
while(1) // Endless loop
{
dt=dt+1;
button = sirc_button(10); //read IR receiver on pin 10
}
}


If I use the lines rem out in main and rem out the "sirc_setTimeout(100);" and "button = sirc_button(10);" in counts, every this works fine. I get a response from pressing a button on the remote right a way.

If I use the code as shown above, it can take upto 10 seconds for a button push to show, and when I release the button it takes 10 seconds for the release to show. I know the
2nd cog is working because dt still counts up.

Any idea why this would run slow on the 2nd cog but fine on on the main cog?

Comments

  • edited 2014-06-07 18:01
    It looks like the code might be streaming data to SimpleIDE Terminal faster than it prints. When you press a different button, it is sent to SimpleIDE Terminal immediately, where it gets buffered and displayed eventually.

    As a quick check, try adding pause(200) into the main while loop, and see if it starts responding more like you'd expect. If that's the problem, and if you don't want to use pause, something I usually do is add a buttonOld variable. The code inside the main function's while(1) loop:

    if(button != buttonOld)
    {
    buttonOld = button
    // add print statements here
    }
  • TalheaernTalheaern Posts: 7
    edited 2014-06-08 19:35
    Thank you! The pause fixed the problem!

    I was not thinking about the delay I had removed in main, just the slow response in counts.
Sign In or Register to comment.