1

I'm coding traffic lights on Raspberry Pi in C++ and ran into a dead-end.

I'm using a functions that's called from the main code block, problem is - it dumps all intended outputs at the end of the loop. Both into the console and to physical outputs.

I've searched for a solution, but came up short.

Does anyone know of a way to force the outputs to happen sequentially or a place to read up on it?

void idle()
{
    int allY[]={9,22,13,26};  //output pin numbers
    for(int i=0;i<=10;i++)   // blink X times
    {
        for(int j=0;j<=4;j++)  //all outputs to HIGH
            {
                digitalWrite (allY[j], HIGH);
                cout<<"+";   // output to console for testing
            }

        usleep(900000); //wait

        for (int k=0;k<=4;k++)  // all outputs to LOW
            {
                digitalWrite (allY[k], LOW);
                cout<<"-";
            }
    }
}

FOR FUTURE READERS - SOLUTION

void idle()
{
    int allY[]={9,22,13,26};//output pin numbers
    for(int i=0;i<=10;i++) // blink X times
    {
        for(int j=0;j<=4;j++)//all outputs to HIGH
            {
                digitalWrite (allY[j], HIGH);
                cout<<flush; //flush
            }

        usleep(900000); //wait

        for (int k=0;k<=4;k++)// all outputs to LOW
            {
                digitalWrite (allY[k], LOW);
                cout<<flush;
            }

        usleep(900000); //wait
    }
}
Noctua
  • 11
  • 3

0 Answers0