Servos and motors bug
Franny
Posts: 127
in Propeller 1
Wrote this code for a small camera rover with servos and motors, using pin 8 to constantly send PWM into the motor controller and when moving on to a different loop for servos only on different pins they wouldn't work until turning off pin 8, why is that?

Comments
/*2 Axis Cam Rover by electron spectrum*/
#include "simpletools.h" // Include simple tools
#include "servo.h"
int main() // Main function
{
int x = 900;
int y = 900;
int t = 13;
while(1)
{
high(16);pause(t);low(16);pause(t);high(17);pause(t);low(17);pause(t);high(18);pause(t);low(18);pause(t);high(19);pause(t);low(19);pause(t);high(20);pause(t);low(20);pause(t);high(21);pause(t);low(21);pause(t);high(22);pause(t);low(22);pause(t);high(23);pause(t);low(23);pause(t);high(22);pause(t);low(22);pause(t);high(21);pause(t);low(21);pause(t);high(20);pause(t);low(20);pause(t);high(19);pause(t);low(19);pause(t);high(18);pause(t);low(18);pause(t);high(17);pause(t);low(17);pause(t);high(16);pause(t);low(16);pause(t);
high(16);pause(t);low(16);pause(t);high(17);pause(t);low(17);pause(t);high(18);pause(t);low(18);pause(t);high(19);pause(t);low(19);pause(t);high(20);pause(t);low(20);pause(t);high(21);pause(t);low(21);pause(t);high(22);pause(t);low(22);pause(t);high(23);pause(t);low(23);pause(t);high(22);pause(t);low(22);pause(t);high(21);pause(t);low(21);pause(t);high(20);pause(t);low(20);pause(t);high(19);pause(t);low(19);pause(t);high(18);pause(t);low(18);pause(t);high(17);pause(t);low(17);pause(t);high(16);pause(t);low(16);pause(t);
while(1)
{
high(0);
high(2);
high(4);
high(6);
servo_angle(8, 7500);
if ((input(15) == 1) && (input(12) == 1)) break;
if ((input(14) == 1) && (input(12) == 0)) low(6);
if ((input(14) == 0) && (input(12) == 1)) low(4);
if (input(13) == 1) low(2);
if (input(15) == 1) low(0);
pause(20);
}
high(16);pause(t);low(16);pause(t);high(17);pause(t);low(17);pause(t);high(18);pause(t);low(18);pause(t);high(19);pause(t);low(19);pause(t);high(20);pause(t);low(20);pause(t);high(21);pause(t);low(21);pause(t);high(22);pause(t);low(22);pause(t);high(23);pause(t);low(23);pause(t);high(22);pause(t);low(22);pause(t);high(21);pause(t);low(21);pause(t);high(20);pause(t);low(20);pause(t);high(19);pause(t);low(19);pause(t);high(18);pause(t);low(18);pause(t);high(17);pause(t);low(17);pause(t);high(16);pause(t);low(16);pause(t);
high(16);pause(t);low(16);pause(t);high(17);pause(t);low(17);pause(t);high(18);pause(t);low(18);pause(t);high(19);pause(t);low(19);pause(t);high(20);pause(t);low(20);pause(t);high(21);pause(t);low(21);pause(t);high(22);pause(t);low(22);pause(t);high(23);pause(t);low(23);pause(t);high(22);pause(t);low(22);pause(t);high(21);pause(t);low(21);pause(t);high(20);pause(t);low(20);pause(t);high(19);pause(t);low(19);pause(t);high(18);pause(t);low(18);pause(t);high(17);pause(t);low(17);pause(t);high(16);pause(t);low(16);pause(t);
while(1)
{
high(0);
high(2);
high(4);
high(6);
servo_angle(8, 20000);
if ((input(15) == 1) && (input(12) == 1)) break;
if ((input(14) == 1) && (input(12) == 0)) low(6);
if ((input(14) == 0) && (input(12) == 1)) low(4);
if (input(13) == 1) low(2);
if (input(15) == 1) low(0);
pause(20);
}
servo_angle(8, 0);
high(16);pause(t);low(16);pause(t);high(17);pause(t);low(17);pause(t);high(18);pause(t);low(18);pause(t);high(19);pause(t);low(19);pause(t);high(20);pause(t);low(20);pause(t);high(21);pause(t);low(21);pause(t);high(22);pause(t);low(22);pause(t);high(23);pause(t);low(23);pause(t);high(22);pause(t);low(22);pause(t);high(21);pause(t);low(21);pause(t);high(20);pause(t);low(20);pause(t);high(19);pause(t);low(19);pause(t);high(18);pause(t);low(18);pause(t);high(17);pause(t);low(17);pause(t);high(16);pause(t);low(16);pause(t);
high(16);pause(t);low(16);pause(t);high(17);pause(t);low(17);pause(t);high(18);pause(t);low(18);pause(t);high(19);pause(t);low(19);pause(t);high(20);pause(t);low(20);pause(t);high(21);pause(t);low(21);pause(t);high(22);pause(t);low(22);pause(t);high(23);pause(t);low(23);pause(t);high(22);pause(t);low(22);pause(t);high(21);pause(t);low(21);pause(t);high(20);pause(t);low(20);pause(t);high(19);pause(t);low(19);pause(t);high(18);pause(t);low(18);pause(t);high(17);pause(t);low(17);pause(t);high(16);pause(t);low(16);pause(t);
while(1)
{
if ((input(15) == 1) && (input(12) == 1)) break;
if (input(14) == 1) x = x + 3; servo_angle(24, x);
if (input(12) == 1) x = x - 3; servo_angle(24, x);
if (input(13) == 1) y = y + 3; servo_angle(5, y);
if (input(15) == 1) y = y - 3; servo_angle(5, y);
if (x > 1799) x = 1799;
if (x < 1) x = 1;
if (y > 1799) y = 1799;
if (y < 1) y = 1;
pause(1);
}
}
}
Code tags look like this:
code
/code
With both inside square brackets.
Also, are you familiar a "for-loop"? It's a way to repeat a certain snippet of code a specific number of times. You can also write multiple functions to run similar pieces of code in different places. For instance, you could have some code like so:
int main () { high(16); pause(t); low(16); pause(t); high(17); pause(t); low(17); pause(t); high(18); pause(t); low(18); pause(t); high(19); pause(t); low(19); pause(t); }and it could be re-written as such:
void blink (const int pinNumber, const int delay) { high(pinNumber); pause(delay); low(pinNumber); pause(delay); } int main () { for (int pin = 16; pin < 20; ++pin) { blink(pin); } }#include "simpletools.h" // Include simple tools
#include "servo.h"
int main() // Main function
{
int x = 900;
int y = 900;
int t = 13;
while(1)
{
while(1)
{
high(0);
high(2);
high(4);
high(6);
servo_angle(8, 20000);
if ((input(15) == 1) && (input(12) == 1)) break;
if ((input(14) == 1) && (input(12) == 0)) low(6);
if ((input(14) == 0) && (input(12) == 1)) low(4);
if (input(13) == 1) low(2);
if (input(15) == 1) low(0);
pause(20);
}
servo_angle(8, 0);
pause(1000);
while(1)
{
if ((input(15) == 1) && (input(12) == 1)) break;
if (input(14) == 1) x = x + 3; servo_angle(24, x);
if (input(12) == 1) x = x - 3; servo_angle(24, x);
if (input(13) == 1) y = y + 3; servo_angle(5, y);
if (input(15) == 1) y = y - 3; servo_angle(5, y);
if (x > 1799) x = 1799;
if (x < 1) x = 1;
if (y > 1799) y = 1799;
if (y < 1) y = 1;
pause(1);
}
}
}
What for?
#include "simpletools.h" // Include simple tools #include "servo.h" int main() // Main function { int x = 900; int y = 900; int t = 13; while(1) { while(1) { high(0); high(2); high(4); high(6); servo_angle(8, 20000); if ((input(15) == 1) && (input(12) == 1)) break; if ((input(14) == 1) && (input(12) == 0)) low(6); if ((input(14) == 0) && (input(12) == 1)) low(4); if (input(13) == 1) low(2); if (input(15) == 1) low(0); pause(20); } servo_angle(8, 0); pause(1000); while(1) { if ((input(15) == 1) && (input(12) == 1)) break; if (input(14) == 1) x = x + 3; servo_angle(24, x); if (input(12) == 1) x = x - 3; servo_angle(24, x); if (input(13) == 1) y = y + 3; servo_angle(5, y); if (input(15) == 1) y = y - 3; servo_angle(5, y); if (x > 1799) x = 1799; if (x < 1) x = 1; if (y > 1799) y = 1799; if (y < 1) y = 1; pause(1); } } }#include "simpletools.h" // Include simple tools #include "servo.h" int main() // Main function { int x = 900; int y = 900; int t = 13; while(1) { while(1) { high(0); high(2); high(4); high(6);There's a pause(1000); so it won't break out of the second loop until you hit those to buttons again, that part is fine, the 20000 is for a motor controller so it's fine too, the only question is why do I have to turn off pin 8 so that the next loop works?
Gotcha thanks!