Some help for a beginner
Hey, I'm pretty new to microcontrollers, so I was wondering whether anyone could help me with what I'm trying to make. The overall goal is to make a vehicle that travels a certain distance and then stops when it reaches that distance. I want to hook up a switch to one of the inputs on the BASIC stamp--when the switch closes the circuit, the BASIC stamp saves it to a variable. So say I press the switch 5 times, the variable will say 5. Then, an output switch will send out a signal to a servo, and the number of times the signal is sent out will be the number of times the switch was pressed, basically (I'll have to multiply the number of times its looped by a certain number so that it travels the distance I want it to). Once its repeated this many times, the motor will engage in reverse, braking the vehicle. This is the code I've written so far:
I have a couple of questions--first, I know that the BASIC stamp will read the PULSIN command for every 2 microseconds that power is run through it--so say I press the switch for 8 microseconds, will that create 4 separate instances of PULSIN that will be stored in the variable? Also, I'm pretty sure the code is wrong in lots of places...this is my first time coding a microcontroller :P...any suggestions on how to fix the places I've screwed up in?
DIRS=%0000000000000011 SwitchIn VAR BYTE TotalSwitch VAR WORD PULSIN 4, SwitchIn DO IF SwitchIn=1 THEN TotalSwitch = TotalSwich+1 LOOP i VAR WORD i = TotalSwitch DO PULSOUT 15, 300 PAUSE 10 LOOP i=20 DO PULSOUT 15, -300 PAUSE 10 LOOP END
I have a couple of questions--first, I know that the BASIC stamp will read the PULSIN command for every 2 microseconds that power is run through it--so say I press the switch for 8 microseconds, will that create 4 separate instances of PULSIN that will be stored in the variable? Also, I'm pretty sure the code is wrong in lots of places...this is my first time coding a microcontroller :P...any suggestions on how to fix the places I've screwed up in?
Comments
Look at the BUTTON instruction for the switch input.
Look at the FOR...NEXT instruction to cycle the number of times the button was pressed
When the code exits the FOR ... NEXT loop then stop the motor with PULSOUT 15,750
Jeff T
Time_out CON 500
Btn············ PIN···· 0
btnWrk········· VAR···· Byte
Idx VAR Word
Pressed VAR Byte
Main:
DO WHILE Idx<Time_out
· PAUSE 5
· BUTTON Btn, 0, 255, 255, btnWrk, 0, No_Press
· DEBUG "*"
· Pressed=Pressed+1
· Idx=0
· No_Press:
· Idx=Idx+1
LOOP
DEBUG CR,"Timed out" ," ",? Pressed
END
Once you cease pressing the button the variable Time_out controls how long it is before the program continues. Run this as stand alone code and play with the settings.
Jeff T.
Here's the code:
when I run it, it just debugs IN4 and Btn, and doesn't start the servo nor timeout...
VDD to one side of the button
on the other side of the button connect a 220 ohm resistor
connect the other end of the 220 ohm resistor to P4
now take a 10K ohm resistor and connect one end to P4
connect the other end of the 10K resistor to VSS
try your program again and see how it responds
Jeff T.
Indeed if IN4 always equals 1, the program will loop forever since the variable TIME will never be incremented, and thus will never reach the LOOP UNTIL value of 200. You never mention what the DEBUG values are for IN4 and Btn. It might be interesting to put in a DEBUG statement for TIME as well.
You don't mention what is attached to pin port 4, nor how it is hooked up (circuitry).
I suppose it's fair to presume that "Starting Servo" is never displayed?
You have also not indicated to us, nor to the PBASIC compiler which PBASIC Stamp you are using?
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sorry, in the original code I have
at the beginning, I just forgot to paste it into my post.
I have a switch connected to pin 4 through a 200 ohm resistor, with a 10k pulldown resistor. The debug values for IN4 indicate that the button circuit is working--it's 0 when it's not pressed, and 1 when it is. The Btn values also seem to be working, as they store the number of times I press the button. When I put in a DEBUG statement for TIME, it went up to 200, then displayed "Starting Servo" and restarted. Also, the Btn variable resets itself. However, the servo itself (which is correctly connected to pin 14, I checked with a test program and it worked fine) never turns on. That's what makes me think I wrote the loop code wrong...
Judith
Post Edited (Judith) : 1/6/2008 9:04:55 PM GMT
If you are powering the servo directly from the Stamp's power supply, it may be causing a brown-out condition, and the Stamp itself is resetting. You can check that by adding the following DEBUG statement right at the beginning of your program, before any looping takes place: DEBUG "STARTING". "STARTING" should appear once and ONLY once during any iteration of the program. If it appears more than once, the Stamp is being RESET.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thanks! That seems to have been the problem, I gave the servo its own 9V power supply and it's working fine [noparse]:)[/noparse]
Thanks again,
Judith
Using 9 volts DC for a servo is a bit high. Generally one doesn't supply them with more than 6-7.5 volts DC.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