controlling leds with the switches


Closed Thread
Results 1 to 3 of 3
  1. #1
    ilteris's Avatar
    ilteris Guest

    Default controlling leds with the switches

    Hello guys, this is my first post

    I am trying to address and control my 9 LEDs which are on RC1, RC2, RC3, RC4, RC5, RD0, RD1, RD2 and RD3 with my switches that are plugged in on my RB0 to RB7 and RD7 on my 18f452 chip.

    It is going to be 32 LEDs if I can figure out the code for this proto 9 LEDs. I wrote some kind of pseudo code to work out my algorithm as well. I am sharing it below, you are going to have an idea afterwards presumably.

    on press switch
    if it is the first one that is pressed
    if the led that is addressing this switch is on
    take the led's address and put it in a variable
    check if two rows up or down or left or right of any LEDs is on or off
    if on
    take this led's address and put it in another variable
    check if one row up or down or left or right of any LED to this is on or off
    if on
    break
    if off
    check if those leds are in a horizontal or a vertical position
    if true
    get the address of the led and get the switch's address of this LED and store in var3
    if false
    break;
    if off
    break;
    if off
    break


    if it is the second one that is pressed
    if the led that is addressing this switch is on
    break;
    if off
    get the value from the var3 and light the LED according to this number
    get the value from var1 and var2 and turn them off



    // end


    so this is the pseudo code I have, can anyone advice what path I should follow here.

    Thanks in advance.
    ilteris.

  2. #2
    BertMan's Avatar
    BertMan Guest


    Did you find this post helpful? Yes | No

    Default

    Step 1 - Define your pins. Add a variable to each pin you want to control. ie

    Switch1 var PORTA.1
    LED1 = PORTA.2

    Step 2 - Add variables for temporary storage ie

    Var1 var Byte
    Var2 var Byte

    Step 3 - Add main code. Here you want to place a lot of "if then" statements to get the status of the LED's and switches (pins), then what you want the program to do when those conditions are met.

    What you are trying to do is very simple in PICBasic. Dive into the help file or manual to get an idea of all the commands PICBasic has to offer. Then download some examples that have similar code to what you are trying to accomplish. You will need to study the manual for the 18f452, as they will be some configuration needed to get all the pins to do what you want them to. Here is an example of some configuration on my 18F2520:

    TRISA= %11101010
    TRISC = %11010000
    ADCON1 = %11111111
    CMCON = %00000111
    OSCTUNE = %00000011
    OSCCON = %11111100
    SSPSTAT = %00000000
    SSPCON1 = %00100001

    If you get stuck, check out the FAQ, or do a search here on the site. If all else fails post you question here, and folks will help out if they think that you genually have a problem and not just want some spoon feeding. Good luck!

  3. #3
    ilteris's Avatar
    ilteris Guest


    Did you find this post helpful? Yes | No

    Default

    hello I managed to do some coding but I got errors, here is the code: the explanation is below :

    TRISB = %11111111
    TRISC= %00000000
    TRISD = %11110000

    state var word

    PORTC = %11111011
    PORTD = %11111111
    main:


    'if switch bottom is pressed
    if PORTB.0 then
    'put a flag that it is pressed with the state variable
    state = 1
    endif
    ' the middle button is pressed and our state is 1
    IF PORTB.2 and state == 1 THEN
    ' do readfirst subroutine
    GOsub readFirst
    else


    IF PORTB.3 and state == 2 then
    state=3
    endif
    if portb.1 and state == 3 then
    gosub readSecond
    endif
    endif
    goto main



    readFirst:
    'check if middle led is turned off

    IF PORTC.2 == 0 then
    'if it is turned off then make the bottom led and the
    one above it turned off
    low PORTD.0
    low PORTC.3
    high PORTC.2
    'problem here, should I set the others led again to
    high, because
    'they were high in the setup
    HIGH PORTC.0
    HIGH PORTC.1
    'change our state to 2 in case not to worry about this
    statement again
    state=2
    endif
    return

    readSecond:
    if portc.1 == 0 then
    low portc.3
    low portc.2
    high portc.1
    state=3
    endif
    return

Similar Threads

  1. Controlling switches with a PIC
    By TonyA in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 4th March 2008, 22:44
  2. Using LEDs as light sensors
    By skimask in forum Code Examples
    Replies: 3
    Last Post: - 30th December 2006, 22:19
  3. switches & LEDs ... my first attempt
    By yankee in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 28th January 2006, 12:51
  4. arrays LEDs Switches
    By ilteris in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 22nd October 2005, 03:05
  5. Can anyone help a beginner in a struggle?
    By douglasjam in forum mel PIC BASIC
    Replies: 1
    Last Post: - 5th May 2005, 23:29

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