I am currently working on a sketch/project with an Arduino Uno to that first allows the user to control how many times a stepper motor runs through a cycle and then through user input begins the cycles.
When the sketch/circuit is connected to the USB power supply it works as intended, when I plug in a 9V DC power supply without the USB connected, the program just starts and the motor immediately starts running without user input. If I have both the 9V supply and USB plugged in it works as intended.
The code is the same each time, which is why I am having trouble wrapping my head around why the voltage level would cause this.
Thanks.
My code is:
#include <LiquidCrystal.h>
#include <Stepper.h>
#define STEPS 200
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Stepper stepper(STEPS, 7, 8, 9, 10);
//Input & Button Logic
const int numOfInputs = 3;
const int inputPins[numOfInputs] = {0,13,6};
int inputState[numOfInputs];
int lastInputState[numOfInputs] = {LOW,LOW,LOW};
bool inputFlags[numOfInputs] = {LOW,LOW,LOW};
long lastDebounceTime[numOfInputs] = {0,0,0};
long debounceDelay = 5;
//LCD Menu Logic
const int numOfScreens = 1;
int currentScreen = 0;
String screens[numOfScreens][2] = {{"Cycles",""}};
int parameters[numOfScreens];
int SWITCH = 0;
int cycles = 1;
void setup() {
for(int i = 0; i < numOfInputs; i++) {
pinMode(inputPins[i], INPUT);
//digitalWrite(inputPins[i], HIGH); // pull-up 20k
}
Serial.begin(9600);
parameters[0] = (int)50; //Test Type
lcd.begin(16,2); //Initialize LCD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Friction Test");
}
void loop() {
setInputFlags();
resolveInputFlags();
if(SWITCH == 1){
int sensorReading = analogRead(A0);
int motorSpeed = map(sensorReading,0,1023,0,100);
if (motorSpeed > 0){
stepper.setSpeed(motorSpeed);
}
armRotate();
SWITCH = 0;
cycles = 1;
}
}
void setInputFlags() {
for(int i = 0; i < numOfInputs; i++) {
int reading = digitalRead(inputPins[i]);
if (reading != lastInputState[i]) {
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (reading != inputState[i]) {
inputState[i] = reading;
if (inputState[i] == HIGH) {
inputFlags[i] = HIGH;
}
}
}
lastInputState[i] = reading;
}
}
void resolveInputFlags() {
for(int i = 0; i < numOfInputs; i++) {
if(inputFlags[i] == HIGH) {
inputAction(i);
inputFlags[i] = LOW;
printScreen();
}
}
}
void inputAction(int input) {
if(input == 0) {
parameters[currentScreen]++;;
}
else if(input == 1) {
parameters[currentScreen]--;
}
else if(input == 2){
if (SWITCH == 1){
SWITCH = 0;
}
else if (SWITCH == 0){
SWITCH = 1;
}
}
}
void printScreen() {
lcd.clear();
lcd.print(screens[currentScreen][0]);
lcd.setCursor(0,1);
lcd.print(parameters[currentScreen]);
lcd.print(" ");
lcd.print(screens[currentScreen][1]);
}
void armRotate(){
while(cycles<=parameters[0]){
lcd.clear();
lcd.setCursor(0,0); lcd.print("Rotation #"); lcd.print(cycles); lcd.print("of"); lcd.print(parameters[0]);
stepper.step(STEPS/3);
stepper.step(-STEPS/3);
cycles++;
}
SWITCH = 0;
}
