Using Reed switches to control LEDS

Hi there,

Im helping on a project using reed switches to alert a digger operator if they are slewing there machine into a no slew zone, using a UNO, 2 reed switches and some LEDS.

I have been trying to get up to speed with learning this, but my efforts in Tinkercad to build this system has not been successful.

There will be 3 LEDs, Green, Amber and Red to represent different zones of the slew.

So on startup I would like each LED to light up briefly in Sequence (Green, Amber, Red)
Then the system to check if either ReedSens1 or ReedSens2 are currently triggered, If not, light up Green LED solid, to show the system is ready.

If machine is slewed past ReedSens1, then Green LED needs to turn off and Amber LED needs to illuminate with a slow flash, 2milli ON, 2 milli OFF. If machines is slewed back and ReedSens1 is triggered again, Amber LED to turn off and Green to illuminate.

If machine is slewed and ReedSens2 is triggered, Amber LED turns off and Red LED starts flashing fast 1 milli ON, 1 milli OFF. This also needs to send a trigger to a relay, which will activate the deadmans switch.

Once ReedSens2 has been triggered, the machine will be inoperable as deadmans switch has been opened. We then will have a momentary switch, which will bypass the deadmans switch and restore operation, allowing operator to slew back into safe zone. This will trigger the ReedSens2, and need to turn the Red LED off and start Amber LED flashing again.

Any help would be great as im a little bit out of my depth.

Thanks
Ned

Here is the link to my tinkercad project Login | Tinkercad

Hello
please post you draft sketch to see how we can help.

If you want help on the forum, post your code and schematic on the forum. Speaking for myself, I do not like jumping through hoops just to see your code and schematic. The easier that you make it for helpers the more help you will get.

Yes very sorry. I hadnt made any headway. But I went back and tried again and made a little headway with the below code.

