Problem with servo

I am doing a project using two servo with, arudino, PWM and power supply


Using above setup my servo motor working fine and then I added a code to stop servo motor
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define servoMIN 300
#define servoMAX 600

int servo1 = 0; // Servo 1
int servo2 = 1; // Servo 2
const int servoNeutral = 450; // Neutral position (adjust as needed)

bool someConditionToStopServos = false;
bool someConditionToStartServos = false; // Change this condition to start the servos

void setup() {
pwm.begin();
pwm.setPWMFreq(60);
stopServos(); // Call the function to stop the servos initially
}

void loop() {
// Your code here

// To stop the servos, call the stopServos() function when needed
if (someConditionToStopServos) {
stopServos();
}

// To start the servos, call the startServos() function when needed
if (someConditionToStartServos) {
startServos();
}
}

// Function to stop both servo motors
void stopServos() {
pwm.setPWM(servo1, 0, servoNeutral); // Set servo 1 to neutral position
pwm.setPWM(servo2, 0, servoNeutral); // Set servo 2 to neutral position
}

// Function to start both servo motors
void startServos() {
// Set servo positions to move them continuously
int position1 = 300; // Start position for servo 1
int position2 = 600; // Start position for servo 2

while (someConditionToStartServos) {
// Continuously move the servos back and forth
pwm.setPWM(servo1, 0, position1);
pwm.setPWM(servo2, 0, position2);

// Delay to control the speed of motion
delay(1000); // Adjust this delay as needed

// Swap positions to move the servos in the opposite direction
int temp = position1;
position1 = position2;
position2 = temp;

}
} after I use this code my motor stops working and I tried to add different code but it is not moving I also change the someconditiontostartservo =true but it's not moving.
Do you think that this code will make servo motor to stop and it will not execute every other code?

@oceanoasis

Topic split from another topic. Please do not add your own questions to the end of other people's topics.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

In particular, use code tags when you post code

Thank you.

2 Likes
2 Likes

I think you are checking if that variable exists?
try:

while (someConditionToStartServos==trueorwhatevercondition) {
1 Like

Delete one of those lines?

1 Like

no. Inside the parenthesis there must be an expression with result boolean.
The most simple "result" is a boolean variable itself

the variables are defined as bool (boolean) and this means it works

it does even work if you use an integer

int myInteger = 1

while(myInteger) {
}

best regards Stefan

no
these two lines declare two variables of type bool.
You could add another 20 variables of any type if you like

best regards Stefan

3 Likes

Modified the code and add startservo == true but servo still not moving

#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define servoMIN 300
#define servoMAX 600

int servo1 = 0;  // Servo 1
int servo2 = 1;  // Servo 2
const int servoNeutral = 450;  // Neutral position (adjust as needed)

bool someConditionToStopServos = false;
bool someConditionToStartServos = true; // Change this condition to start the servos

void setup() {
  pwm.begin();
  pwm.setPWMFreq(60);
  stopServos();  // Call the function to stop the servos initially
}

void loop() {
  // Your code here
  
  // To stop the servos, call the stopServos() function when needed
  if (someConditionToStopServos) {
    stopServos();
  }
  
  // To start the servos, call the startServos() function when needed
  if (someConditionToStartServos) {
    startServos();
  }
}

// Function to stop both servo motors
void stopServos() {
  pwm.setPWM(servo1, 0, servoNeutral);  // Set servo 1 to neutral position
  pwm.setPWM(servo2, 0, servoNeutral);  // Set servo 2 to neutral position
}

// Function to start both servo motors
void startServos() {
  // Set servo positions to move them continuously
  int position1 = 300; // Start position for servo 1
  int position2 = 600; // Start position for servo 2
  
  while (someConditionToStartServos == true) {
    // Continuously move the servos back and forth
    pwm.setPWM(servo1, 0, position1);
    pwm.setPWM(servo2, 0, position2);
    
    // Delay to control the speed of motion
    delay(1000); // Adjust this delay as needed
    
    // Swap positions to move the servos in the opposite direction
    int temp = position1;
    position1 = position2;
    position2 = temp;
  }
}

I also run above program using servo monitor it is giving me 300 , 600 so program is working

Hardware:- Arudino Uno, PWM , Power supply, hg90 servo motor( U can refer above Image)

Hardware I check :-
1)At pin 0 where we attach the servo motor I check the voltage using multimeter it is showing 5 volt
2) Also we give 5 volt power supply directly to servo motor after opening the motor it is continuously rotating 360 degree

