Sketch not running properly without USB

Hey guys,

Not sure it here is the right place for that but…

I’m build a slider for my dslr and I’m trying to add a stepper motor controle by an arduino. I’m also using a potentiometer to set the motor speed and a push button to change its direction. As I’m new with arduino, I first went through the examples (button, stepper motor velocity control and state change detection).

I’ve tried just adding the needed parts from each example so everything could work properly.The point is: when plugged to my laptop the code is running fine (at least as I think it should) i.e. I can set the motor speed using the potentiometer and when I press the push button the rotation’s direction changes.

However, when I unplug the board from my computer, the code does not present the same behaviour. The motor only changes its direction if I push and hold the button (and when I release it the direction changes again).

I’m using a L298n H-bridge to control de stepper motor, powered with a 12v battery. I’ve tried both using the 5v output from the H-bridge as well a 9v battery to power the arduino.

My code is as follows:

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0;  // number of steps the motor has taken
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
    // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:

void loop() {
  // read the sensor value:
  int sensorReading = analogRead(A0);
  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
    // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
    // Delay a little bit to avoid bouncing
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;

// turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(ledPin, HIGH);

    if (motorSpeed > 0) {
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution / 100);
  } else {
        // set the motor speed:
        if (motorSpeed > 0) {
    // step 1/100 of a revolution:
    myStepper.step(-stepsPerRevolution / 100);
    digitalWrite(ledPin, LOW);

Do not cross-post. Other thread removed.

as well a 9v battery to power the arduino.

Not one of those square things that goes in the smoke detector? Those can't provide much current. The voltage sags really quick. They're not really very good for projects with motors or anything that needs much current.

Grab a handful of AA batteries and try that instead.