Toggle switch help

Hi guys

I just added a switch to my arduino but it doesnt matter if the switch is on or off it seems that the code is maybe getting stuck in my while loop and not carrying on with the rest of the loop.

Could someone please point out what im doing wrong as ive checked the switch with a multimeter and that is correct so i think its my code.

Another question how important is a resistor on the switch, I know its for safety as to not accidentally burn your board but anything else?

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
#define ultraTRIG 5
char *msg1 = "C",*msg2 = "E",*msg3 = "S";
int timer = 200,timer2=150;
int Button=7;
int buttonState;

void setup()
{
  pinMode(ultraTRIG,OUTPUT);
  vw_set_ptt_inverted(true);
  vw_setup(2000);
  vw_set_tx_pin(3);
  pinMode(Button,INPUT);
}

void loop()
{
  buttonState = digitalRead(Button);
  
  if (buttonState == HIGH){    //stop follow button 
  digitalWrite(13, true);vw_send((uint8_t *)msg3, strlen(msg3));vw_wait_tx();digitalWrite(13, false);
  while (buttonState == HIGH){ 
    //must add remote controll here
  if (buttonState == LOW){
    break;
  } 
  }
  } 
  digitalWrite(13, true);vw_send((uint8_t *)msg1, strlen(msg1));vw_wait_tx();digitalWrite(13, false);
  digitalWrite(ultraTRIG,LOW);
  delayMicroseconds(timer);
  digitalWrite(ultraTRIG,HIGH);delayMicroseconds(10);digitalWrite(ultraTRIG,LOW);
  delay(timer2);
  digitalWrite(13, true);vw_send((uint8_t *)msg2, strlen(msg2));vw_wait_tx();digitalWrite(13, false);
  digitalWrite(ultraTRIG,LOW);
  delayMicroseconds(timer);
  digitalWrite(ultraTRIG,HIGH);delayMicroseconds(10);digitalWrite(ultraTRIG,LOW);
  delay(200);//delay before next loop
  
}

Thanks Mathys

You're not reading the button anymore inside the while loop ...

How is it ever not going to be high once it's in there?

haha ok thanks got that :-[

Undermentioned:
Another question how important is a resistor on the switch, I know its for safety as to not accidentally burn your board but anything else?

A resistor on a switch is not there for safety or to prevent burning your board.
It is there to give the input a defined level when the switch is "open".
Without resistor, the pin is "floating", and could be any digital level.

Easier than using a resistor is using the internal pullup resistor (the switch between pin and ground).
See Arduino reference about adding PULLUP to your input pin setup.
Leo..

Wawa:
A resistor on a switch is not there for safety or to prevent burning your board.
It is there to give the input a defined level when the switch is "open".
Without resistor, the pin is "floating", and could be any digital level.

Hmmmm, you might want to think that through a bit more Wawa. A wire would pull up without a resistor.....

Uhhhh, a wire has resistance, so it must be a resistor.

Undermentioned:
haha ok thanks got that :-[

Sweet, don't sweat it though - sometimes basic stuff like that hides in plain sight.

Bet you won't do it again for a long time now! :wink:

Wawa:
Uhhhh, a wire has resistance, so it must be a resistor.

That's true, but I think you know that's not what I meant.

Pullup resistor from input pin to +5volt. External, or internal with code.
Switch from input pin to ground.
Scratching my head here. Don't see your point.
Leo..

Wawa:
Scratching my head here. Don't see your point.

Don't lose any sleep over it.....

I might.
I know OP is using a toggle switch, but that could have been wired as single pole.
Maybe I’m just thick.
Leo…

Hehe, no my point is that you could pull up with a 0R resistor (ie a bit of wire). But when the switch is closed so the pin's other side is grounded then there's a dead short from +ve to 0V through the 0R and the switch.

So the "pull up resistor" is actually there to prevent that short, not actually do the pulling up which a wire could do.

You can, after all, wire an input pin to high or low with a wire and measure it with digitalRead(). The resistor just makes sure you don't do both at the same time...

My understanding of the function of a pull-up resistor is that it pulls the input pin to a logical 1.

For a cmos input, that resistor can be anything from zero/wire (or close to zero with a switch) to tens of megohms.

The upper limit being the leakage of the input protection diodes, the input fets, the circuit board, and the switch.

If you have no pull-up resistor, and the switch is wired between input pin and ground, and there is sufficient leakage to ground, you never will see a logic 1 on your input pin.
Leo…