Go Down

Topic: digital read/write not working within switch/case serial loop (Read 1 time) previous topic - next topic

PaulS

Quote
Any idea how I can implement reading the pin state and changing the mode whilst also monitoring the serial input?

Do them independently. Read the switch state. Then, read the serial data, if any.

flotilla

#6
Dec 10, 2012, 06:49 pm Last Edit: Dec 10, 2012, 06:52 pm by flotilla Reason: 1
Thanks, Can I change the mode within another IF statement from outside the IF statement? I need to be able to interupt {mode = 6} with
the switch, making the program stop? I thought that is what I had done already, unsuccesfully

PaulS

Quote
Can I change the mode within another IF statement from outside the IF statement?

You keep using the definitive article - the IF statement. There is more than one if statement. You need to clarify what if statement you are referring to.

Most likely, the answer is yes.

Quote
I need to be able to interupt {mode = 6} with
the switch, making the program stop?

You don't want to interrupt mode 6. That implies letting it resume after the interruption. You want to terminate mode 6. Anytime you are in mode 6, you can read the switch state, and change to a different mode if the state is to your liking.

flotilla

Quote
Anytime you are in mode 6, you can read the switch state, and change to a different mode if the state is to your liking.


I have tried adding the following to within the (mode = 6) code:

Code: [Select]
case 6:  // Run Motor until StopPin pressed
      digitalWrite (FeedPin, HIGH);
      buttonState = digitalRead(StopPin);
      if (buttonState == HIGH) { mode = 7;}
      break;
     
    case 7:  // Reset all variables and go to wait mode
      digitalWrite (TestPin,HIGH);
      digitalWrite (FeedPin, LOW);
      key = 0;
      val = 0;
      mode = 0;
      break;


    }
  }
}


The TestPin does not go HIGH and the FeedPin does not go LOW when the StopPin switch is pressed whilst mode = 6 is running.

I am trying to find out where in my code I need to monitor the StopPin to allow me to interrupt (sorry, change mode) whilst mode
6 is running. It currently does not. Can anyone suggest where I need to monitor the switch to allow the mode to be changed







PaulS

Quote
I have tried adding the following to within the (mode = 6) code:

Which looks correct.

Quote
The TestPin does not go HIGH and the FeedPin does not go LOW when the StopPin switch is pressed whilst mode = 6 is running.

You need to add a Serial.print() statement to loop, to print out the value in mode.

Go Up