Shop OBEX P1 Docs P2 Docs Learn Events
SimpleIDE PWM — Parallax Forums

SimpleIDE PWM

I´m looking for a simple example to change the intensity of a led with PWM, I can´t found any.
I do this with SPIN and I want to know how use PWM with SimpleIDE.
I think is something with
pulse_out (int pin, int time)
Thanks

Comments

  • Check out the pwm_set, pwm_start and pwm_stop functions.

    I think there's a way to search SimpleIDE for these functions too, but I just searched "pwm" at the top-right of http://david.zemon.name/PropWare and found this
    http://david.zemon.name/PropWare/simpletools_8h.xhtml#a57f683188c3dd35ee0574ff349835682
  • ASAS Posts: 149
    Thanks you!

    I will try do something simple. I ask it here without try nothing, because I thought that should be a example in the (http://learn.parallax.com).
  • ASAS Posts: 149
    edited 2016-02-04 19:23
    DavidZemon,

    This code is working ok:
    #include "simpletools.h"           // Include simpletools                    
    
    int main()                                  // main function
    {
    
      int Led_Intensity=0;
      pwm_start (1000);
      
      while(1)                                    // Endless loop
      {
        
        while (Led_Intensity<1000)
        {
          pwm_set (0,1,Led_Intensity);
          Led_Intensity ++;
          pause (1);
        }  
        
              
        while (Led_Intensity>0)
        {
          pwm_set (0,1, Led_Intensity);
          Led_Intensity --;
          pause (1);
        }      
    
      }
    }
    

    I have two simple questions for now:
    1 - The channel of pwm_set can be "0" or "1" it is the Counter Modules for each COG "ctra" and "ctrb". Right?
    2 - Now I´m trying to control the Led with the LabView, at this moment I have only one question.
    How I should to do to convert a string from the array to a integer? In the Arduino I do something like:
    number = String(c[index]).toInt();
    
    Where can I see information to convert strings and numbers?

    Thank you
  • Lots of different options for that, depending on how flexible you want it to be. In this case, you could choose the highly inflexible, but very compact solution:
    int number = c[index] - '0';
    

    Or that's the equivalent of
    int number = c[index] - 0x30;
    

    This is because the ASCII value for a zero is 0x30. And the ASCII value for a one is 0x31. See http://www.asciitable.com/.
    Of course, you might notice this only works for single digits :)

    If you need a solution that works for many digits, but always whole numbers and only numbers, then you can use atoi (array to integer) or atol (array to long).
    int number = atoi(c);
    

    Notice that this time I did not use c[index], but c instead. This is because atoi and atol require "C-string"s. That is another way to say "null-terminated character array". Or, more importantly, more than a single character. Most of time, this is how people deal with input from the terminal: they read in whole strings at a time, rather than individual bytes. So, this method is easier than the first that I mention, but it requires modifying the rest of your code to read strings instead of bytes from LabView.

    Finally, there's sscanf. This is like the reverse of printf. You give it a formatted string and it breaks up the words into the values you want. I wouldn't recommend it for this specific application, but the documentation is worth a glance so that you know what tools are in your toolbox :)
  • AS wrote: »
    1 - The channel of pwm_set can be "0" or "1" it is the Counter Modules for each COG "ctra" and "ctrb". Right?]

    I don't have the code in front of me, so I can't confirm it... but your guess sounds like a very good one to me.
  • ASAS Posts: 149
    edited 2016-02-04 23:30
    I already try 1001 different situations and I can´t do this :(

    I need your help!

    The LabView is sending something like:
    "ok0," or "ok78," or "ok675," or "ok1000,"
    I can change it, if you think usefull.

    I go post the code just for you laugh a little :D, the real reason is to let you understand how I´m thinking to do this :), of course when this run it is possible to simplify.
    #include "simpletools.h"                      // Include simpletools                    
    
    
    int main()                                    // main function
    {
      int index;
      int Led_Intensity_I[4];
      int Led_Intensity;
      char c[10];
      pwm_start (1000);
      
      
      while(1)                                    // Endless loop
      {
    
        index = 0;
        Led_Intensity_I[0] = 0;
        Led_Intensity_I[1] = 0;    
        Led_Intensity_I[2] = 0;     
        Led_Intensity = 0;    
        
        
        
        while (index<7)            // "ok????,"  Get string from LabView
        {                          //  0123456
          c[index] = getChar();
          index ++;
        }  
        
        
    
        //////////////////////// Try to make the number :P ///////////////////////////////////////
        if (c[3]==',')   //////////// if the number is between 0 - 9 /////////////
        {
          int (Led_Intensity = c[2] - 0x30);
        }
        
        if (c[4]==',')    //////////// if the number is between 10 - 99 /////////////
        {
          int (Led_Intensity_I[0] = c[2] - 0x30);
          int (Led_Intensity_I[1] = c[3] - 0x30);
          Led_Intensity = Led_Intensity_I[0]*10 + Led_Intensity_I[1];
        }
        
        if (c[5]==',')    //////////// if the number is between 100 - 999 /////////////
        {
          int (Led_Intensity_I[0] = c[2] - 0x30);
          int (Led_Intensity_I[1] = c[3] - 0x30);
          int (Led_Intensity_I[2] = c[4] - 0x30);
          Led_Intensity = Led_Intensity_I[0]*100 + Led_Intensity_I[1]*10 + Led_Intensity_I[2];
        }  
        
        if (c[6]==',')   //////////// if the number is 1000 /////////////
        {
          Led_Intensity = 1000;
        }            
        
        //////////////////////////////////////////////////////////////////////////////////////////
        
        index = 0;
        while (index<7)         // send back the received string
        {
          printi("%c", c[index]);
          index ++;
        }   
                 
        pwm_set (0,1,Led_Intensity); // send to the Led the PWM
        
        pause (20);
        
     }   
    }
    

    I think the best solution for me is to do something like:
    int number = c[index] - 0x30;
    
    Because I want send numbers and letters each cycle!? (from the LabView)

    I think I don´t understand when you said: "highly inflexible" :P, I already done similar tasks with SPIN and C (to Arduino) and I think it was much easier, maybe because are more information and examples. I hope you can help me!
    Thanks
  • ASAS Posts: 149
    edited 2016-02-05 00:15
    The code like this don´t give any warnings or errors but don´t work :P:
    #include "simpletools.h"                      // Include simpletools                    
    
    
    int main()                                    // main function
    {
      int index;
      int Led_Intensity=0;
      int Led_Intensity_aux1=0;
      int Led_Intensity_aux2=0;
      int Led_Intensity_aux3=0;  
      char c[10];
      pwm_start (1000);
      
      
      while(1)                                    // Endless loop
      {
    
        index = 0;
        
        while (index<7)            // "ok????,"  Get string from LabView
        {                          //  0123456
          c[index] = getChar();
          index ++;
        }  
        
        if (c[3]==',')
        {
          Led_Intensity = c[2] - '0';
        }
        
        if (c[4]==',')
        {
          Led_Intensity_aux1 = c[2] - '0';
          Led_Intensity_aux2 = c[3] - '0';
          Led_Intensity = Led_Intensity_aux1*10 + Led_Intensity_aux2;
        }
        
        if (c[5]==',')
        {
          Led_Intensity_aux1 = c[2] - '0';
          Led_Intensity_aux2 = c[3] - '0';      
          Led_Intensity_aux3 = c[4] - '0';       
          Led_Intensity = Led_Intensity_aux1*100 + Led_Intensity_aux2*10 + Led_Intensity_aux3;
        }  
        
        if (c[6]==',')
        {
          Led_Intensity = 1000;
        }   
        
        index = 0;
        while (index<7)         // send back the received string
        {
          printi("%c", c[index]);
          index ++;
        }          
              
        pwm_set (0,1,Led_Intensity);
        
        pause(20);
        
     }   
    }
    

    Any sugestion? Please!
  • AribaAriba Posts: 2,690
    Your code looks quite complicated. I would parse the characters while they arrive, without storing them in a string first.
    The following code shows what I mean, but be aware that it's untested:
    #include "simpletools.h"                      // Include simpletools                    
    
    
    int main()                                    // main function
    {
      int Led_Intensity;
      char c;
    
      pwm_start (1000);
      
      while(1)                                    // Endless loop
      {
    
        Led_Intensity = 0;
        c = 0;
    
        while(c != 'o')  getChar();		     // wait for 'o'
        if(getchar() != 'k') continue;           // restart loop if not 'k'
        while(c != ',')			     // get dec-value from LabView
        {
          c = getChar();
          if(c>='0' && c<='9')  Led_Intensity = Led_Intensity*10 + (c-'0');
        }  
    
        pwm_set (0,1,Led_Intensity);    
      }   
    }
    

    Andy
  • ASAS Posts: 149
    Aribe,

    At this moment your code is not working right, I will see it better and I will have some question about it for sure :P
    I´m getting some issues with the "getChar()" I think I will need use "fdserial".

    Thanks a lot for the example you sent.

  • DavidZemonDavidZemon Posts: 2,973
    edited 2016-02-05 02:51
    AS wrote: »
    I´m getting some issues with the "getChar()" I think I will need use "fdserial".

    Thanks a lot for the example you sent.

    At brief glance, ariba's code looks good (as usual :) ). The only issue I see right away is that he mixed cases on the getChar. Perhaps that is the issue you're running into?
  • ASAS Posts: 149
    edited 2016-02-05 15:53
    Ariba,
    Thanks a lot (again) your code is Fantastic!
    I learn with your code. But I don´t understand why "gerchar()" is not working here :(
    The code that is running here is with "fdserial.h":
    #include "simpletools.h"                      // Include simpletools       
    #include "fdserial.h" 
    
    fdserial *LabView;             
    
    
    int main()                                    // main function
    {
      LabView = fdserial_open(31, 30, 0, 115200);  
      pause(500);
      int Led_Intensity;
      char c;
    
      pwm_start (1000);
      
      while(1)                                    // Endless loop
      {
    
        Led_Intensity = 0;
        c = 0;
        fdserial_rxFlush(LabView);
    
        while(c != 'o')      	                 // wait for 'o'
        {
          c = fdserial_rxChar(LabView);
        }      
        
        if(fdserial_rxChar(LabView) != 'k') continue;           // restart loop if not 'k'
        
        while(c != ',')			     // get dec-value from LabView
        {
          c = fdserial_rxChar(LabView);
          if(c>='0' && c<='9')  Led_Intensity = Led_Intensity*10 + (c-'0');
        }  
    
        pwm_set (0,1,Led_Intensity);    
        pause(20);
      }   
    }
    

    Ariba and DavidZemon, I hope you can help me understand what is the issue with "gertchar()"

    DavidZemon I want to see PropWare running this, I want try it, but at this moment I need to understand the other issue (http://forums.parallax.com/discussion/163457/simpleide-fdserial-h#latest).

    Your help has been fantastic!
  • What was the error message with "getchar()"? Did you just not define it, or was it something else?
  • ASAS Posts: 149
    Hi Eletrodude! No error message with "getchar()". I just can´t control the Led_Intensity, it´s off all the time.
    The issue here is very similar with the other one (http://forums.parallax.com/discussion/163457/simpleide-fdserial-h#latest). With "fdserial.h" is everything ok but with "getchar()" I just can´t understand why it is not working.
Sign In or Register to comment.