Questions:-

  1. is their any property in servo motor using which we can stop the servo motor and it will not work at any other code
  2. Give me a simple code which I can try to execute servo motor which gives me motor properties like why it is not moving or the code is working or not
    Thanks for your time

> Blockquote

But didn't use code tags when posting it. Please edit your post and add them

2 Likes

You should not power the adafruit-servodriver-board from the Arduino

The onboard voltage-regulator of the arduino has not enough power for servos.

The onboard voltage-regulator of the arduino can only supply small sensors which consume minimal power.

In the picture above the additional power-supply is directly connected to the servo.
In your case if you are using the adafruit servo-board you must connect the additional power-supply to the servo-board.

As you can read here

best regards Stefan

2 Likes


Yes I have the same connection as shown in the diagram above and using this connection my servo motor also moving past few weeks until I run the above code which I sent above

Here is your code with additional debug-output
open the serial monitor adjust baudrate to 115200

then flash this code-version into the arduino.
The serial monitor will show what is going on in your code

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  }

#define dbgc(myFixedText, variableName) \
  { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }

#define dbgcf(myFixedText, variableName) \
  { \
    static float lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *

#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define servoMIN 300
#define servoMAX 600

int servo1 = 0;  // Servo 1
int servo2 = 1;  // Servo 2
const int servoNeutral = 450;  // Neutral position (adjust as needed)

bool someConditionToStopServos = false;
bool someConditionToStartServos = true; // Change this condition to start the servos

void setup() {
  Serial.begin(115200);
  while(!Serial); // wait until serial connection is ready
  Serial.println( F("Setup-Start") );
  PrintFileNameDateTime();
  dbg("01",someConditionToStopServos);
  dbg("02",someConditionToStartServos);
  pwm.begin();
  pwm.setPWMFreq(60);
  stopServos();  // Call the function to stop the servos initially
  Serial.println( F("setup() - stopServos() done") );
}

void loop() {

  // ONLY in case the variable with name "someConditionToStopServos"
  // CHANGES its value print ONCE
  dbgc("L01",someConditionToStopServos);
  
  // ONLY in case the variable with name "someConditionToStartServos"
  // CHANGES its value print ONCE
  dbgc("L02",someConditionToStartServos);
  // To stop the servos, call the stopServos() function when needed
  
  if (someConditionToStopServos) {
    stopServos();
  }
  
  // To start the servos, call the startServos() function when needed
  if (someConditionToStartServos) {
    startServos();
  }

  // once every second print NAME and value of someConditionToStopServos
  //dbgi("L99",someConditionToStopServos,1000);

  // once every second print NAME and value of someConditionToStartServos
  //dbgi("L99",someConditionToStartServos,1000);
}

// Function to stop both servo motors
void stopServos() {
  pwm.setPWM(servo1, 0, servoNeutral);  // Set servo 1 to neutral position
  pwm.setPWM(servo2, 0, servoNeutral);  // Set servo 2 to neutral position
  //Serial.println("function stopServos() done" );
}


// Function to start both servo motors
void startServos() {

  //Serial.println("entering function startServos()" );
  
  // Set servo positions to move them continuously
  int position1 = 300; // Start position for servo 1
  int position2 = 600; // Start position for servo 2
  
  while (someConditionToStartServos == true) {
    // Continuously move the servos back and forth
    pwm.setPWM(servo1, 0, position1);
    pwm.setPWM(servo2, 0, position2);
    
    // Delay to control the speed of motion
    delay(1000); // Adjust this delay as needed
    
    // Swap positions to move the servos in the opposite direction
    int temp = position1;
    position1 = position2;
    position2 = temp;
  }
  //Serial.println("leaving function startServos()" );  
}


void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );  
}


// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}

