Program codes for Reed Switch

Hello
I have a reed magnet switch and Arduino nano and red and green led

How do I program it for it when it detect the magnet it turns on the green LED for 5 Minutes,

And turn on the Red LED for 4 minutes

*GREEN LED ONLY TURNS ON WHEN MAGNET IS Detected

*RED LED GOES OFF WHEN MAGNET IS DETECTED

This is a code

Const byte reedSensor = 3
Const byte led = 2

Void setup
PinMode (reed sensor, INPUT);
PinMode(led, OUTPUT);

Void Loop
digitalWrite (led, digitalReed(reedSensor)

Before asking questions:


  • Questioners know what their hardware and software looks like but the volunteers here do not; you need to fully explain what’s not working, and how it is supposed to work.
  • Please read all the posting guidelines before asking your questions; follow these guidelines in your post.

Hardware

  • As always, show us a good schematic of your proposed circuit. Hand drawn schematics are acceptable.
  • Show us good images of your ‘actual’ wiring.
  • Give WEB links to your major components.

Software

  • For readability, place { and } on separate lines by themselves, place each line of code on a separate line.
  • In the Arduino IDE, use Ctrl T or CMD T to format your code, then copy the complete sketch.
  • Use the < CODE / > icon from the ‘posting menu’ to attach your copied sketch.


  • When you follow the posting guidelines, volunteers can be more effective.

  • More volunteers will help if you properly post your questions. (77)

There is no difference between programming for reed switches and any other switch. Don't you find examples of code using switches in the Arduino IDE, program development system?

Sounds like homework.
Give it your best shot, and tell us what didn't work.
Then we might be able to help you.
Leo..

sorry i have code only for GREEN led

I can post the full code, but like to see OP's effort first.
Teach a man to fish...

Hint:

  1. When the reed switch is activated,
    mark the time, turn the green LED on and the red LED off.
  2. turn the red LED on after 4 minutes.
  3. turn the green LED off after 5 minutes.

Leo..

Yes correct

Anything to see this working

The forum is not a free code writing service.
Volunteers will help you when you put a little effort in yourself.
Let's see your code.
Leo..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.