// set pin numbers:
const int ReedSens1 = 2;     // the number of the pushbutton pin
const int ReedSens2 = 3;     // the number of the pushbutton pin
const int ledPinGr =  13;      // the number of the LED pin
const int ledPinAm =  12;      // the number of the LED pin
const int ledPinRe =  11;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPinGr, OUTPUT);
   // initialize the LED pin as an output:
  pinMode(ledPinAm, OUTPUT);
   // initialize the LED pin as an output:
  pinMode(ledPinRe, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(ReedSens1, INPUT);
    // initialize the pushbutton pin as an input:
  pinMode(ReedSens2, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(ReedSens1);
  Serial.println(buttonState);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is LOW:
  if (buttonState == LOW) {
    // turn LED on:
    digitalWrite(ledPinGr, HIGH);
    digitalWrite(ledPinAm, LOW);
    digitalWrite(ledPinRe, LOW);
  } else {
        if (buttonState == HIGH);
      digitalWrite(ledPinGr, LOW);
    digitalWrite(ledPinAm, HIGH);
    delay(200);
    digitalWrite(ledPinAm, LOW);
    delay(200);
       
  } 
}

This has functioned, turning the Green LED on with no button input. Then, when ReedSens1 is pressed, Green turns off and Amber starts flashing. But now I am stuck on how to proceed with the rest.

The way that the switches are wired they will read LOW when not pressed and HIGH when pressed. If you want active low switches (low when pressed), disconnect Vcc and the pull down resistor , connect one side of the switch to ground and the diagonally opposite pin to an input pin with its pinMode set to INPUT_PULLUP.

Im trying to replicate a NO reed switch with a press button switch, as Tinkercad doesnt have a reedswitch in its components list.

Hello,
to find a good kick off to the semi complex task I would like to give the following hint:
My standard recipe:
As a basis, study the IPO model and then take a bag of data structures, timers and a small finite state machine and stir everything together to the desired functionality. And don't forget the seasoning.

if (buttonState == LOW) {
// turn LED on:
digitalWrite(ledPinGr, HIGH);
digitalWrite(ledPinAm, LOW);
digitalWrite(ledPinRe, LOW);
} else {
if (buttonState == HIGH);
digitalWrite(ledPinGr, LOW);
digitalWrite(ledPinAm, HIGH);
delay(200);
digitalWrite(ledPinAm, LOW);
delay(200);

It makes no sense to test a digital input for LOW and then test again if it is HIGH. It can only be one of the two, making the second if unnecessary.

Your switches are probably wired incorrectly as three connections are shown, a switch only needs two. The easiest way to deal with a NO switch is to use the internal pullup resistor when declaring the pin as an input like this:

pinMode(reedSens1,INPUT_PULLUP);

Then wire one side of the switch to the input pin, the other side to GND. The input will be LOW when the switch is active. Test it with a jumper wire and serial prints. With those switches, always connect to two pins on opposite corners. Otherwise, the switch will always appear always closed or always open, depending upon how you have it oriented.

So I took an exisiting project from Tinkercad and then made some changes., Your suggestions are far more logical. I have made those changes.

// set pin numbers:
const int ReedSens1 = 2;     // the number of the pushbutton pin
const int ReedSens2 = 3;     // the number of the pushbutton pin
const int ledPinGr =  13;      // the number of the LED pin
const int ledPinAm =  12;      // the number of the LED pin
const int ledPinRe =  11;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPinGr, OUTPUT);
   // initialize the LED pin as an output:
  pinMode(ledPinAm, OUTPUT);
   // initialize the LED pin as an output:
  pinMode(ledPinRe, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(ReedSens1, INPUT_PULLUP);
    // initialize the pushbutton pin as an input:
  pinMode(ReedSens2, INPUT_PULLUP);
}

void loop() {
  
  // read the state of the ReedSens1 value:
  buttonState = digitalRead(ReedSens1);
  Serial.println(buttonState);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is LOW.
  if (buttonState == HIGH) {
      // turn LED on:
    digitalWrite(ledPinGr, HIGH);
    digitalWrite(ledPinAm, LOW);
    digitalWrite(ledPinRe, LOW);
  } else {
    digitalWrite(ledPinGr, LOW);
    digitalWrite(ledPinRe, LOW);
    digitalWrite(ledPinAm, HIGH);
    delay(500);
    digitalWrite(ledPinAm, LOW);
    delay(500);
        } 
}

Im now struggling how to keep ledPinAm flashing until ReedSens2 is HIGH.

Your code is not even reading ReedSens2!

Im fully aware of that. I dont understand enough to know how to incorporate a query which checks both sensor states. I would really appreciate some guidance on how that would look.

You already understand how to use digitalRead() to read the state of a switch/button. You already know how to use if...else to test that state and take different actions depending on it. So you already know what you need to know, unless I am missing something?

Also, unless I have misunderstood, you don't need to check both states, at least not together, do you? If ReedSens2 is LOW, then the state of ReadState1 does not matter, correct?

So you need to check ReadSens1 first, take necessary actions, otherwise check ReadSens2, take necessary actions, otherwise...

Yes that would be correct. As there is only 1 magnet so only one Reed Sensor can be trigger at once.

So I cant figure out how to keep the ledPinAm flashing after ReedSens1 has been triggered, until ReedSens2 is triggered.

I think my issues is starting with the method I am using buttonstate. It feels like its written for only one Input?

It seems to :slight_smile:

Ok, so I think I have fixed that. Now I need to figure out how to 'latch" ledPinAm until ReedSens2

// set pin numbers:
const int ReedSens1 = 2;     // the number of the pushbutton pin
const int ReedSens2 = 3;     // the number of the pushbutton pin
const int ledPinGr =  13;      // the number of the LED pin
const int ledPinAm =  12;      // the number of the LED pin
const int ledPinRe =  11;      // the number of the LED pin

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;         // variable for reading the pushbutton status


void setup() {
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPinGr, OUTPUT);
   // initialize the LED pin as an output:
  pinMode(ledPinAm, OUTPUT);
   // initialize the LED pin as an output:
  pinMode(ledPinRe, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(ReedSens1, INPUT_PULLUP);
    // initialize the pushbutton pin as an input:
  pinMode(ReedSens2, INPUT_PULLUP);
}

void loop() {
  
  // read the state of the ReedSens1 value:
  buttonState1 = digitalRead(ReedSens1);
  Serial.println(buttonState1);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is LOW.
  if (buttonState1 == HIGH) {
      // turn LED on:
    digitalWrite(ledPinGr, HIGH);
    digitalWrite(ledPinAm, LOW);
    digitalWrite(ledPinRe, LOW);
  } else {
    digitalWrite(ledPinGr, LOW);
    digitalWrite(ledPinRe, LOW);
    digitalWrite(ledPinAm, HIGH);
    delay(500);
    digitalWrite(ledPinAm, LOW);
    delay(500);
    
        } 
}

please post your sketch in code tags </>

Hi @nedmo

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
https://forum.arduino.cc/index.php?topic=712198.0
Then look down to "code problems" about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.... :grinning: :+1: :coffee: :australia:

Fixed. Apologies.

1 Like

Hi,
I have been involved in design and implementation of such a system.
Be careful with just stopping the slew, if it is hydraulics and the operator is slewing at full speed, an abrupt stop can be damaging to hardware and any load being carried could become a missile.

The system, if it is going to disable the hydraulic slew, it needs to control the rate of slew as the digger APPROACHES the limit.
This involves some major extra hydraulic control hardware and a better method of detecting where the digger slew position is at ALL times.

The system I worked on used two inductive sensors looking at the turntable teeth and configured as a quadrature encoder, an extra sensor was included to detect a reference point.

Apon start up, the slew rate was restricted to an unworkable crawl, this was to make the operator slew to find the reference position.
Once the position was detected, the operator had full control, except when approaching slew limits.
Then the hydraulics would be throttled back until fully off at the limit, reversing the slew instantly restored full control.
A series of lights/LEDs would indicate where in the slew limits the digger was.

Quite a lot of human and machine safety involved.

Tom... :grinning: :+1: :coffee: :australia:

1 Like