Toggle Switch pull-up problem

I have this toggle switch in hands:

I need to make a simple switch so that a led switch states (on <-> off) when I
change the position of the switch.

#define LED_PIN 5
#define TOOGLE_SWITCH_PIN 4

int readingSwitch;
int previousSwitch = LOW;
boolean on = false;

void setup()
{
    pinMode(TOOGLE_SWITCH_PIN, INPUT);
    
    pinMode(LED_PIN, OUTPUT);
    digitalWrite(LED_PIN, LOW);
}

void loop()
{
   if (wasSwitchToggled()) {
     switchRelayState();
   }
}

void switchLedState() {
    if (on) {
      digitalWrite(LED_PIN, LOW);
      on = false;
    } else {
      digitalWrite(LED_PIN, HIGH);
      on = true;
    }
}

boolean wasSwitchToggled() {
  readingSwitch = digitalRead(TOOGLE_SWITCH_PIN);
  boolean toggled = false;
  
  if (readingSwitch != previousSwitch) {
    toggled = true;
  }

previousSwitch = readingSwitch;
return toggled;
}

I am connecting the switch on the arduino like this:

When the switch is closed, the led maintain its state.
When the switch is open the led starts changing states reaaaly fast.

So I'm not really sure of how I should connect the switch on the arduino,
or if I'm connecting the pullup resistor correctly, or if I'm programming it incorrectly.

(I have already tried connecting the resistor between the switch and pin4 -> led still blinks like crazy)

Someone has a suggestion on what I'm doing wrong?
Also, I think this is a General Eletronics question, not a programming one. Not sure though

Either way, thanks in advance

wasButtonPressed() and switchRelayState() are both undefined.

The sketch shows the switch being read via Pin 13 but your diagram shows it connected to Pin 4.

Try fixing those things first.

The resistor needs to pull-up - your circuit shows the resistor in-line with the switch - it's not doing much there, the input
will "float" when the switch is open, and it will pick up noise from all the nearby circuitry (and your fingers).

You connect the switch between input pin and ground, the resistor between input pin and +5V. When the switch is open
the resistor pulls the pin to 5V, when the switch is closed it pulls the pin down to 0V.

A physical resistor is not actually needed as the Arduino pins have a pull-up mode:

// in newer Arduino versions:
  pinMode (pin, INPUT_PULLUP) ;

// equivalent to
  pinMode (pin, INPUT) ;
  digitalWrite (pin, HIGH) ;  // enables internal pull-up when pin is an input

Switch toggle code that should not require a resistor. Put switch between pin 5 and ground.

/zoomkat servo-LED button toggle test 11-12-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(13, OUTPUT); //LED on pin 13
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      digitalWrite(13, HIGH);   // set the LED on
      servo.write(160);
      toggle = !toggle;
    }
    else
    {
      digitalWrite(13, LOW);    // set the LED off
      servo.write(20);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

@johnwasser
yeah, you're right. I was messing up some parts.
Though I was using the right pins when testing

@MarkT
thanks mark, I think I got it now!
I didn't think I could do that

  pinMode (pin, INPUT) ;
  digitalWrite (pin, HIGH) ;  // enables internal pull-up when pin is an input

because I was assigning pin as INPUT, but now that I know that it rocks!

gcats:
@MarkT
thanks mark, I think I got it now!
I didn't think I could do that

  pinMode (pin, INPUT) ;

digitalWrite (pin, HIGH) ;  // enables internal pull-up when pin is an input



because I was assigning pin as INPUT, but now that I know that it rocks!

Its a special feature of the AVR microcontroller chips - not something you'd expect to work unless
you've read the datasheet section 13...