unsigned long MyTestTimer = 0;                   // Timer-variables MUST be of type unsigned long
const byte    OnBoard_LED = 2;


void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);
  
  if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
    digitalWrite(IO_Pin,!digitalRead(IO_Pin) ); 
  }
}

there are some lines of code commented
If you un-comment them more serial printing is done

best regards Stefan

2 Likes
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define servoMIN 300
#define servoMAX 600

int servo1 = 0;  // Servo 1
const int servoNeutral = 450;  // Neutral position (adjust as needed)

bool someConditionToStopServo = false;
bool someConditionToStartServo = true; // Change this condition to start the servo
#define dbgc(myFixedText, variableName) \
  { \
    static bool lastState; \
    if (lastState != variableName) { \
      Serial.print(F(myFixedText " ")); \
      Serial.print(F(#variableName " changed from ")); \
      Serial.print(lastState ? F("true") : F("false")); \
      Serial.print(F(" to ")); \
      Serial.println(variableName ? F("true") : F("false")); \
      lastState = variableName; \
    } \
  }

void setup() {
  Serial.begin(115200);
  while(!Serial); // wait until serial connection is ready
  Serial.println( F("Setup-Start") );
  PrintFileNameDateTime();
  pwm.begin();
  pwm.setPWMFreq(60);
  stopServo();  // Call the function to stop the servo initially
  Serial.println( F("setup() - stopServo() done") );
}

void loop() {
  // ONLY in case the variable with name "someConditionToStopServo"
  // CHANGES its value print ONCE
  dbgc("L01", someConditionToStopServo);
  
  // ONLY in case the variable with name "someConditionToStartServo"
  // CHANGES its value print ONCE
  dbgc("L02", someConditionToStartServo);
  
  // To stop the servo, call the stopServo() function when needed
  if (someConditionToStopServo) {
    stopServo();
  }
  
  // To start the servo, call the startServo() function when needed
  if (someConditionToStartServo) {
    startServo();
  }
}

// Function to stop the servo
void stopServo() {
  pwm.setPWM(servo1, 0, servoNeutral);  // Set servo 1 to neutral position
}

// Function to start the servo
void startServo() {
  // Set servo positions to move it continuously
  int position1 = 300; // Start position for servo 1
  int position2 = 600; // End position for servo 1
  
  while (someConditionToStartServo) {
    // Continuously move the servo back and forth
    pwm.setPWM(servo1, 0, position1);
    
    // Delay to control the speed of motion
    delay(1000); // Adjust this delay as needed
    
    // Swap positions to move the servo in the opposite direction
    int temp = position1;
    position1 = position2;
    position2 = temp;
  }
}

void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );  
}

After making some changes as per my hardware right now I am trying to move one servo motor on PWM pin 0 the above code gives me output in serial monitor as
Setup-Start Codese to true Fan.ino compiled Sep 10 2023 19:23:02 setup() - stopServo() done L02 someConditionToStartServo changed from false to true �y��X��"��\��b�^�
But the motor is still not moving don't know the reason yet :frowning:

It is weird that the second line of the serial output has this strange characters.
Are you really using this Adafruit-servo-board?

What exact type of microcontroller are you using?

Or did you connect the servos directly to IO-pin 0 and io-pin 1 on your arduino-Uno?

here is a code-version that has

more

serial printing as you left in
This code-version will make visible of your microcontroller is executing the code or not
The serial output should look like this

Setup-Start
Code running comes from file 
F:\myData\Arduino\oceanoasis-original\oceanoasis-original.ino
  compiled Sep 10 2023 16:38:17
setup() - stopServo() done
L02 someConditionToStartServo changed from false to true
Setup-Start
Code running comes from file 
F:\myData\Arduino\oceanoasis-original\oceanoasis-original.ino
  compiled Sep 10 2023 16:45:18
setup() - stopServo() done
"L02" someConditionToStartServo changed from 0 to 1
"while" position1 changed from 0 to 300
"while" position1 changed from 300 to 600
"while" position1 changed from 600 to 300
// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  }

#define dbgc(myFixedText, variableName) \
  { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }

