I would like to use a button to control two programs (PING and GPS). The entire program will start using the PING program. when the button is pushed it will go to the GPS program. if it is pushed again, the PING program will run... etc. The following code compiles, but the output on the serial monitor does not change when the button is pushed. I am using this schematic.
Here is the code:
const int buttonPin = 10;
int buttonState = 0;
int WIM = 0;
void setup()
{
Serial.begin(4800);
pinMode(buttonPin, INPUT);
}
void loop()
{
if (WIM = 0){
Serial.print("running WIM =0");
Serial.println();
//run PING program
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
WIM = 1;
}
}
else {
//run GPS program
Serial.print("running WIM =1");
Serial.println();
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
WIM = 0;
}
}
}
Any help troubleshooting the code would be appreciated
Your button needs to be pulled LOW, meaning the resistor should be to ground and the probe should be connect to the other end of the resistor, along with the button.
Or simply change the line of code from button == HIGH to LOW.