Sketch not running properly on external power

Hi,

My sketch is not running properly on external power (12V 1A). The best way I can describe it is that it seems the clock is running very quickly or the board is resetting.

I have a button that when pressed moves a servo forward and backward 3 or so times. The light on the button turns off while this is occurring and for 15 seconds afterward. This works fine while plugged into my PC. While plugged into the 12V 1A external power supply it only does 1 forward and half a backward motion and the button lights up much more quickly than 15 seconds. I am not using delay(), I am timing based on the millis() function.

I also have a function that blinks the builtin LED based on the number of button presses. The light does not blink meaning the servo function never completes and that the count constantly stays 0. Not sure if it means the board is restarting or what.

I checked voltage from 5v to ground and got 4.9V. I checked voltage from Vin to ground and got 11.3V.

I have tried two boards with the same exact results.

Specs:
Arduino Uno
12V 1A power
1 servo
1 button with LED (330 ohm resistor)

#include <Servo.h>

Servo foodServo;
const int stopp = 90;
const int forward = 180;
const int backward = 0;
const int ledPin = 5;
const int buttonPin = 2;
const unsigned long foodDelay = 15000; //60 * 60 * 1000;
const unsigned long fwdDelay = 750;
const unsigned long bckDelay = 250;
// How many forward iterations should the servo go through?
const int dispenseAmount = 3;

// buttonState == LOW when pressed
int buttonState = 0;
unsigned long lastDispense = 0;
bool dispensing = false;
bool readyToDispense = true;
int dispenseCount = 0;

bool goingForward = true;
unsigned long dispenseTime = 0;
int dispenseLoop = 0;

int blinkCount = 0;
unsigned long lastBlink = 0;
// Delay between blinks while counting up
unsigned long blinkDelay = 100;
// Delay to separate blinks - i.e. to actually count
unsigned long blinkBigDelay = 1000;
void setup() {
  foodServo.attach(9);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(ledPin, HIGH);
  foodServo.write(stopp);
}

// Function to blink the built in LED based on the number of dispenses
void ledBlinkThink() {
  if (dispenseCount == 0) {
    digitalWrite(LED_BUILTIN, HIGH);
    return;
  }
  if (millis() > lastBlink + blinkDelay && blinkCount < dispenseCount) {
    digitalWrite(LED_BUILTIN, HIGH);
    blinkCount++;
    lastBlink = millis();
  } else {
    digitalWrite(LED_BUILTIN, LOW);
  }
  if (blinkCount >= dispenseCount) {
    blinkCount = 0;
    lastBlink = millis() + blinkBigDelay;
  }
}


void dispenseThink() { 
   if (dispenseLoop >= dispenseAmount) {
    dispenseLoop = 0;
    dispensing = false;
    dispenseCount++;
    lastDispense = millis();
    goingForward = true;
    return;
  }
  if (!dispensing) {
    return;
  }
  if (goingForward && millis() < dispenseTime + fwdDelay) {
    foodServo.write(forward);
  } else if (goingForward) {
    goingForward = false;
    dispenseTime = millis();
  }
  if (!goingForward && millis() < dispenseTime + bckDelay) {
    foodServo.write(backward);
  } else if (!goingForward) {
    goingForward = true;
    dispenseTime = millis();
    dispenseLoop++;
  }
}

void loop() {
  if (!dispensing) {
    foodServo.write(stopp);
  }
  if (millis() > lastDispense + foodDelay) {
    readyToDispense = true;
  }
  if (readyToDispense) {
    digitalWrite(ledPin, HIGH);
  }
  ledBlinkThink();
  if (dispensing) {
    dispenseThink();
  }
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW && !dispensing) {
    digitalWrite(ledPin, LOW);
    lastDispense = millis();
    dispenseTime = millis();
    dispensing = true;
    readyToDispense = false;
  }
}

goigle

May I suggest you READ THIS to help you get the best out of the forum.

Then you can include your sketch that nobody can see.

Bob.

ballscrewbob:
goigle

May I suggest you READ THIS to help you get the best out of the forum.

Then you can include your sketch that nobody can see.

Bob.

Oops, for some reason I had an edit ready with the code but forgot to actually hit save last night. Added it into the post now. Already read that thread and did a bit of research, other people's issues were similar but not the same (that's how I knew to check and include the 5v and Vin in the OP)

Update: I found a thread mentioning a 9v 1A power supply solved someone's issue so I took on from my piano and observed the same issue

Delta_G:
How do you have the servo powered? If it is pulling power through the Arduino board (dangerous) then it may be pulling more current than the onboard regulator can handle and resets the board.

Ah, it is directly powered through the board. I just basically followed the Servo sweep guide and followed its pin layout. Is there a reason this would happen via external power and not through USB (in my head I'm thinking the regulator doesn't have to work as hard through USB power so it can handle the higher current, is that correct)?

Doesn't really matter. Servos need their own power. Trivial examples with a small servo under no load may work, but there's little to be gained trying to figure out what's happening here. Just fix the problem at the root.

wildbill:
Doesn't really matter. Servos need their own power. Trivial examples with a small servo under no load may work, but there's little to be gained trying to figure out what's happening here. Just fix the problem at the root.

Yup, definitely going to power it externally now, just trying to understand everything that's happening here.

Delta_G:
When powered from USB the voltage regulator isn’t involved. When powered from 12V it is pushed to its limit.

That makes sense. Thank you both for your help!