new to programing PIC and need some help

Discussion in 'Embedded Systems and Microcontrollers' started by onextwo12, Mar 31, 2014.

  1. onextwo12

    Thread Starter New Member

    Mar 31, 2014
    3
    0
    Hi,
    I am very new to programing PIC in C and need some help. I have been looking on the net for some sample code to do the following:

    I what to use one switch to turn on different outputs e.g.

    1st short switch push, turn on o/p 1
    2nd short switch push, keep o/p 1 on and turn on o/p 2
    3rd long hold , turn off o/p 1 and 2 (plus turn of o/p 1 if only 1st short switch used).

    Plus
    1st long hold, turn on o/p 1 then short delay (keep o/p 1 on) turn on o/p 2
    3rd long hold , turn off o/p 1 and 2

    But I cannot find any examples of using a switch this way.

    If anyone can help and point me in the right direction I would be grateful.

    Thanks

     
  2. nigelwright7557

    Senior Member

    May 10, 2008
    487
    71
    With this sort of problem it is best to draw a flow chart.
    Then turn that into code.
     
    onextwo12 likes this.
  3. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    When you are new to programming, take one step at a time.

    Step 1. Write a program to turn on an LED.

    Step 2. Write a program to make the LED flash once every second, i.e. on for half a second, off for half a second.

    Step 3. Write a program to make the LED turn on while a button is pressed.

    Step 4. Write a program to make the LED turn on when the button is pressed and released and turn off when the button is pressed and released a second time. Repeat the same sequence on subsequent presses of the button.

    There is more to come but this will get you programming for awhile.

    When you learn the art of programming you do not have to rely on other people's code.
     
    onextwo12 likes this.
  4. onextwo12

    Thread Starter New Member

    Mar 31, 2014
    3
    0
    I draw this flow chart, will this work?
     
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Looks good to me.
     
    onextwo12 likes this.
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    That looks good to me too. I do notice the 4 rightmost steps duplicate steps on the left, so you could join the code and only write it once.

    For problems like this a state diagram can also be very useful. Flow charts are fine but once you code as well as you speak you'll stop using them, but state diagrams will always be useful.
     
    onextwo12 likes this.
  7. onextwo12

    Thread Starter New Member

    Mar 31, 2014
    3
    0
    Yes I see what you mean is this better?

    What is a state diagram, any usefully links ?
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605

    Like that?
     
Loading...