Program on Arduino works only when connected to computer

Hi everyone.

I'm using an Arduino Uno with a 25Kg Feetech servo motor and encoder.
The problem I'm having is that when the Arduino is powered via USB from the computer the program works correctly, while if it is connected via USB to an external power supply or if it is powered via the Jack from 12V to 7V the program works for the first 10 seconds then it stops.

How come?

Thank you for your availability.

Can you show us your code?

Can you see something like this in your code?

while(!Serial)
{
}

With 12 V Arduino 5 V voltage regulator can get very hot and stop working.

ahahah no man, I would never do such a thing.

This is my code:

#include <Servo.h>

#define servoPIN 9
#define inputCLK 4
#define inputDT 5
#define inputButton 7
#define led 13

// Create a Servo object
Servo myservo;

int counter = 0, currentStateCLK, previousStateCLK, servoPositionMin = 94, servoPositionMid1 = 85, servoPositionMid2 = 75, servoPositionMid3 = 65, servoPositionMax = 56, valButton = 0;
bool stateFunction = false, goAtoB = true;
unsigned long oldTimeMillis = 0;

void setup() {

  pinMode(inputCLK, INPUT);
  pinMode(inputDT, INPUT);
  pinMode(inputButton, INPUT_PULLUP);
  pinMode(led, OUTPUT);

  digitalWrite(led, LOW);

  // Setup Serial Monitor
  Serial.begin(9600);

  // Attach servo on pin 9 to the servo object
  myservo.attach(servoPIN);

  //EEPROM.write(0, true); //togliere il commento per far resettare la posizione del servomotore**************************************************

  // Read the initial state of inputCLK
  // Assign to previousStateCLK variable
  previousStateCLK = digitalRead(inputCLK);

  myservo.write(servoPositionMin);
}

void loop() {

  // Read the current state of inputCLK
  currentStateCLK = digitalRead(inputCLK);
  valButton = digitalRead(inputButton);

  if (valButton == LOW) {
    if (millis() - oldTimeMillis >= 10000) {
      if (!stateFunction) {
        stateFunction = true;
        digitalWrite(led, HIGH);
      } else {
        stateFunction = false;
        digitalWrite(led, LOW);
      }
      myservo.write(servoPositionMin);
      counter = 0;
      oldTimeMillis = millis();
    }
  } else if (valButton == HIGH) {
    oldTimeMillis = millis();
  }

  if (currentStateCLK != previousStateCLK) {

    // If the inputDT state is different than the inputCLK state then
    // the encoder is rotating counterclockwise

    if (digitalRead(inputDT) != currentStateCLK) {
      counter--;
      goAtoB = false;
    } else {
      // Encoder is rotating clockwise
      counter++;
      goAtoB = true;
    }

    // Update previousStateCLK with the current state
    previousStateCLK = currentStateCLK;
  }

  if (!stateFunction) {
    // If the previous and the current state of the inputCLK are different then a pulse has occured

    if (counter == 6 && goAtoB == true) {
      myservo.write(servoPositionMax);
    }

    if (counter == 4 && goAtoB == false) {
      myservo.write(servoPositionMin);
    }

  } else {

    if (counter == 0) {
      myservo.write(servoPositionMin);
    }

    if (counter == 2) {
      myservo.write(servoPositionMid1);
    }

    if (counter == 4) {
      myservo.write(servoPositionMid2);
    }

    if (counter == 6) {
      myservo.write(servoPositionMid3);
    }

    if (counter == 8) {
      myservo.write(servoPositionMax);
    }
  }
}

Yes I know. I said I've tried all kinds of voltages, but nothing has worked

Pleas show your connection diagram with designating pin numbers

How it all are powered?

With the 5V of Arduino the are enough in my opinion.

But my real question is: why does it work by powering the arduino from the USB socket of the computer, while if I connect it to an external power supply it doesn't!!!

In what arduino pin do you connect the external power supply?
To the round DC connector?

I have no idea about your problem but using lots of !!!! won't help you solve the problem more fast.

What I know is: The encoder can give better response when using interrupt.

