Interrupt Problem

I am trying to make a program that keeps the arduino asleep until I press a button. It then moves a servo 90 degrees as long as the button state is high, and then move the servo back to 0 when the button state is low. the problem may also be with the way I tried to make the arduino sleep, however it stil doesnt work when I take the sleeping part out.

sleep_arduino_latching_button_code.ino (487 Bytes)

The attached code. OP, when it’s short just put it in the post. That way the folks on phones and tablets can see it. That will help you get more responses.

#include <Servo.h>
#include "LowPower.h"
Servo servo;
void setup() {
  // put your setup code here, to run once:
pinMode(2,INPUT);
pinMode(9, OUTPUT);
servo.write(0);
servo.attach(9);
attachInterrupt(0, buttonPressed, RISING);
attachInterrupt(0, buttonDepressed, FALLING);
}

void loop() {
  // put your main code here, to run repeatedly:
 LowPower.powerDown(SLEEP_FOREVER , ADC_OFF, BOD_OFF);
}
void buttonPressed (){
  servo.write(180);
}
void buttonDepressed(){
  servo.write(0);
}
attachInterrupt(0, buttonPressed, RISING);
attachInterrupt(0, buttonDepressed, FALLING)

You can't attach two interrupts on the same pin. Make it a CHANGE interrupt and test the pin state in the ISR to see if it was rising or falling.

I also tried to test purely just the interrupt with this code and it still didnt work. If you cant tell I am a bit new to this.

#include <Servo.h>
Servo servo;

void setup() {
// put your setup code here, to run once:
servo.attach(9);
servo.write(0);
pinMode(2, INPUT);
attachInterrupt(0, pressed, RISING);
}

void loop() {
// put your main code here, to run repeatedly:

}
void pressed(){
servo.write(90);
}

Define "didn't work". What happened? How did that compare with what you expected?

It did nothing as in the servo didnt move at all. I wanted it to sweep to 90 degrees in the test.

Show your wiring. Draw us a schematic. How is the button wired?

This is the current setup right now.

So your button is connecting to ground. That's good, but I want you to think for a second. Where would it ever see 5V from to read HIGH?

Change your pinMode to INPUT_PULLUP and that will turn on the 5V pullup resistor so that the pin reads HIGH when it is not pressed. It should read LOW when it is pressed, so you want a FALLING interrupt to react to a button press.

Thanks! I should be able to build off from here!