Sensor won't work with buttons and stepper motor

Hello all,

I am trying to make a arduino powered vending machine using stepper motors, buttons and an optical switch. Unfortunately my program isn't letting the buttons control the stepper motors. Once the optical switch is activated it goes right to the stepper motor and completely bi passes the buttons. Can someone please tell me what is wrong with my code. Thanks

#include <Stepper.h>
#define STEPS_PER_MOTOR_REVOLUTION 45
#define STEPS_PER_OUTPUT_REVOLUTION 45 * 64
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 10, 12, 11, 13);

Stepper small_stepper2(STEPS_PER_MOTOR_REVOLUTION, 6, 8, 7, 9);

int Steps2Take; // double check after
int IR_LED_PIN = 4;
int IR_DETECTOR_PIN = 2;
int TestLED = 4;
int button1 = 1;
int button2 = 3;
int buttonstate1 =0;
int buttonstate2 = 0;
volatile byte state = LOW;
int var = LOW;
int var2 = LOW;
int IR_READER = 0;

void setup()
{

Serial.begin(9600);
pinMode(IR_LED_PIN, OUTPUT);
pinMode(TestLED, OUTPUT);
digitalWrite(IR_LED_PIN, HIGH); // Turn on Opto LED
int buttonstate1 = LOW;
int buttonstate2 = LOW;
}
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
buttonstate1 = digitalRead(button1);
buttonstate2 = digitalRead(button2);
IR_READER = digitalRead(IR_DETECTOR_PIN);
if (IR_READER == HIGH)
{
if (buttonstate1 == HIGH) //Beam is not Seen
{
small_stepper.setSpeed(600); // SLOWLY Show the 4 step sequence
Steps2Take = 2160; // Rotate CW
small_stepper.step(Steps2Take);
delay(10);
}

//else if (IR_READER == HIGH && buttonstate2 == HIGH){
//motor bullshit 2
else if (buttonstate2 == HIGH)
{
small_stepper2.setSpeed(600); // SLOWLY Show the 4 step sequence
Steps2Take = 2160; // Rotate CW
small_stepper2.step(Steps2Take);
delay(10);
}
}
// }

delay(1500);
}

First, don't use pin 1 for a button, it's the serial "TX" pin, second, how are your buttons wired?

Thank you. Switching the button to another pin did work! The only problem is that the motors can only be activated when the buttons are pressed while the optical switch is being activated.

Once again, how are the buttons wired? If they are wired between the input pin and VCC you need a pulldown resistor (about 10k) from the input pin to ground, without it the pin is "floating" and might read anything. Best way is wire the button between pin and GND, then in setup() put "pinMode(button1,INPUT_PULLUP)", the button will read LOW when pressed and HIGH when not.

int buttonstate1 =0;
int buttonstate2 = 0;
volatile byte state = LOW;
int var = LOW;
int var2 = LOW;
int IR_READER = 0;

void setup()   
{

  Serial.begin(9600);
  pinMode(IR_LED_PIN, OUTPUT);
  pinMode(TestLED, OUTPUT);
  digitalWrite(IR_LED_PIN, HIGH); // Turn on Opto LED
  int buttonstate1 = LOW;
  int buttonstate2 = LOW;

It's a poor practice to have local variables with the same name as local variables. It's pointless to have local variables that go out of scope without being used.

Hi
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile: