Combining sketches

Can anyone take a look at this attached sketch and tell me what the problem might be? My motor is working but I am unable to get my button to work. Thank you!

sketch_apr09a.ino (1.47 KB)

The LED should light only when the button is held down. How is the button wired? Is there a pulldown resistor on the button input (pin 2)?

Here is how code should be posted in code tags. It makes easy for everyone to see your code. See this How to use the forum post. #7.

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status


#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);


void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
   // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
buttonLoopCode();
stepperLoopCode();
}

void buttonLoopCode(){
   buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

void stepperLoopCode(){
   // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

Yes, a schematic would be good. As ground fungus said, the button has to be held down to see the led light if it is a momentary switch, non latching switch ?

(deleted)

thanks, all for your feedback. the button is non-latching so it pops back up every time it is pressed. the LED should illuminate when the button is pressed. I attached my schematic so that you can see what I am working with. Thank you!

Here is my diagram and code.

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status


#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);


void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
   // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
buttonLoopCode();
stepperLoopCode();
}

void buttonLoopCode(){
   buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

void stepperLoopCode(){
   // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

Does your project do what you want?

If you want the LED to light when you push the button and stay lit until you press the button again, look into the state change detection example. It will show how to detect when the button becomes pressed (high to low transition) rather than when it is pressed (a low level). And you can ignore the low to high transition and/or high level.

// initialize the pushbutton pin as an input, enable internal pullup
  pinMode(buttonPin, INPUT_PULLUP);

You can eliminate the pullup resistor on the switch by using the internal pullup.

const int buttonPin = 2;

You can use the byte data type there and save a byte of, limited, SRAM. Seems like a small thing, but as you go on with programming you will see the value.

const byte buttonPin = 2;

I want the light to be illuminated when and only when the button is depressed. when let go, the light should turn off. this is not working right now with the current schematic and code.

You said what it should do, but neglected to say what it does do which would be helpful.

if (buttonState == HIGH) {  
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }

That code says that if the button is not pressed, turn the LED on and when the button is pressed turn the LED off. The wiring of the button makes the input HIGH when not pressed and LOW when the button is pressed.
You need to swap the logic in the if statements.

The light seems to be not associated with the button state at all. it flickers on and off, whether the button is pressed or not.

All that I can suggest is to carefully check your wiring. The symptom suggests that the button input is floating (not connected). There will be a delayed response due to the delay()s in the stepper loop, but the LED should be on when the button is not pressed and off when pressed (using the code in reply #5).

Thank you, groundfungus. I think everything was working, but the computer was just running through the code slowly than I had expected which made the light look like it was not syncing with the button input.