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)