Shop OBEX P1 Docs P2 Docs Learn Events
Next SimpleIDE Issue: PING and 2 Servos — Parallax Forums

Next SimpleIDE Issue: PING and 2 Servos

NWCCTVNWCCTV Posts: 3,629
edited 2014-09-04 19:53 in Learn with BlocklyProp
So I know the Servos work and I know the PING works. However, in the following code only the Servo on Pin 14 will run but not the one on Pin 15. Helppppp!!!!
/*Test Ping Distance.c
Measure and display Ping))) Ultrasonic Distance Sensor distance measurements.
http://learn.parallax.com/propeller-c-simple-devices/sense-distance-ping
*/


#include "simpletools.h" // Include simpletools header
#include "ping.h" // Include ping header
#include "servo.h"
int main() // main function
{
while(1) // Repeat indefinitely
{
int cmDist = ping_cm(13); // Get cm distance from Ping)))
print("cmDist = %d\n", cmDist); // Display distance
pause(200); // Wait 1/5 second
if(cmDist <6)
{
servo_speed(14, 100); // P18 servo full speed CCW
servo_speed(15, -100); // P19 servo full speed CW
} 
else 
servo_speed(14,0);
servo_speed(15,0);
}
}

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2014-09-04 18:53
    It would really help you if adapted some form of indentation for your C programs. The else branch only covers pin 14, the pin 15 call is always executed (which I assume is effectively a stop).
    #include "simpletools.h" // Include simpletools header
    #include "ping.h" // Include ping header
    #include "servo.h"
    int main() // main function
    {
        while(1) // Repeat indefinitely
        {
            int cmDist = ping_cm(13); // Get cm distance from Ping)))
            print("cmDist = %d\n", cmDist); // Display distance
            pause(200); // Wait 1/5 second
            if(cmDist <6)
            {
                servo_speed(14, 100); // P18 servo full speed CCW
                servo_speed(15, -100); // P19 servo full speed CW
            } 
            else 
                servo_speed(14,0);
     [COLOR="#FF0000"]       servo_speed(15,0);[/COLOR]
        }
    }
    
  • NWCCTVNWCCTV Posts: 3,629
    edited 2014-09-04 19:40
    Servos on pin 14 and 15. The one on 14 works correctly. The one on 15 does not. When I pasted my code that is how it shows up, Why would the for pin 15 always be called and not 14 if they are identical?
  • kuronekokuroneko Posts: 3,623
    edited 2014-09-04 19:47
    If you want the servo_speed(?, 0) calls to be executed conditionally (i.e. in the else branch, together) then you have to group them with curly brackets. This is C, not SPIN.
    if(cmDist <6)
            {
                servo_speed(14, 100); // P18 servo full speed CCW
                servo_speed(15, -100); // P19 servo full speed CW
            } 
            else 
            {
                servo_speed(14,0);
                servo_speed(15,0);
            }
    
    Your original code is equivalent to
    if(cmDist <6)
            {
                servo_speed(14, 100);  // if
                servo_speed(15, -100); // if
            } 
            else
            {
                servo_speed(14,0);     // else
            }
            servo_speed(15,0);         // always
    
    
  • NWCCTVNWCCTV Posts: 3,629
    edited 2014-09-04 19:53
    Got it. Thanks for that. I think I am confusing myself because I am going back and forth between Spin and SimpleIDE. Trying to figure out which one is going to work better and require the least amount of coding.
Sign In or Register to comment.