Try:

#include <Arduino.h>
#include <Servo.h>
#include <Rotary.h>
#include <Bounce2.h>

#define servoPIN 9
#define inputCLK 2
#define inputDT 3
#define inputButton 4
#define ledPIN 13

Bounce2::Button button = Bounce2::Button();


Rotary r = Rotary(inputDT, inputCLK);

// Create a Servo object
Servo myservo;

const int servoPositionMin = 0, servoPositionMid1 = 45,
          servoPositionMid2 = 90, servoPositionMid3 = 135, servoPositionMax = 180;

int counter = 0;
bool ledState = false;

void handleEncoder()
{
  unsigned char result = r.process();
  if (result == DIR_NONE)
  {
  }
  else if (result == DIR_CW)
  {
    counter++;
  }
  else if (result == DIR_CCW)
  {
    counter--;
  }
  if(counter < 0)
  {
    counter = 0;
  }
  else if(counter > 4)
  {
    counter = 4;
  }

  Serial.println(counter);
}

void setup()
{
  // Setup Serial Monitor
  Serial.begin(9600);

  button.attach(inputButton, INPUT_PULLUP);
  button.setPressedState(LOW);
  button.interval(5);

  attachInterrupt(digitalPinToInterrupt(inputCLK), handleEncoder, CHANGE);
  attachInterrupt(digitalPinToInterrupt(inputDT), handleEncoder, CHANGE);

  myservo.write(servoPositionMin);

  // Attach servo on pin 9 to the servo object
  myservo.attach(servoPIN);
}

void loop()
{
  button.update();

  if (button.pressed())
  {
    Serial.println("Button pressed");
    
    ledState = !ledState;
    digitalWrite(ledPIN, ledState);

    if(ledState == false)
    {
      counter = 0;      
    }
    else
    {
      counter = 4;
    }
  }
  switch (counter)
  {
    case 0:
      myservo.write(servoPositionMin);
      break;
    case 1:
      myservo.write(servoPositionMid1);
      break;
    case 2:
      myservo.write(servoPositionMid2);
      break;
    case 3:
      myservo.write(servoPositionMid3);
      break;
    case 4:
      myservo.write(servoPositionMax);
      break;
    default:
      break;
  }
}

We don't work on opinions here we work on facts. Your opinions are at odds with the facts.

How much current will your stepping motor take? Remember. A stepping motor draws the maximum current when it is stationary.
A servo can pull up to 1A stall current every time it starts to move from a standing start.

This is almost certainly caused by an internal regulator over heating because you are drawing too much current. When powering it from the DC jack.

It sounds like your computer is not a very good one and the USB current protection it should have is not kicking in correctly.

I tried to connect the external power via the round DC connector with 12/10/9/8/7V and nothing works. Via the USB of the Arduino UNO connecting it first to the USB socket of the computer everything works, then to a USB socket of a my power strip and nothing works anymore

Your scheme is incorrect. Please note that the arduino can not a power source for the servos, motors, displays etc

When you power the board via round DC connector - it works through internal board regulator. This regulator doesn't have enough ability to feed your servo, it designed to power the board only.

You need to power your servo with separate power supply. As a compromise, you can power both board and servo with 5v external power supply, connected to board (to 5v and GND pins) and to servo in parallel .

Unfortunately it didn't work, but thanks anyway for taking the time to make code very similar to mine

Your 5V regulator (circled in red below) may be fried from demanding too much current through it.

One proper way to power 5V devices is by using an external power source as depicted.

Maybe you are right on everything you said

I've tried powering the Arduino and all other components via the Vin and GND pin and everything works fine, so I guess I'll use this power mode.
Instead, trying to power everything through the 5V pin, it works for the first 10 seconds and then doesn't make the servo move anymore

Yes you are right. Trying to power the components with an external power supply everything works.

Can you tell me what doesn't works?

Can you try this example?

The encoder works correctly, but what was wrong before was the servomotor since the current given by Arduino was not sufficient

The encoder works with interrupt?

Are you interested in this feature?