I have a project that I have been working on for a while. The program I need to write takes four digital inputs and based on which one is low, It assigns a value of 0 through 4 to a variable labeled pinState. based on whether this value is greater than the last value or lesser than the last value it determines which order the inputs are going low to determine the direction a motor is spinning.

I can't figure out how to get the value previousState to keep the value that pinState was previously. I need previousState to always contain the value that pinState was one read ago. Can anyone help me figure out how to do this?

Here is the code that I have been trying to debug:

int pinState = 0;

int previousState = 0;

int motorDirection = 0;

int motorPinA = digitalRead(12);

int motorPinB = digitalRead(11);

int motorPinC = digitalRead(10);

int motorPinD = digitalRead(9);void

setup() {pinMode (12, INPUT);

pinMode (11, INPUT);

pinMode (10, INPUT);

pinMode (9, INPUT);

Serial.begin(9600);

}

voidloop() {if (motorPinA == LOW) {

pinState = 1;

}

else if (motorPinB == LOW) {

pinState = 2;

}

else if (motorPinC == LOW) {

pinState = 3;

}

else if (motorPinD == LOW) {

pinState = 4;

}

if ((motorPinA == HIGH) && (motorPinB == HIGH) && (motorPinC == HIGH) && (motorPinD == HIGH)) {

pinState = 0;

}

if ((pinState == 1) && (previousState == 4)){

motorDirection = 2;

}

if ((pinState == 2) && (previousState == 1)){

motorDirection = 2;

}

if ((pinState == 3) && (previousState == 2)){

motorDirection = 2;

}

if ((pinState == 4) && (previousState == 3)){

motorDirection = 2;

}

if ((pinState == 4) && (previousState == 4) && (motorDirection == 2)){

motorDirection == 2;

}

if ((pinState == 3) && (previousState == 3) && (motorDirection == 2)){

motorDirection == 2;

}

if ((pinState == 2) && (previousState == 2) && (motorDirection == 2)){

motorDirection == 2;

}

if ((pinState == 1) && (previousState == 1) && (motorDirection == 2)){

motorDirection == 2;

}

if ((pinState == 4) && (previousState == 1)){

motorDirection = 4;

}

if ((pinState == 3) && (previousState == 4)){

motorDirection = 4;

}

if ((pinState == 2) && (previousState == 3)){

motorDirection = 4;

}

if ((pinState == 1) && (previousState == 2)){

motorDirection = 4;

}

if ((pinState == 4) && (previousState == 4) && (motorDirection == 4)){

motorDirection == 4;

}

if ((pinState == 3) && (previousState == 3) && (motorDirection == 4)){

motorDirection == 4;

}

if ((pinState == 2) && (previousState == 2) &&( motorDirection == 4)){

motorDirection == 4;

}

if ((pinState == 1) && (previousState == 1) && (motorDirection == 4)){

motorDirection == 4;

}

if ((pinState == 0) && (previousState == 0)){

motorDirection = 0;

}

if ((pinState == 0) && (previousState > 0)){

motorDirection = 0;

}

previousState = pinState;

Serial.println(motorDirection , DEC);

}

I'm sorry the code isn't commented, that is why I tried to give a general flow of the way the code is moving at the beginning of this post. Any advice is very greatly appreciated. Thank you in advanced.