Hello,
I would really apappreciate your help! I will write a bit about my project and then add the code, if you could go through my code and tell me if its the cause of my proble, it will help me alot.
The project Im working on is using magnetic sensors. what it does is that if a door is open for 20 seconds, the rduino will do 2 things, one, is sending this information to a raspberry pi, and it will also give voltage to a certain pin, and that pin will be connected to another arduino that controls a solenoid.
My problem is with the main arduino that gets the information from the magnetic sensors, when I seperate the magnet from the sensors, sometimes it doesnt sense the state change.
I am not sure why, I have tried alot of different things to fix it and none worked. currently, I am looking to try new sensors, but this problem didnt happend a month ago, and the only thing that changed at the code is that I added the parts regarding to a switch that stops the voltage to the pin if both doors are closed and also the part the concerns sending the voltage.
this is the magnetic sensors class:
#ifndef MAGNETIC_SENSOR_H
#define MAGNETIC_SENSOR_H
#include <Arduino.h>
class magnetic_sensor
{
private:
byte pin;
byte state;
byte lastState;
byte reading;
bool toggled;
unsigned long lastDebounceTime;
unsigned long debounceDelay;
public:
magnetic_sensor() {} //do not use
magnetic_sensor(byte x)
{
pin = x;
}
void init()
{
pinMode(pin, INPUT_PULLUP);
lastDebounceTime = 0;
debounceDelay = 20;
lastState = LOW;
}
void isChanged()
{
reading = digitalRead(pin);
if (reading != lastState) {
lastDebounceTime = millis();
}
}
void debounce()
{
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != state) {
state = reading;
toggled = HIGH;
}
else {
toggled = LOW;
}
}
lastState = reading;
}
bool isOpen()
{
updateState();
if (state == HIGH) {
return LOW;
}
else {
return HIGH;
}
}
bool isClosed()
{
return !isOpen();
}
void updateState()
{
isChanged();
debounce();
}
bool isToggledOn()
{
if (toggled == HIGH && state == HIGH) {
return HIGH;
}
else {
return LOW;
}
}
bool isToggledOff()
{
if (toggled == HIGH && state == LOW) {
return HIGH;
}
else {
return LOW;
}
}
/*void tester(byte num)
{
Serial.print("micro");
Serial.print(num);
Serial.print(" - ");
Serial.print("pin: ");
Serial.print(pin);
Serial.print(" state: ");
Serial.print(state);
Serial.print(" lastState: ");
Serial.print(lastState);
Serial.print(" reading: ");
Serial.print(reading);
Serial.print(" lastDebounceTime: ");
Serial.print(lastDebounceTime);
Serial.print(".\n");
}*/
};
#endif MAGNETIC_SENSOR_H
and this is the main code:
#include "magnetic_sensor.h"
#include "button.h"
magnetic_sensor magnetic_sensor_1(12);
magnetic_sensor magnetic_sensor_2(8);
Button button(2);
const int outputPin = 4;
unsigned long lastSensorTime = 0;
const unsigned long sensorTimeout = 20000;
bool sensorMessageSent = false;
void doorClosedRoutine() {
Serial.println("shutter off");
digitalWrite(outputPin, LOW);
}
void setup() {
Serial.begin(9600);
button.init();
magnetic_sensor_1.init();
magnetic_sensor_2.init();
pinMode(outputPin, OUTPUT);
}
void loop() {
magnetic_sensor_1.updateState();
magnetic_sensor_2.updateState();
button.updateState();
if (magnetic_sensor_1.isToggledOn()) {
Serial.println("Door 1 opened");
if (lastSensorTime ==0) {
lastSensorTime = millis();
sensorMessageSent = false; //reset the flag
}
}
if (magnetic_sensor_2.isToggledOn()) {
Serial.println("Door 2 opened");
if (lastSensorTime ==0) {
lastSensorTime = millis();
sensorMessageSent = false; //reset the flag
}
}
if (lastSensorTime != 0 && millis() - lastSensorTime >= sensorTimeout){
digitalWrite(outputPin, HIGH);
if (!sensorMessageSent) {
Serial.println("Sensor open for 20 seconds");
sensorMessageSent = true; }
}
if (magnetic_sensor_1.isOpen() && magnetic_sensor_2.isOpen()) {
lastSensorTime = 0;
if (button.isToggledOn()) {
doorClosedRoutine(); }
}
}
thank you for your help and sorry for the long read!
