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;
}
}