Help Comparing Changes In Variables

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);
}
void loop() {

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.

I don't see anywhere that the motorPinA/B/C/D variables are updated.


Rob

The variables motorPin A/B/C/D are digital reads.

They just update next time the loop runs through.

Is this not what I should have it be doing?

They just update next time the loop runs through.

Not placed where they are they don't.

You have to include all the digitalRead() inside loop(), where they are the reads are only done once when the program starts. Add

motorPinA = digitalRead(12);
motorPinB = digitalRead(11);
motorPinC = digitalRead(10);
motorPinD = digitalRead(9);

to the top of the loop() function. You can remove them from the top of the program if you like.


Rob

So, I made all the changes that you suggested. This is the code I came up with:

int pinState = 0;
int previousState = 0;
int motorDirection = 0;

void setup() {

pinMode (12, INPUT);
pinMode (11, INPUT);
pinMode (10, INPUT);
pinMode (9, INPUT);
Serial.begin(9600);
}
void loop() {

int motorPinA = digitalRead(12);
int motorPinB = digitalRead(11);
int motorPinC = digitalRead(10);
int motorPinD = digitalRead(9);

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

But still all I get out through the serial monitor is a big fat zero. Do you see anything else I can try?

That logic looks pretty dodgy and overly repetitive.

From the context I guess that pinState is being used to hold a position with five possible values (0 .. 4). Is that right? If so, I'd suggest the name could be improved to make that clearer. Is your logic to work out pinState from the four inputs actually giving you sensible values?

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

This 8 lines of code doesn't make any sense to me. Next, you are assigning a motor direction in this sections (8 lines):

if ((pinState == 1) && (previousState == 4)){
  motorDirection = 2;
}

When your code is starting up previousState == 0, so condition is ALWAYS wrong, and motor directions never changed.
Just idea, not tested - of course:

if ((pinState == 1) && ((previousState == 4) ||  (previousState == 0))){
  motorDirection = 2;
}