PDA

View Full Version : How to make this works ??



CipiCips
- 19th May 2011, 12:40
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

cncmachineguy
- 19th May 2011, 14:14
Try a while loop instead of the for loop



while buttonx=1
run motor
pause something
wend

Of course you need 2 of these, just like you have 2 for..next

Archangel
- 20th May 2011, 08:17
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

aratti
- 21st May 2011, 10:01
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.

Demon
- 23rd May 2011, 15:49
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)