Hallo i have a reed switch that when a door is closed the swich is closed and when the door is open the switch opens can someone pls help me with the code
I want that when the door is closed the serial monitor must print once door closed and when the door is open it must print once door opend and it must change a value called output door open=1 closed=0 and if the door is open for more than 6min it sould change a other value2 from 0 to 1 and if the door at any time closes the other value2 or stays 0 or changes from 1 to 0.
i tried ezButtons.h for it but got confused so now im asking here for some help
Int reedswitch = 12;
Int enableddisablebutton = 11;
Int led = 13;
Int value1 = 0;
Int value2 =0;
Bool Enable_Disable = false;
Void setup(){
//Code...
}
Void loop(){
//Code...
}
Have a look at the Example in the IDE called "StateChangeDetection".
That will get you to the stage where you can print once only on the serial monitor.
/*
State change detection (edge detection)
Often, you don't need to know the state of a digital input all the time, but
you just need to know when the input changes from one state to another.
For example, you want to know when a button goes from OFF to ON. This is called
state change detection, or edge detection.
This example shows how to detect when a button or button changes from off to on
and on to off.
The circuit:
- pushbutton attached to pin 2 from +5V
- 10 kilohm resistor attached to pin 2 from ground
- LED attached from pin 13 to ground through 220 ohm resistor (or use the
built-in LED on most Arduino boards)
created 27 Sep 2005
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection
*/
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
In your starter code, you have a capital letter at the start of each line, they should all be lower case.
Here is the first part done:
/*
reedswitch is connected between pin 12 and GND,
with pullup resistor enabled.
When door closed pin 12 is low.
When door open pin 12 is high.
derived from https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection
*/
int reedswitch = 12;
int enableddisablebutton = 11;
int led = 13;
int value1 = 0;
int value2 = 0;
bool Enable_Disable = false;
int reedswitchState = 0;
int lastReedswitchState = 0;
void setup() {
pinMode(reedswitch, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// read the reedswitch input pin:
reedswitchState = digitalRead(reedswitch);
// compare the reedswitchState to its previous state
if (reedswitchState != lastReedswitchState) {
// if the state has changed, print message.
if (reedswitchState == HIGH) {
// if the current state is HIGH then the reedswitch
// went from closed to open:
Serial.println("\nDoor Open");
value1 = 1;
Serial.print("value1 = ");
Serial.println(value1);
}
else {
// if the current state is LOW then the reedswitch
// went from open to closed:
Serial.println("\nDoor Closed");
value1 = 0;
Serial.print("value1 = ");
Serial.println(value1);
}
}
// save the current state as the last state,
//for next time through the loop
lastReedswitchState = reedswitchState;
}
// this constant won't change:
const int buttonPin = 2;
const int reedswitchpin = 3;
const int ledPin = 13;
// Variables will change:
int reedswitchstate = 0;
int lastreedswitchstate = 0;
int buttonState = 0;
int lastButtonState = 0;
int value1 = 0;
int value2 = 0;
bool enabledisable = false;
bool doorstate = false;
void setup() {
pinmode(reedswitchpin, INPUT);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
reedswitchstate = digitalRead(reedswitchpin);
if (lastreedswitchstate != lastreedswitchstate) {
if (reedswitchstate == HIGH) {
Serial.println("Door closed");
doorstate = false;
} else {
Serial.println("Door opend");
doorstate = true;
}
lastButtonState = buttonState;
//--------------------------------------------
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
Serial.println("Button pressed");
enabledisable = true;
} else {
Serial.println("Button released");
enabledisable = false;
}
lastButtonState = buttonState;
}
I got to here but am struggling with making a toggle switch with a button because i dont want to use delays it stops the rest of my code from running and checking values but the suggestion it works for me it only prints once like it sould thx
I think you have got confused by having both a push button and a reed switch.
My solution does not use a pushbutton, it relies on the reed switch alone.
As I don't have a reed switch available I have used a function generator to simulate the door opening/closing every 5 seconds.
(I've reduced the time from 6 minutes to 6 seconds for test purposes.)
/*
reedswitch is connected between pin 12 and GND,
with pullup resistor enabled.
When door closed pin 12 is low.
When door open pin 12 is high.
derived from https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection
*/
int reedswitch = 12;
int enableddisablebutton = 11;
int led = 13;
int value1 = 0;
int value2 = 0;
bool Enable_Disable = false;
bool doorOpenTimingFlag = false;
int reedswitchState = 0;
int lastReedswitchState = 0;
unsigned long doorOpenedTime = 0;
// unsigned long doorAlarmInterval = 6 * 60 * 1000; // 6 minutes
unsigned long doorAlarmInterval = 6 * 1000; // 6 seconds for test purposes.
void setup() {
pinMode(reedswitch, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// read the reedswitch input pin:
reedswitchState = digitalRead(reedswitch);
// compare the reedswitchState to its previous state
if (reedswitchState != lastReedswitchState) {
// if the state has changed, print message.
if (reedswitchState == HIGH) {
// if the current state is HIGH then the reedswitch
// went from closed to open:
// note the time the door was opened & set timing flag
doorOpenedTime = millis();
doorOpenTimingFlag = 1;
Serial.println("\nDoor Open");
value1 = 1;
Serial.print("value1 = ");
Serial.println(value1);
Serial.print("value2 = ");
Serial.println(value2);
}
else {
// if the current state is LOW then the reedswitch
// went from open to closed:
// stop the timing (clear flag) reset value1 & value2
doorOpenTimingFlag = 0;
Serial.println("\nDoor Closed");
value1 = 0;
Serial.print("value1 = ");
Serial.println(value1);
value2 = 0;
Serial.print("value2 = ");
Serial.println(value2);
}
}
// save the current state as the last state,
//for next time through the loop
lastReedswitchState = reedswitchState;
// check whether door has been opened too long
unsigned long now = millis();
if ((now - doorOpenedTime > doorAlarmInterval) && (doorOpenTimingFlag == 1)) {
doorOpenTimingFlag = 0;
value2 = 1;
Serial.println("\nDoor Opened too long");
Serial.print("value1 = ");
Serial.println(value1);
Serial.print("value2 = ");
Serial.println(value2);
}
}
And the results:
16:20:15.572 -> Door Closed
16:20:15.572 -> value1 = 0
16:20:15.572 -> value2 = 0
16:20:20.533 ->
16:20:20.533 -> Door Open
16:20:20.573 -> value1 = 1
16:20:20.573 -> value2 = 0
16:20:25.565 ->
16:20:25.565 -> Door Closed
16:20:25.565 -> value1 = 0
16:20:25.565 -> value2 = 0
16:20:30.537 ->
16:20:30.537 -> Door Open
16:20:30.577 -> value1 = 1
16:20:30.577 -> value2 = 0
16:20:36.541 ->
16:20:36.541 -> Door Opened too long
16:20:36.581 -> value1 = 1
16:20:36.581 -> value2 = 1
16:20:45.560 ->
16:20:45.560 -> Door Closed
16:20:45.560 -> value1 = 0
16:20:45.560 -> value2 = 0
16:20:53.627 ->
16:20:53.627 -> Door Open
16:20:53.667 -> value1 = 1
16:20:53.667 -> value2 = 0
/*
reedswitch is connected between pin 12 and GND,
with pullup resistor enabled.
When door closed pin 12 is low.
When door open pin 12 is high.
derived from https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection
*/
int reedswitch = 12;
int enableddisablebutton = 11;
int led = 13;
int value1 = 0;
int value2 = 0;
bool Enable_Disable = false;
bool doorOpenTimingFlag = false;
int reedswitchState = 0;
int lastReedswitchState = 0;
unsigned long doorOpenedTime = 0;
// unsigned long doorAlarmInterval = 6 * 60 * 1000; // 6 minutes
unsigned long doorAlarmInterval = 6 * 1000; // 6 seconds for test purposes.
void setup() {
pinMode(reedswitch, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// read the reedswitch input pin:
reedswitchState = digitalRead(reedswitch);
// compare the reedswitchState to its previous state
if (reedswitchState != lastReedswitchState) {
// if the state has changed, print message.
if (reedswitchState == HIGH) {
// if the current state is HIGH then the reedswitch
// went from closed to open:
// note the time the door was opened & set timing flag
doorOpenedTime = millis();
doorOpenTimingFlag = 1;
Serial.println("\nDoor Open");
value1 = 1;
Serial.print("value1 = ");
Serial.println(value1);
Serial.print("value2 = ");
Serial.println(value2);
}
else {
// if the current state is LOW then the reedswitch
// went from open to closed:
// stop the timing (clear flag) reset value1 & value2
doorOpenTimingFlag = 0;
Serial.println("\nDoor Closed");
value1 = 0;
Serial.print("value1 = ");
Serial.println(value1);
value2 = 0;
Serial.print("value2 = ");
Serial.println(value2);
}
}
// save the current state as the last state,
//for next time through the loop
lastReedswitchState = reedswitchState;
// check whether door has been opened too long
unsigned long now = millis();
if ((now - doorOpenedTime > doorAlarmInterval) && (doorOpenTimingFlag == 1)) {
doorOpenTimingFlag = 0;
value2 = 1;
Serial.println("\nDoor Opened too long");
Serial.print("value1 = ");
Serial.println(value1);
Serial.print("value2 = ");
Serial.println(value2);
}
}
Do u know where i can find someone trustworthy like yourself on helping me with my code without me posting it here i have other parts of my code i want help with but i dont want the code to be stolen im planning to make a product and sell it in my country for a low cheaper alternative
It's a piece of test equipment that I used to simulate the reedswitch opening and closing. (I did not have a reedswitch available).
You don't need to worry about it.
I set it up to give out a 5V amplitude squarewave of frequency 100mHz. The output is connected to the Arduino reedswitch input.
That simulates the door being open for 5 seconds, closed for 5 seconds, open for 5 seconds, closed for 5 seconds etc.
Turning off the output simulates the reedswitch being open all the time.