Have one handler for each input, and caapture both edges of each interrupt. Attach them once, in setup(), and leave them there. It kinda makes my head hurt trying to figure out how that's even supposed to work.įirst, don't keep attaching and detaching interrupts. What am I doing wrong in the code? Is there any way to fix it without changing the functionality of the device? I could fix this by just keeping the solenoids retracted the whole time the lid is opened, but the solenoids use a lot of power in a little bit of time, so I want to avoid this. It seems that all interrupts are working except for a HIGH level interrupt on the hall-effect sensor.Īnd using debug prints to Serial, I determined that, when pressing the button and unlocking the box, when the solenoids are retracted, the currentState does successfully change to WAITING_FOR_LID_OPEN. However, in step 6, when the box is opened, the interrupt for the hall-effect sensor is not triggered, so the solenoids do not extend once the box is opened, and the currentState is not changed to LOCKED_OPEN. Here’s the issue: Everything works fine through step 5, so I know that the interrupts are working properly for both the button and the hall-effect sensor. Serial.println("Attaching interrupt to magnet pin.") ĪttachInterrupt(digitalPinToInterrupt(MAGNET_PIN), lidOpened, HIGH) ĭetachInterrupt(digitalPinToInterrupt(MAGNET_PIN)) ĪttachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonPressed, LOW) Long currentTime //Increment this every time timer is triggeredĭetachInterrupt(digitalPinToInterrupt(BUTTON_PIN))
Long lastLocked //Time box was last locked Long lastTime //Time last event happened. LOCKED_OPEN, //Lid is open, solenoids locked LOCKED_CLOSED, //Lid is closed and locked Would be more like 2700 in the final thing)ĬLOSED, //Lid is closed, but solenoids are unlocked #define WAIT_TIME 2 //Time box is to remain locked, in 8-second intervals (Is only this small for testing. #define SOLENOID_TIME 1000 //Time solenoids are to remain activated You open the lid, which triggers an interrupt via that aforementioned hall-effect sensor, and extends the solenoids.EitherĪ- Not enough time has passed, so nothing happens, except the button interrupt is reattachedī- Enough time has passed, so the solenoids are retracted and the lid is unlocked You hit the button again, which triggers an interrupt.You close the box, which triggers an interrupt via a hall-effect sensor in the lid and a magnet in the box, and extends the solenoids.You hit a button on the outside of the box, which triggers an interrupt and retracts the solenoids.The program starts out assuming the box is opened, and the solenoids are extended (locked) (Since the box will have to be open to replace the batteries, which is when the arduino will start up).
The solenoids are connected to pin 12, and are extended (locked) when unpowered. The circuit consists of a hall-effect magnet sensor and a momentary button, wired to pins 2 and 3 respectively, and set to INPUT_PULLUP mode.
#Smoothcursor only working sometimes pro#
I’m using an Arduino Pro Mini, and to save power, it spends most of its time in SLEEP_MODE_PWR_DOWN, and I’m using interrupts to wake it. I am making a box that, after being opened and shut, locks itself and won’t open again for another 6 hours (or so).