I have a short test program to turn and stop the left and right wheels on a small robot driving by a NANO. When I put the motion commands in functions things get wacky. When I do get movement it doesn’t halt on one motor at all and the speed changes by itself in just a few revolutions, eventually stopping forever. I have no control of the direction, speed, or stop-movement of the two continuous turn servos where I sorta did without functions. I am trying to put all servo movements in seperate functions. You can see I am a amateur C coder so don't laugh too hard at my code.
// Test code just to operate continous rotation servo(stop
// Less than 103 on left is CW on left motor.
// greater than 103 on right is CCW on right motor.
// 103 is stop on both motors.
// #include <Servo.h>;
//
void setup()
{
};
//
void loop () {;
// Test program to call functions
//
Straight(); // Function
delay(1000);
No_movement(); // Function
delay(1000);
//
};
void Straight()
{;
Servo LeftWheel;
Servo RightWheel;
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
LeftWheel.attach(9);
RightWheel.attach(10);
//
LeftWheel.write(113); // That is center position (103) + 10
delay(500);
RightWheel.write(93); // That is centor position (103) - 10
delay(500);
};
void No_movement()
{;
Servo LeftWheel;
Servo RightWheel;
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
LeftWheel.attach(9);
RightWheel.attach(10);
LeftWheel.write(103); // That is Stop position (103)
delay(100);
LeftWheel = 0;
RightWheel.write(103); // That is Stop position (103)
delay(100);
};
Welcome! You have an interesting project, but there is a proper way to post code, this allows us to help you. Taking what you posted and putting into a readable form takes a lot of time many do not want to spend. It will only take you another 10 seconds after your figure it out. l have no clue as to what each button is or how it interacts with your code, post an annotated schematic showing how you have wired it, not pictures such as frizzy drawings they are useless. Include links to "Technical information" on the hardware parts, links to sales outlets like azon are next to useless. Doing this will have a big positive effect on your answers and will improve the accuracy of the answer a lot.
The main problem of your sketch is, that you not only put the motion commands in your functions, but that you create locals Servo objects there.
Create your Servo objects once as global objects and initialise them in setup().
Then you can put your control commands in the various functions.
Then you did something else wrong. You can post your attempt where you got this error.
Creating the Servo objects local to the functions is definitely not correct.