I just hooked up the sensor as described by First, wiring the two digital outputs to switch 15 and 16 and also wrote the following command statement:
if rc_sw15 = 1 then p2_y=150
if rc_sw16 = 1 then p4_y=150
But when I turn the robot on and try different surfaces, the sensor doesn’t do anything, it doesn’t turn p2_y=150
Help!
i don’t think you can set p2_y to a set speed. what you want to do is set your actual drive to a set speed.
when you set p2_y to a set speed, it immediately switches back to whatever position the joystick is in, probbly around 127. therefore 127 is sent to the serout line.
this is what you want:
if rc_sw15 = 1 then drive_L = 150
or whatever drive variable you are using
You can use the p1_y, p2_y values in the default code. The SEROUT command maps the p1_y, p2_y values to the actual PWM outputs.
As for the code above. It sounds a lot like a wiring problem.
Remember -
Blue/Brown = Power and Ground
White/Black = Inputs
Pick either White or Black and wire it up. DONT USE BOTH!!!
White-wired sensors return a 1 if they detect something and a 0 if the detect nothing. Black-wired sensors are exactly opposite.
The code would be correct for a white-wired sensor on rc_sw15 + rc_sw16.
Yes, you can use the p1_y or p2_y but it CHANGES TO WHATEVER THE REAL VALUE IS FROM THE JOYSTICK. When your program runs, it sees your joystick at roughly 127, and then your code changing it to 150. This makes is continually switch from 127 to 150 to 127.
you could set your wheel to make p1_y = 150 but it’s safer, and (although this is coming from someone completely insane and mentally unstable) easier to use your drive variable.
*Originally posted by mjt902 *
**Yes, you can use the p1_y or p2_y but it CHANGES TO WHATEVER THE REAL VALUE IS FROM THE JOYSTICK. When your program runs, it sees your joystick at roughly 127, and then your code changing it to 150. This makes is continually switch from 127 to 150 to 127.you could set your wheel to make p1_y = 150 but it’s safer, and (although this is coming from someone completely insane and mentally unstable) easier to use your drive variable. **
Not true. p1_y is not “bound” to the joystick. It only gets updated when you do a SERIN. Otherwise, it acts like any other normal variable. Likewise, setting PWM1 to a value won’t actually change the value sent to the motor until you do a SEROUT.
So what you are suggesting this guy do is not put SERIN in the mainloop??
That makes sense and clears me up on a few things. Thaks for the gentle and insightful correction.
*Originally posted by mjt902 *
**So what you are suggesting this guy do is not put SERIN in the mainloop??
That makes sense and clears me up on a few things. Thaks for the gentle and insightful correction. **
NO!!! If you don’t do a SERIN, delta_t will never get updated. Nor will any of your sensors, etc.
All I’m saying is that as long as you don’t do a SERIN between the p1_y=150 and the SEROUT, p1_y can be used as a normal variable. For example:
DO
SERIN…
if rc_sw15 = 1 then p2_y=150
if rc_sw16 = 1 then p4_y=150
SEROUT…
LOOP
Until the code reaches the next SERIN, p2_y/p4_y will still have the values 150.
Back to the problem: my guess is that you have the incorrect sensor wire hooked up. You can use either, but your code needs to match. To see if this is the problem, try doing a debug? rc_sw15 right after the SERIN and see if it changes.
*Originally posted by mjt902 *
**So what you are suggesting this guy do is not put SERIN in the mainloop??
That makes sense and clears me up on a few things. Thaks for the gentle and insightful correction. **
Was that sarcastic?
p1_y is only a variable. It is not bound to the joystick. For simplicity in the default code, the value of this variable at the end of the mainloop is transfered to the motor via SEROUT.