My goal is to make a logic controller for a cassette tape mechanism, right now I'm simply trying to get the mechanism to engage and disengage the pinchroller arm which is motorized, spin the reels/capstan to advance the tape for playback; and stop the process by stopping the capstan motor, and retracting the pinchroller arm.
Currently this code doesn't respond to any of the button inputs, and I'm unsure as to why that is. I have another program that allows manual control of the motors, and that works fine, this however does not.
Can someone point out/correct what is the mistake? Because I can't seem to find what is wrong. Thank you in advance!
Here is my current full code:
const int motor1Pin = 5; //If motor1 is HIGH and motor2 is LOW, the motor will spin one way, if motor1 is LOW and motor2 HIGH then the motor will spin in the opposite direction.
const int motor2Pin = 4;
const int motor3Pin = 2;
const int motor4Pin = 1;
const int enablePin = 6; //turns pinch roller arm motor on or off
const int enablePin2 = 3; //turns capstan motor on or off
const int limitswitch1Pin = 7; // end of top travel limit switch
const int limitswitch2Pin = 9; // end of bottom travel limit switch
const int cyclePin = 8; //switch that cycles the pinch roller arm depending on limit switches
const int reversePin = 10;
const int forwardPin = 11;
const int stopPin = 12;
int state;
int buttonState;
int limit1State;
int lastlimit1State;
int limit2State;
int lastlimit2State;
int reverseState;
int forwardState;
int stopState;
enum State_enum {STOP,PLAY};
bool enableState = false;
bool enable2State = false;
void setup() {
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(motor3Pin, OUTPUT);
pinMode(motor4Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(enablePin2, OUTPUT);
pinMode(reversePin, INPUT_PULLUP);
pinMode(forwardPin, INPUT_PULLUP);
pinMode(stopPin, INPUT_PULLUP);
pinMode(limitswitch1Pin, INPUT_PULLUP);
pinMode(limitswitch2Pin, INPUT_PULLUP);
pinMode(cyclePin, INPUT_PULLUP);
pinMode(A0, INPUT);
buttonState = digitalRead(cyclePin);
limit1State = digitalRead(limitswitch1Pin);
limit2State = digitalRead(limitswitch2Pin);
reverseState = digitalRead(reversePin);
forwardState = digitalRead(forwardPin);
stopState = digitalRead(stopPin);
}
void loop() {
if (buttonState == LOW) int state = PLAY;
if (stopState == LOW) int state = STOP;
switch (state) {
case PLAY: //Plays the cassette
if (stopState == LOW) {
state = STOP;
}
if (limit1State == LOW) { //running the pinchroller arm clockwise from the top to bottom
digitalWrite(motor1Pin, HIGH); // set leg 1 of the pinchroller motor H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the pinchrooler motor H-bridge low
analogWrite(enablePin, 176); //set pinchroller motor speed
} else if (limit2State == LOW) { //pinchroller arm is fully down (at bottom)
digitalWrite(enablePin, LOW); //disable pinch roller motor
digitalWrite(motor3Pin, LOW); // set leg 3 of the capstan motor H-bridge low
digitalWrite(motor4Pin, HIGH); // set leg 4 of the capstan motor H-bridge high
analogWrite(enablePin2, 200); // set capstan motor speed
digitalWrite(enable2State, true);
}
break;
case STOP: //Stops the cassette
if (enable2State == true && limit2State == LOW) {
digitalWrite(enablePin2, LOW);
digitalWrite(enable2State, false);
} else if (enable2State == false && limit2State == LOW) {
//running the pinchroller arm counter-clockwise from the bottom to top
digitalWrite(motor1Pin, LOW); // set leg 1 of the motor H-bridge high
digitalWrite(motor2Pin, HIGH); // set leg 2 of the motor H-bridge low
analogWrite(enablePin, 176); //set pincroller motor speed
if (limit1State == LOW) {
digitalWrite(enablePin, LOW);
}
}
break;
}
}