0

I am trying to integrate solenoid lock, reed switch in my Arduino Code. The flow that I am trying to achieve is - first, the solenoid lock is set to LOW and then the program checks if reed switch is High or Low. Only if its High I want to proceed further. However, even when the reed switch is HIGH the program is not executing further. Here is my code below:

    #include<HX711_ADC.h>
    #include<math.h>

    //pins for lock
    int solenoidPin = 7;

    //pins for reed switch
    int reedSwitch = 2;
    bool reedSwitchStatus;

    //pin for led
    int led_pin= 9;

    void setup() 
    {
      Serial.begin(38400); 
      pinMode(solenoidPin, OUTPUT);          
      pinMode(reedSwitch, INPUT);

      doorUnlock();
    }

    void loop() 
      {}

    void doorUnlock()
    {
      digitalWrite(solenoidPin, LOW);
      delay(5000); 
      checkReedSwitchStatus();
    }

    void checkReedSwitchStatus()
    {
      reedSwitchStatus = digitalRead(reedSwitch);

      if(reedSwitchStatus == 1)
      {
        Serial.println("Beginning Transaction");
        beginTransaction();
        }
      if (reedSwitchStatus == 0)
        {
          Serial.println("Ending trasaction");
          endTransaction();
          }
      }
      void beginTransaction()
      {
      digitalWrite(led_pin, HIGH);
      delay(500);
      digitalWrite(led_pin, LOW);
      delay(500);
      }

My issue is the beginTransaction function is never executed. Serial monitor continuously prints 'Beginning Transaction'. Attached the screenshot of the serial monitor. Please suggest me what is the error in my code. Thanks in advance! enter image description here

  • How do you know, that the beginTransaction() function is not executed. Please include the full code. Also: Do you have an external pullup or pulldown resistor for the reed switch? – chrisl Oct 7 at 7:56
  • Hi Chrisl, I have used an external pull up resistor for the reed switch. I have also edited my question to add the beginTransaction() function in the code. The function is not getting executed because the LED does not blink. – Prashant Oct 7 at 8:08
0

The problem is, that you don't initiate the LED's pin to an output. On reset the pin is configured as input. Doing a digitalWrite(led_pin, HIGH) in this state just enables the internal pullup resistor. If you measure the voltage without the LED connected, it will be 5V, but since the pin only provides a very small amount of current through the pullup resistor, the LED cannot light up when connected.

You need to configure the LED's pin as an output through

pinMode(led_pin, OUTPUT);

in your setup() function.

So actually the beginTransaction() function was correctly executed. You just couldn't see it.

  • Thanks!! That corrected the issue. – Prashant Oct 7 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.