Newbie help. RGB Led on a PIC10F202


Closed Thread
Results 1 to 40 of 42

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    969


    Did you find this post helpful? Yes | No

    Default Re: Newbie help. RGB Led on a PIC10F202

    Hi Brian

    Take a look at this code. ButtonCount starts with 0 and then loops between 1 and 3 depending on your button presses. You may want it to roll over to 0 and have an all off program too. EDIT : My post assumes you are using a high input as indication of switch being pressed.

    Code:
    Red var GPIO.0 
    Green var GPIO.1 
    Blue var GPIO.2 
    SwitchPin var GPIO.3  
    ButtonCount VAR BYTE 
    
        ButtonCount = 0  ;Make sure LEDs are off 
    
        HIGH Red  
        HIGH Green 
        HIGH Blue  
    
    MAIN: 
    SWITCHLOOP:
        if SwitchPin = 1 then
          ButtonCount = ButtonCount+1
          if ButtonCount > 3 then ButtonCount = 1
          while SwitchPin = 1: wend     ' wait here while the switch remains pressed
        endif
    
        If ButtonCount = 1 THEN GOSUB RedProgram 
        If ButtonCount = 2 THEN GOSUB GreenProgram 
        If ButtonCount = 3 THEN GOSUB BlueProgram 
    GOTO SWITCHLOOP 
    
    END  
    
    RedProgram: 
        PWM Red,127,100 
    RETURN  
    
    GreenProgram: 
        PWM Green,127,100 
    RETURN  
    
    BlueProgram: 
        PWM Blue,127,100 
    RETURN
    
    END

  2. #2
    Join Date
    Aug 2011
    Posts
    16


    Did you find this post helpful? Yes | No

    Default Re: Newbie help. RGB Led on a PIC10F202

    I wish i could reply back and say that works.. But it doesnt. Its randomly cycling through each color in order (i.e.; Red, Green, Blue, etc.) without me pressing anything. Also, Im thinking i screwed up in the code i provided where the color should always stay lit UNTIL the switch is pressed, so the program always needs to be waiting for a switch press, then it changes color.

  3. #3
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    969


    Did you find this post helpful? Yes | No

    Default Re: Newbie help. RGB Led on a PIC10F202

    I suspect the switch is connected between the pin and ground effectively giving you a logic low for a switch press. That is why the colors are cycling.
    If this is truly the case, then you can do well to change these lines in switchloop
    Code:
        if SwitchPin = 0 then       ButtonCount = ButtonCount+1       if ButtonCount > 3 then ButtonCount = 1       while SwitchPin = 0: wend     ' wait here while the switch remains pressed     endif

  4. #4
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Newbie help. RGB Led on a PIC10F202

    Good morning Brian, well morning for me anyway. We need to know a bit about your circuit. How is the switch actually connect to the pic? Do you have a pull up/down resistor?
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: Newbie help. RGB Led on a PIC10F202

    Yup, or there's no pull-up/down resistor. whatever
    Code:
            '-------------------------------------------------------------------------------------------
            '
            '       The following have been tested, no problem at all
            '       
            '       Hardware description
            '           1) LEDs connected betwee PIC I/O and GND via resistor
            '           2) Push button between PIC and GND... nothing else.
            ' 
            '-------------------------------------------------------------------------------------------  
             
    @       __config _WDT_OFF & _MCLRE_OFF & _CP_OFF
                    ' Disable watch dog timer
                    ' Disable MLCR, GP3 as I/O
                    ' Disable Code Protection 
                                  
            Red         var GPIO.0
            Green       var GPIO.1
            Blue        var GPIO.2
            SwitchPin   var GPIO.3  ' Active low
           
            OPTION_REG = %00000000
                       ' -0-------- _GPWU -- Disable Wake-Up on pin change
                       ' --0------- _GFPPU - Enable Weak Pull-Ups 
                       ' ---0------ T0CS --- Timer0 Clock Source Internal (O T0CKI pin, 0 Internal)
                       ' ----xxxxx- Timer0 related stuff... don't care
            
            TRISIO = 0          ' Clear all I/Os
            GPIO = %00001000    ' GP3: Input, other pin: Output 
                   
            ButtonCount VAR BYTE
    
    MAIN:
            IF !SwitchPin THEN                              ' Switch down?
                                                            ' - YES
                    If ButtonCount = 3 THEN ButtonCount = 0 ' - Currently on GP3 (MCLR)?  If so, pass go, claim 200$ 
                    pwm ButtonCount,127,100                 ' - Light Show
                    WHILE !SwitchPin : wend                 ' - Spin 'till button is released
                    pAUSE 50                                ' - arbitrary debouince delay
                    ButtonCount = ButtonCount + 1           ' - Point to next LED
                    endif                                   ' 
            GOTO MAIN
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Join Date
    Aug 2011
    Posts
    16


    Did you find this post helpful? Yes | No

    Default Re: Newbie help. RGB Led on a PIC10F202

    Simple circuit. VDD hooked up to 3.7v, GPIO.0 to Red Lead, GPIO.1 to Green Lead, GPIO.2 to Blue Lead, GPIO.3 to Switch and ground when pressed, Vss hooked to Ground. And of course resistors coming from each of the I/O pins to the LED cathodes while the anode goes to 3.7v.

    Steve, When i run your code i get all three leds on, making white, press button i get Bright Blue, press button again i get more of a regular blue, again i get a dim green with both green and red leds lit.

    But at least the switch is working.
    Last edited by brianmorris; - 14th August 2011 at 18:41.

  7. #7
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Newbie help. RGB Led on a PIC10F202

    So If the switch is not pressed, the input pin is floating, unless there are weak pull-ups in play here. If not, try adding a pull-up to the input. This way the input is either high or low, but never allowed to float.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  8. #8
    Join Date
    Aug 2011
    Posts
    16


    Did you find this post helpful? Yes | No

    Default Re: Newbie help. RGB Led on a PIC10F202

    I connected a 10k resistor from VCC to GPIO.3 and then the button is still connected to GPIO.3 and GND. Push the button, the color changes, but still the same as above.

  9. #9
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: Newbie help. RGB Led on a PIC10F202

    Quote Originally Posted by brianmorris View Post
    Steve, When i run your code i get all three leds on, making white, press button i get Bright Blue, press button again i get more of a regular blue, again i get a dim green with both green and red leds lit.

    But at least the switch is working.
    I guess i've been lucky then, modifs in RED

    Code:
            TRISIO = 0          ' Clear all I/Os
            GPIO = %00001000    ' GP3: Input, other pin: Output 
                   
            ButtonCount VAR BYTE
            CLEAR
    MAIN:
            IF !SwitchPin THEN                              ' Switch down?
                                                            ' - YES
                    If ButtonCount = 3 THEN ButtonCount = 0 ' - Currently on GP3 (MCLR)?  If so, pass go, claim 200$ 
                    PWM ButtonCount,127,100                 ' - Light Show
                    LOW ButtonCount
                    WHILE !SwitchPin : WEND                 ' - Spin 'till button is released
                    PAUSE 50                                ' - arbitrary debouince delay
                    ButtonCount = ButtonCount + 1           ' - Point to next LED
                    endif                                   ' 
            GOTO MAIN
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  10. #10
    Join Date
    Aug 2011
    Posts
    16


    Did you find this post helpful? Yes | No

    Default Re: Newbie help. RGB Led on a PIC10F202

    Bruce, your code is working perfectly. I can cycle through every color, the only thing I am having trouble with is maybe the switch is a tad too sensitive. I am thinking maybe using TMR0 where if i hold down the switch for 1-2 seconds, it will then change colors.

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts