How to make this works ??


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2011
    Posts
    17

    Default How to make this works ??

    Hi

    I have a one small problem, I cant figure it out ?

    I have two buttons, PIC 16f877a and a Servo Motor.

    My goal is when one button is pressed that motor turn left, and when another one button is pressed that motor turns to the right side, and motor should turn with low spead.
    This is the code

    button1 Var BIT
    button2 Var BIT
    i Var Byte

    start:

    If bottun1=1 then
    for i= 1 to 50
    PULSOUT portd.2, 220
    PAUSE 20
    next i
    ELSE
    ENDIF

    IF button2=1 then
    for i =1 to 50
    PULSOUT portd.2, 80
    PAUSE 20
    next i
    ELSE
    ENDIF

    GOTO start

    Well this is quite easy and I've manage to do that, motor turn slowly to the left and right.
    My porblem is that I wanna make program to run like this:

    1. WHen button 1 is pressed motor turn slowly to one side, and If I relase button then motor stops at his current location (on my program above motor will stop only when it do FOR loop to the end)

    2.If motor turn to one side, and I press second button how to make motor to turn on the other side, at same speed.


    Ufff :)

    THx

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


    Did you find this post helpful? Yes | No

    Default Re: How to make this works ??

    Try a while loop instead of the for loop

    Code:
    while buttonx=1
     run motor
     pause something
    wend
    Of course you need 2 of these, just like you have 2 for..next
    -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!

  3. #3
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: How to make this works ??

    Hello CipiCips,
    Ok first off make sure you spell words the same way every time Button <> Botton.

    Second If you are using R/C servos is there not some setting to cause them to stop ( I am not an R/C guy ) if so perhaps between else and endif you could specify that setting.

    I think I would group my code into sub routines and make a main loop which checks the buttons constantly, something like so:
    mainloop:
    while 1 = 1
    if button1 then gosub right
    if button2 then gosub left
    wend
    Do not forget to put return in both sub routines
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  4. #4
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: How to make this works ??

    Follow Joe advice with reference to the wording. Next you should associate the button name to an input pin of your pic. Using the TRIS command make two pins input and then associate them to your buttons alias.

    Button1 VAR PORTB.0
    Button2 VAR PORTB.1

    I have used portB as an example you can use any port you will consider handy.
    Remember to place a pulldown resistor (4.7 to 10k) and a 10 nanoFarad across
    For debouncing

    Cheers

    Al.
    All progress began with an idea

  5. #5
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,601


    Did you find this post helpful? Yes | No

    Default Re: How to make this works ??

    This is just a comment about variable names. If you set this project aside and come back to it in a while, you might not remember which button turns what way.

    Try to use variable names that mean something:

    ButtonL
    ButtonR

    That way you will know at a glance what to expect in your code. It also makes it easier to others if you work in a team environment, or share your code with others.

    (you don't have to do this, it's just to help make things clear)
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

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