Shop OBEX P1 Docs P2 Docs Learn Events
Blink an led 10 times — Parallax Forums

Blink an led 10 times

Hello, I am trying to make p26 blink 10 times when wL = 0 and p27 blink 10 times when wR = 0. Here is the code I have to modify to make that happen but I am confused on where to start. A hint that is on the parallax website is to nest a for loop inside an if statement. Does anyone have any suggestion about how to go about this?



#include "simpletools.h" // Include simpletools header

int main() // main function
{
freqout(4, 10, 3000); // Speaker tone: P4, 2 s, 3 kHz
while(1) // Endless loop
{
int wL = input(7); // Left whisker -> wL variable
int wR = input(8); // Right whisker -> wR variable
if(wL == 0) // Light for left whisker
{
high(26);
pause(50);
low(26);
pause(50);
}
if(wR == 0) // Light for right whisker
{
high(27);
pause(50);
low(27);
pause(50);
}
print("%c", HOME); // Terminal cursor home (top-left)
print("wL = %d wR = %d", wL, wR); // Display whisker variables
pause(50); // Pause 50 ms before repeat
}
}

Comments

  • Between the "{" and "}" for the if statement:
    if(wL == 0) // Light for left whisker
    
    put your existing code into a for loop for the number of times you want the led 26 to blink.

    Tom
  • I am still a little confused about how the for statement has the p26 blink 10 times. The only type of for statement I have done is where it would count to a certain number and stop like this.
    for(int wL = 0; wL <= 10; wL++);

    Should I be including P26 in the for statement or not?

    Thanks
  • You will just put the code you wrote to turn the LED on and off one time into a for loop that cycles 10 times. Use a new variable, for example "int i" as the counting index.
    
    if(wL == 0) // Light for left whisker
    {
      for (int i = 0; i < 10; i++)
      {
        high(26);
        pause(50);
        low(26);
        pause(50);
       }
    }
    
    

    I hope this helps.
    Tom
  • Tom,

    This helps a lot.

    Thanks for your time
Sign In or Register to comment.