OK, Back again. I did get the new Syren Friday PM, and this time I set everything up nicely with plugs, inline fuse, and on/off switches to avoid mistakes. I loaded the code you sent me Jim, no errors, but nothing happened. So, I started analyzing and making changes. Well Jim , now I'm convinced you are testing the new guy to see if he can solve problems or just wants the answer on a silver plate. Well, I enjoy the challenges of problems like this. It's so much more satisfying to have been part of the solution. The reason I say you involved me in the process is that there were some obvious bugs in the code that would stand out by just reading thru the code. I'm referring to you specifying pin 2 in the pulsout of the third IF..THEN on each of the select gear sub routines instead of pin 4. Unfortunately it did not work after correcting this. It did get me going on my quest to get this code to work. so after working on this till 4:30 AM on both Friday and Saturday night, I realized that I got it to work, and went to bed. I fine tuned a bit today, and I actually have a working solution. It's right on the money.
Thank you so much Jim. All joking about you testing me aside, the codes you sent me were exactly what I needed, and after working out the bugs I now have a much better understanding of how the code works. Here is the code that works as of today. I know there will be more changes.
Code:
pot_position var word '<- sets up the variable for storing the pot value.
time var word
gear_A con 961 '6th gear
gear_B con 803 '5th gear
gear_C con 682 '4th gear
buttonA var nib '<- nibble variable holds 0-1.
buttonB var nib
buttonC var nib
prevA var nib
prevB var nib
prevC var nib
deadband con 5 '<- we want this as low as will reliably operate.
gain con 4
loop:
buttonA = in12
buttonB = in13
buttonC = in14
if (buttonA = 0) AND (prevA = 1) then
gosub select_gearA
endif
if (buttonB = 0) AND (prevB = 1) then
gosub select_gearB
endif
if (buttonC = 0) AND (prevC = 1) then
gosub select_gearC
endif
prevA = buttonA
prevB = buttonB
prevC = buttonC
goto loop
select_gearA:
low 4
gosub get_pot_position
time=2870 + gain*(pot_position - gear_A)
if (pot_position < (gear_A + deadband)) AND (pot_position > (gear_A - deadband)) then
pulsout 4, 2870 '<- stop the motor if running
endif
if pot_position > (gear_A + deadband) then
pulsout 4, time
endif
if pot_position < (gear_A - deadband) then
pulsout 4, time
endif
goto select_gearA
select_gearB:
low 4
gosub get_pot_position
time=2870 + gain*(pot_position - gear_B)
if (pot_position < (gear_B + deadband)) AND (pot_position > (gear_B - deadband)) then
pulsout 4, 2870 '<- stop the motor if running
endif
if pot_position > (gear_B + deadband) then
pulsout 4, time
endif
if pot_position < (gear_B - deadband) then
pulsout 4, time
endif
goto select_gearB
select_gearC:
low 4
gosub get_pot_position
time=2870 + gain*(pot_position - gear_C)
if (pot_position < (gear_C + deadband)) AND (pot_position > (gear_C - deadband)) then
pulsout 4, 2870 '<- stop the motor if running
endif
if pot_position > (gear_C + deadband) then
pulsout 4, time
endif
if pot_position < (gear_C - deadband) then
pulsout 4, time
endif
goto select_gearC
get_pot_position:
adin 0, pot_position
serout s_out,i9600,["Analog input reading:",dec pot_position,13]
return
The way it is now there are only 2 slight problems.
first one is that I have to reset before i can select a different gear position. I guess once it's stopped at a position it keeps running the loop. This is actually good, because it causes the motor to counteract any movement that could possibly cause the gearbox to pop out of gear.
the second one is a problem that i have to correct. the way I have the proportional control set up, once the new gear position is further away the base time + proportional time exceed 4000, and the motor either stops or drops to a very slow speed until it's close enough to drop below either 4000 or above 2000. I'm not sure how I am going to adjust that yet. the final hurdle I solved that had me all confused was when at 4:00 AM I figured out that for whatever reason the pulse value that stopped the motor in my case was 2870 and not 3000. After I figured that out I got it working perfectly for the 3 gear positions on the gearbox itself. It's late again. Thanks for the guidance, I appreciate it very much. BTW, We are just getting started. LOL.