#define dbgcf(myFixedText, variableName) \
  { \
    static float lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *


#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define servoMIN 300
#define servoMAX 600

int servo1 = 0;  // Servo 1
const int servoNeutral = 450;  // Neutral position (adjust as needed)

bool someConditionToStopServo = false;
bool someConditionToStartServo = true; // Change this condition to start the servo

void setup() {
  Serial.begin(115200);
  while(!Serial); // wait until serial connection is ready
  Serial.println( F("Setup-Start") );
  PrintFileNameDateTime();
  pwm.begin();
  pwm.setPWMFreq(60);
  stopServo();  // Call the function to stop the servo initially
  Serial.println( F("setup() - stopServo() done") );
}

void loop() {
  // ONLY in case the variable with name "someConditionToStopServo"
  // CHANGES its value print ONCE
  dbgc("L01", someConditionToStopServo);
  
  // ONLY in case the variable with name "someConditionToStartServo"
  // CHANGES its value print ONCE
  dbgc("L02", someConditionToStartServo);
  
  // To stop the servo, call the stopServo() function when needed
  if (someConditionToStopServo) {
    stopServo();
  }
  
  // To start the servo, call the startServo() function when needed
  if (someConditionToStartServo) {
    startServo();
  }
}

// Function to stop the servo
void stopServo() {
  pwm.setPWM(servo1, 0, servoNeutral);  // Set servo 1 to neutral position
}

// Function to start the servo
void startServo() {
  // Set servo positions to move it continuously
  int position1 = 300; // Start position for servo 1
  int position2 = 600; // End position for servo 1
  
  while (someConditionToStartServo) {
    // Continuously move the servo back and forth
    pwm.setPWM(servo1, 0, position1);
    dbgc("while",position1);
    
    // Delay to control the speed of motion
    delay(1000); // Adjust this delay as needed
    
    // Swap positions to move the servo in the opposite direction
    int temp = position1;
    position1 = position2;
    position2 = temp;
  }
}

void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );  
}

If you see this serial output you can conclude it is a

hardware-problem

But of course you can again reduce the code to play "I can't find the reason"

Here is a code-version for narrowing down where the strange printing starts

#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define servoMIN 300
#define servoMAX 600

int servo1 = 0;  // Servo 1
const int servoNeutral = 450;  // Neutral position (adjust as needed)

bool someConditionToStopServo = false;
bool someConditionToStartServo = true; // Change this condition to start the servo
#define dbgc(myFixedText, variableName) \
  { \
    static bool lastState; \
    if (lastState != variableName) { \
      Serial.print(F(myFixedText " ")); \
      Serial.print(F(#variableName " changed from ")); \
      Serial.print(lastState ? F("true") : F("false")); \
      Serial.print(F(" to ")); \
      Serial.println(variableName ? F("true") : F("false")); \
      lastState = variableName; \
    } \
  }

void setup() {
  Serial.begin(115200);
  while (!Serial); // wait until serial connection is ready
  Serial.println( F("Setup-Start") );
  PrintFileNameDateTime();
  pwm.begin();
  pwm.setPWMFreq(60);
  stopServo();  // Call the function to stop the servo initially
  Serial.println( F("setup() - stopServo() done") );
}

void loop() {
  Serial.println("Top of loop");
  // ONLY in case the variable with name "someConditionToStopServo"
  // CHANGES its value print ONCE
  dbgc("L01", someConditionToStopServo);

  // ONLY in case the variable with name "someConditionToStartServo"
  // CHANGES its value print ONCE
  dbgc("L02", someConditionToStartServo);

  // To stop the servo, call the stopServo() function when needed
  Serial.println("right before if (someConditionToStopServo)");
  if (someConditionToStopServo) {
    stopServo();
  }

  // To start the servo, call the startServo() function when needed
  Serial.println("right before someConditionToStartServo");
  if (someConditionToStartServo) {
    Serial.println("someConditionToStartServo is true");
    Serial.println("right before startServo()");
    startServo();
  }
}

// Function to stop the servo
void stopServo() {
  pwm.setPWM(servo1, 0, servoNeutral);  // Set servo 1 to neutral position
}

// Function to start the servo
void startServo() {
  Serial.println("entering startServo()");

  // Set servo positions to move it continuously
  int position1 = 300; // Start position for servo 1
  int position2 = 600; // End position for servo 1

  Serial.println("right before while (someConditionToStartServo)");
  while (someConditionToStartServo) {
    // Continuously move the servo back and forth
    Serial.println("right before pwm.setPWM(servo1, 0, position1)");
    pwm.setPWM(servo1, 0, position1);
    Serial.println("right AFTER pwm.setPWM(servo1, 0, position1)");

    // Delay to control the speed of motion
    delay(1000); // Adjust this delay as needed

    // Swap positions to move the servo in the opposite direction
    int temp = position1;
    position1 = position2;
    position2 = temp;
  }
}

void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );
}

