Shop OBEX P1 Docs P2 Docs Learn Events
Cog blinking an LED — Parallax Forums

Cog blinking an LED

PliersPliers Posts: 280
edited 2021-06-13 16:00 in Propeller 1

I'm trying to get a cog to run a blinking LED (pin 18) with values coming from the main function.
The LED on pin 18 is always on. At first I thought it was the wrong use of a pointer (and it may still be wrong). So I tried using set variables, but still no blinking action.

Sorry, I could not get the code formatter to work for me

#include "simpletools.h"         // Include simple tools
void Modulation_Out(void);       // Forward declaration
int Mod=18;                                 // Pin 18
int   Mod_high=500; 
int   Mod_low=500;

int main()                           
{   
  cog_run(Modulation_Out, 128);  
  while(1){ }   
} 

void Modulation_Out()
 {     
   low(18);        //  low(Mod);            
   pause(1000);    //  pause(*Mod_low);      
   high(18);       //  high(Mod);                       
   pause(1000);    //  pause(*Mod_high);                          
 }

Comments

  • Code above updated to include code tags. @Pliers you could click the cog icon, and edit your post, to see how the code tags work.

  • Thanks.

  • JonnyMacJonnyMac Posts: 8,926
    edited 2021-06-13 17:16

    The LED stays on because the last command to it was high(). Your cog code is not a loop. Try this:

    void modulation_out() 
    {
      for(;;) {    
        low(18); 
        pause(1000); 
        high(18); 
        pause(1000);    
      }
    }
    

    Unlike the Arduino, there are no hidden loops in the Propeller.

    Still, this code uses fixed timing values. You'll need to pass a pointer to your timing which is in the hub, then have your cog code read the new timing values from that pointer.

  • Duh!
    Thanks a lot.
    I feel so stupid.

    JohnyMac to the rescue again.

Sign In or Register to comment.