View Full Version : Mapping Buttons to PWMs
actorindp
09-02-2004, 22:05
Hey guys, how can i map a joystick button to say put pwm08 = 200
Thanks
-Nick
a.k.a
n00b under stress
Ryan Cumings
09-02-2004, 22:08
In a bit of pseudo code
if(p1_sw_trig) {
// When we hold down the button do this
pwm08 = 200;
}
else {
// When we are not holding down the button, do this
pwm08 = 127;
}
deltacoder1020
09-02-2004, 22:09
say you want the trigger to set pwm3 to 200 when pressed, 127 when not:
if(p1_sw_trig) pwm03 = 200;
else pwm03 = 127;
any of the button status variables will work in the if statement.
--edit--
lol, apparently ryan and I are both forum hawks ;)
actorindp
09-02-2004, 22:25
now where do i throw this code?
In default routine?
-Nivk
ShadowKnight
09-02-2004, 23:03
A good majority of the working modifications you make to the default code will go into user_routine.c, but you will want to throw your autonomous code into a specified area of user_routine_fast.c. There actually is a section of the user_routine.c file that is marked with comments indicating that that is where you need to include custom code. Hope that helps.
FotoPlasma
09-02-2004, 23:05
Another composition of this if-statement would be:
(p1_sw_trig)?pwm03 = 200:pwm03 = 127;
deltacoder1020
10-02-2004, 00:17
or even (if you want to be fancy)
pwm03 = p1_sw_trig ? 200 : 127;
as a general rule of thumb, if you're working from the default code without massive modifications, anything dealing with user control (such as button mapping) will go in Default_Routine().
Joe Ross
10-02-2004, 09:06
If you want to skip the if statement, you can use the following code
pwm03 = 127 + 63 * p1_sw_trig;
If you want your motor to be able to go backwards also, you can use 2 switches with the following code.
pwm03 = 127 + (63 * p1_sw_trig) - (63 * p1_sw_top);
deltacoder1020
10-02-2004, 10:59
If you want to skip the if statement, you can use the following code
pwm03 = 127 + 63 * p1_sw_trig;
If you want your motor to be able to go backwards also, you can use 2 switches with the following code.
pwm03 = 127 + (63 * p1_sw_trig) - (63 * p1_sw_top);
just a cautionary real-world note, although this may work for the FRC, in actual C, the value of "true" for booleans is not always 1 - in fact, it's often -1. :)
but yes, for the FRC, one could use Joe's code.
just a cautionary real-world note, although this may work for the FRC, in actual C, the value of "true" for booleans is not always 1 - in fact, it's often -1. :)
but yes, for the FRC, one could use Joe's code.
Well, to be accurate, the value of true is not-false, that is, anything but 0. But p1_sw_trig is a bit, representing 1 for on, and 0 for off, so it'll be okay.
Oh, and yall don't know how lucky you have it (well, most of you do). Back in my day, we didn't have your stinkin' order of operations, and had to cram our code full of paretheses. (Or, more accurately, casually forget to do so and hit yourself when you see robot go crazy.) Yet another benefit of C over PBASIC. I, for one, am breathing a sight of relief.
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.