Do me a favor and adjust your serial monitor to use carriage return for linefeed
it is strange that your serial output is all in one line
This

Setup-Start Codese to true Fan.ino compiled Sep 10 2023 19:23:02 setup() - stopServo() done L02 someConditionToStartServo changed from false to true �y��X��"��\��b�^�

should look like this

Setup-Start
Code running comes from file 
F:\myData\Arduino\oceanoasis-original\oceanoasis-original.ino
  compiled Sep 10 2023 16:38:17
setup() - stopServo() done
L02 someConditionToStartServo changed from false to true

You have deleted that lines of code that would print the most interesting information
You have modified the dbgc macro to only print true / false instead of any value
you have deleted all the rest of the macros that would help analyse the problem
why?

With the new code-version the serial output should look like this

Setup-Start
Code running comes from file 
F:\myData\Arduino\oceanoasis-001\oceanoasis-001.ino
  compiled Sep 10 2023 16:35:28
setup() - stopServo() done
Top of loop
L02 someConditionToStartServo changed from false to true
right before if (someConditionToStopServo)
right before someConditionToStartServo
someConditionToStartServo is true
right before startServo()
entering startServo()
right before while (someConditionToStartServo)
right before pwm.setPWM(servo1, 0, position1)
right AFTER pwm.setPWM(servo1, 0, position1)
right before pwm.setPWM(servo1, 0, position1)

Hello oceanoasis

Keep it simple and stupid.

Follow the pure example code that comes with the library or
run some tutorials for the hardware selected.
If you are happy with the results of the tutorials you can merge these to your project.

Have a nice day and enjoy coding in C++.

2 Likes


My setup
First code as u provide

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  }

#define dbgc(myFixedText, variableName) \
  { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }

#define dbgcf(myFixedText, variableName) \
  { \
    static float lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *


#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define servoMIN 300
#define servoMAX 600

int servo1 = 0;  // Servo 1
const int servoNeutral = 450;  // Neutral position (adjust as needed)

bool someConditionToStopServo = false;
bool someConditionToStartServo = true; // Change this condition to start the servo

void setup() {
  Serial.begin(115200);
  while(!Serial); // wait until serial connection is ready
  Serial.println( F("Setup-Start") );
  PrintFileNameDateTime();
  pwm.begin();
  pwm.setPWMFreq(60);
  stopServo();  // Call the function to stop the servo initially
  Serial.println( F("setup() - stopServo() done") );
}

void loop() {
  // ONLY in case the variable with name "someConditionToStopServo"
  // CHANGES its value print ONCE
  dbgc("L01", someConditionToStopServo);
  
  // ONLY in case the variable with name "someConditionToStartServo"
  // CHANGES its value print ONCE
  dbgc("L02", someConditionToStartServo);
  
  // To stop the servo, call the stopServo() function when needed
  if (someConditionToStopServo) {
    stopServo();
  }
  
  // To start the servo, call the startServo() function when needed
  if (someConditionToStartServo) {
    startServo();
  }
}

// Function to stop the servo
void stopServo() {
  pwm.setPWM(servo1, 0, servoNeutral);  // Set servo 1 to neutral position
}

// Function to start the servo
void startServo() {
  // Set servo positions to move it continuously
  int position1 = 300; // Start position for servo 1
  int position2 = 600; // End position for servo 1
  
  while (someConditionToStartServo) {
    // Continuously move the servo back and forth
    pwm.setPWM(servo1, 0, position1);
    dbgc("while",position1);
    
    // Delay to control the speed of motion
    delay(1000); // Adjust this delay as needed
    
    // Swap positions to move the servo in the opposite direction
    int temp = position1;
    position1 = position2;
    position2 = temp;
  }
}

void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );  
}

