Toggle magnetic hall sensor A3144

Hello everybody. I have a question about the hall sensor. For a school project I have to adjust a arduino project from a button toggle switch to an hall sensor toggle. They already gave me an example of the button code and a start of the project for a normal hall sensor. Now I have to modify the example below for a toggle switch. Also I have to modify the code., but I am a little bit stuck. I don't know where to start. The LED on pin 13 has to go on and off when pushing the button so I have to reverse the action of the hall sensor. But can I do that in the code when in the code there is also an action comparing the old and new button state? And what do I have to modify on my breadboard. At this time I do not need the complete answer but just a direction at what to do and where to start. Can anybody help me? I already searched the internet a couple of hours and can not find anything with the code I have been given to modufy.
I can not see where to insert the code so I am sorry I have put it down here:


/* Toggle-schakelaar
Met een toggle-functie kun je met dezelfde handeling een actuator
zowel in- als uitschakelen. Voorbeeld:
knop kort indrukken, LED aan, knop kort indrukken, LED uit etc.

Verbind de drukknopschakelaar met +5V en pin D2
Verbind een pull-down weerstand tussen D2 en GND
Gebruik de on-board LED L op poort D13 als LED

// Twee constanten vertegenwoordigen pinnen D2 en D13:
const int buttonPin = 2; // Pinnummer drukknopschakelaar (D12)
const int LedPin = 13; // Pinnummer LED (D13)

// Variabele vertegenwoordigt status van de drukknopschakelaar:
int buttonState = 0;

// Twee extra variabelen voor de toggle-functie
int teller = 0; // teller telt aantal knopindrukken
int buttonState_oud = 0; // vorige status van de drukknop

void setup() {
// Initialiseer digitale poort D13 als uitgang (OUTPUT):
pinMode(LedPin, OUTPUT);
// Initialiseer digital poort D2 als ingang (INPUT):
pinMode(buttonPin, INPUT);

void loop() {
// Lees de status van de drukknopschakelaar:
buttonState = digitalRead(buttonPin);

// vergelijk de status met de vorige status
if (buttonState != buttonState_oud) {
// als de status is veranderd, controleer dan HOE
if (buttonState == 1) {
// is de verandering van UIT naar AAN
// verhoog dan de teller met 1:
} else {
// is de verandering van AAN naar UIT
// Pauzeer na elke knopindruk om bouncing te elimineren
// pas de status aan voor de volgende loop
buttonState_oud = buttonState;

// schakel LED aan of uit op basis van teller
if (teller % 2 == 0) {
digitalWrite(LedPin, HIGH);
} else {
digitalWrite(LedPin, LOW);


It is more common to wire the switch from ground and an input set to pinMode INPUT_PULLUP using the internal pullup resistor. No external resistor required.

Then the code to toggle the built in LED is this:

// by C Goulding aka groundFungus

const byte  buttonPin = 7;    // the pin that the pushbutton is attached to
const byte ledPin = 13;       // the pin that the LED is attached to

boolean buttonState = 0;         // current state of the button
boolean lastButtonState = 0;     // previous state of the button

void setup()
   // initialize the button pin as a input with internal pullup enabled
   pinMode(buttonPin, INPUT_PULLUP);
   // initialize the LED as an output:
   pinMode(ledPin, OUTPUT);
   // initialize serial communication:

void loop()
   static unsigned long timer = 0;
   unsigned long interval = 50;  // check switch 20 times per second
   if (millis() - timer >= interval)
      timer = millis();
      // read the pushbutton input pin:
      buttonState = digitalRead(buttonPin);
      // compare the new buttonState to its previous state
      if (buttonState != lastButtonState)
         if (buttonState == LOW)
            // if the current state is LOW then the button
            // went from off to on:
            digitalWrite(ledPin, !digitalRead(ledPin)); // toggle the output
      // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;

This same code will work with a Hall effect switch (I know, I tested it). The internal pullup replaces the external pullup in your OP.

Read the how to use this forum-please read sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

How to post images so we don't have to download them and everyone can see them.
How to post an image.
Another page on posting images.

Thank you for the reply. I see that I only forgot something to mention. The task that I have been give is to use an external pull_up resistor and the hall sensor has to be reversed. The hall sensor normally switches off when the magnet comes closes but the task I have been given is to switch the hall sensor on whe the magnet comes to the sensor and goes of when removing the magnet.