I basically have two push button switches and a motor. When the program starts the motor turns a mechanism until the first push button switch is pressed, the motor then changes direction. When it reaches the next push button switch I want it to stop and change direction until the first push button switch is pressed.
Of course, I've been trying to get it work by trying so many different things that the original stuff that was working isn't anymore.
#include <Servo.h> // servo library
//Used IOs
// 2,3,4,5,6,7,8,9,10,11,12,13
Servo servo1; // servo
int led1 = 4; // This is the red light
int led2 = 5; // This is the yellow light
const int button3Pin = 6; // pushbutton 3 pin
const int button2Pin = 7; // pushbutton at the end of the drawer
const int button1Pin = 10; // pushbutton 1 pin
int intReverse;
int button1State, button2State, button3State;
void setup() {
Serial.begin(9600);
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
pinMode(13, OUTPUT); //Initiates Motor Channel B pin
pinMode(8, OUTPUT); //Initiates Brake Channel B pin
//seting up for the lights
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
//seting up for the buttons switches
pinMode(button1Pin, INPUT); //This is the switch on the back of the unit
pinMode(button2Pin, INPUT); //This is the switch on the front of the unit
pinMode(button3Pin, INPUT); //This is the switch on the side of the unit
servo1.attach(2); //This attaches the servo to IO 2
}
void loop() {
button1State = digitalRead(button1Pin);
//when it is LOW (off) this code will not run
//when it is HIGH (on) this code will run
if (button1State == HIGH){
MotorBaseBack(); //motor is high
/*if (intReverse==1){
MotorBaseBack2();
}
if (intReverse==2){
ForwardsA();
}*/
}
if (button1State == LOW){ //LOW is pressed and HIGH is not pressed
Back(); //the motor is low
}
} //end of loop
void Back(){
MotorBaseStop();
ForwardsA();
/* delay(5000);
MotorsOff();
delay(2000);
DispenseMotor();*/
intReverse=1;
}
void MotorBaseBack2(){
// while (button1State =! HIGH && (intReverse==2)){
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Engage the Brake for Channel A
analogWrite(3,175);
}
void MotorBaseBack(){
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Engage the Brake for Channel A
analogWrite(3, 175);
}
void ForwardsA(){
digitalWrite(12, LOW); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Engage the Brake for Channel A
analogWrite(3, 250);
}
void MotorBaseStop(){
delay(50);
digitalWrite(9, HIGH); //Engage the Brake for Channel A
}
void MotorsOff(){
digitalWrite(8, HIGH); //Engages the brake for motor A
digitalWrite(9, HIGH); //Engages the brake for motor B
}
void DispenseMotor(){
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Engage the Brake for Channel B
analogWrite(11, 150);
delay(1000); //This dispenses the pills for 3/4 second
digitalWrite(8, HIGH); //Engage the Brake for Channel B
}