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