Gives below output

����[������|�����:cSetup-Start
Cod8�멲<�:����ks�0޴�S�F�״��:������)��YOG�SR��޴`�ִP�Q�W�SR���.�SR��YOG#define

Second code to check hardware :-

#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define servoMIN 300
#define servoMAX 600

int servo1 = 0;  // Servo 1
const int servoNeutral = 450;  // Neutral position (adjust as needed)

bool someConditionToStopServo = false;
bool someConditionToStartServo = true; // Change this condition to start the servo
#define dbgc(myFixedText, variableName) \
  { \
    static bool lastState; \
    if (lastState != variableName) { \
      Serial.print(F(myFixedText " ")); \
      Serial.print(F(#variableName " changed from ")); \
      Serial.print(lastState ? F("true") : F("false")); \
      Serial.print(F(" to ")); \
      Serial.println(variableName ? F("true") : F("false")); \
      lastState = variableName; \
    } \
  }

void setup() {
  Serial.begin(115200);
  while (!Serial); // wait until serial connection is ready
  Serial.println( F("Setup-Start") );
  PrintFileNameDateTime();
  pwm.begin();
  pwm.setPWMFreq(60);
  stopServo();  // Call the function to stop the servo initially
  Serial.println( F("setup() - stopServo() done") );
}

void loop() {
  Serial.println("Top of loop");
  // ONLY in case the variable with name "someConditionToStopServo"
  // CHANGES its value print ONCE
  dbgc("L01", someConditionToStopServo);

  // ONLY in case the variable with name "someConditionToStartServo"
  // CHANGES its value print ONCE
  dbgc("L02", someConditionToStartServo);

  // To stop the servo, call the stopServo() function when needed
  Serial.println("right before if (someConditionToStopServo)");
  if (someConditionToStopServo) {
    stopServo();
  }

  // To start the servo, call the startServo() function when needed
  Serial.println("right before someConditionToStartServo");
  if (someConditionToStartServo) {
    Serial.println("someConditionToStartServo is true");
    Serial.println("right before startServo()");
    startServo();
  }
}

// Function to stop the servo
void stopServo() {
  pwm.setPWM(servo1, 0, servoNeutral);  // Set servo 1 to neutral position
}

// Function to start the servo
void startServo() {
  Serial.println("entering startServo()");

  // Set servo positions to move it continuously
  int position1 = 300; // Start position for servo 1
  int position2 = 600; // End position for servo 1

  Serial.println("right before while (someConditionToStartServo)");
  while (someConditionToStartServo) {
    // Continuously move the servo back and forth
    Serial.println("right before pwm.setPWM(servo1, 0, position1)");
    pwm.setPWM(servo1, 0, position1);
    Serial.println("right AFTER pwm.setPWM(servo1, 0, position1)");

    // Delay to control the speed of motion
    delay(1000); // Adjust this delay as needed

    // Swap positions to move the servo in the opposite direction
    int temp = position1;
    position1 = position2;
    position2 = temp;
  }
}

void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );
}

Give output as

��<
����_X:]��e|��+{��^�['��:�<�ìY��ؙ�h`�ۓ��/����o<���O�g<^��_��/Z��_�g<Z��_��m���_�g<^��`��/

DIS-connect the servo-board completely from the Arduino.
Then run the last code code again.

1 Like

Also by using second code it is showing disconnected in serial plotter

To disconnect servo board from arudino as I posted below image should I run the code

Yes

1 Like

h���+��}��˚��P�i�<��%��T$ �I$��=n����/Z�}�t���h� �+ِ�E��pm�����/��E��pm�����/����o<��������h�+�ѐ�-

Same output but now the serial plotter don't show board disconnected message

Did you adjust the baudrate of the serial monitor to the baudrate that is in the code?

2 Likes