Button problems

Hi :

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

The pull down resistor needs to be wire to the other side of the switch, from the blue wire to ground.

Lefty

The button in the picture is correct, the code is not. What you need is a latch.

boolean latch = false;
//Read the button
if(buttonState == HIGH) {
latch = !latch;

latch ? true (WIM = 0 : WIM = 1);
}

  if (WIM = 0){

Assigning a value to a variable in an if statement is rarely a good idea. == needs to be used here.

Which pin is the button connected to ?
The picture shows pin 2, the code says pin 10

I suspect you need to activate the internal pullup resistor as well

digitalWrite(buttonPin, HIGH);

in setup

And make sure you have the button the right way round- if it's 90 degrees out, it won't be switching the right wires. See pic attached.....

switch2.jpg

What I gave is a basic, simplified latch code. It's not meant to be the final solution, just a kick in a new direction.

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.

Edit: where did the other posts go. o_O