-1

My sketch below is simply using a touch sensor to activate a servo motor to go from from initial position 0 to 180 then back to 0. then I want the Adruino NANO to go to sleep to conserve some battery life.

Being a novice to programing and after many attempts to use his code and intergrade it with mine. Was unsuccessful.

so if anyone can help intergrade the gammon's sketch below into mine it would be greatly appreciated. As I'm out of ideas.

HERE IS MY SKETCH

#include <Servo.h>

// constants won't change
const int SENSOR = 2; // Arduino pin connected to motion sensor's pin 7
const int SERVO = 9; // Arduino pin connected to servo motor's pin 9

Servo servo; // create servo object to control a servo

int pos = 0; // variable to store the servo position

// variables will change:
int lastMotionState; // the previous state of motion sensor
int currentMotionState; // the current state of motion sensor

void setup() {

Serial.begin(9600); // initialize serial
pinMode(SENSOR, INPUT); // set arduino pin 9 to input mode
servo.attach(SERVO); // attaches the servo on pin 12 to the servo object

servo.write(0);
currentMotionState = digitalRead(SENSOR);
}

void loop() {
lastMotionState = currentMotionState; // save the last state
currentMotionState = digitalRead(SENSOR); // read new state

if (currentMotionState == LOW && lastMotionState == HIGH) { // pin state change: LOW -> HIGH
Serial.println("OFF");
servo.write(0);
delay(100); // waits (X)ms for the servo to reach the position
}
else
if (currentMotionState == HIGH && lastMotionState == LOW) { // pin state change: HIGH -> LOW
Serial.println("ACTIVE");
servo.write(180);
delay(2000); // waits (x)ms for the servo to reach the position
}
delay(10); // repeats program - larger delay - trigger is
}

______________________________________

Author:Ab Kurk SLEEP SKETCH

This sketch is part of the beginners guide to putting your Arduino to sleep tutorial. It is to demonstrate how to put your arduino into deep sleep and how to wake it up.

*/

#include <avr/sleep.h>//this AVR library contains the methods that controls the sleep modes
#define interruptPin 2 //Pin we are going to use to wake up the Arduino

void setup() {
Serial.begin(115200);//Start Serial Comunication
pinMode(LED_BUILTIN,OUTPUT);//We use the led on pin 13 to indecate when Arduino is A sleep
pinMode(interruptPin,INPUT_PULLUP);//Set pin d2 to input using the buildin pullup resistor
digitalWrite(LED_BUILTIN,HIGH);//turning LED on
}

void loop() {
delay(5000);//wait 5 seconds before going to sleep
Going_To_Sleep();
}

void Going_To_Sleep(){
sleep_enable();//Enabling sleep mode
attachInterrupt(0, wakeUp, LOW);//attaching a interrupt to pin d2
set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting the sleep mode, in our case full sleep
digitalWrite(LED_BUILTIN,LOW);//turning LED off
delay(1000); //wait a second to allow the led to be turned off before going to sleep
sleep_cpu();//activating sleep mode
Serial.println("just woke up!");//next line of code executed after the interrupt
digitalWrite(LED_BUILTIN,HIGH);//turning LED on
}

void wakeUp(){
Serial.println("Interrrupt Fired");//Print message to serial monitor
sleep_disable();//Disable sleep mode

detachInterrupt(0); //Removes the interrupt from pin 2;

}
3
  • please ask a question ... this is a Q&A site
    – jsotola
    Sep 7, 2021 at 2:01
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Community Bot
    Sep 7, 2021 at 6:00
  • Question was how to incorporate my my sketch with an Kirk’s.
    – Anthony
    Sep 9, 2021 at 1:11

1 Answer 1

1

I'm going to guess that you are doing something like a door or safe lock, which may occasionally be used to activate a motor to unlock something but 99.99% of the time is sitting there doing nothing, and you want to save power in that time.

You need to rework somewhat.

  • An interrupt to detect a switch press. Then the processor can sleep, using very little power.

  • The interrupt, when it happens, wakes the processor

  • You activate the motor to unlock the door or whatever-it-is.

  • You put the processor into low power mode, and go back to sleep

Since my name was mentioned in the question, tips for doing low-power stuff are at this link. And here for stuff about interrupts.

Plus a slightly abridged version (due to space limitations) here on this site.

(You aren't supposed to make answers where the answer is totally in the link, but in this case the link is just supplementary material)

2
  • "You aren't supposed to make answers where the answer is totally in the link...". I totally understand Nick. Power management is complex, really depends on the HW (i.e. processor & circuit you are using) and should not be "added" to a project. Rather the project (including the HW) should be built around power management.
    – st2000
    Sep 7, 2021 at 12:28
  • Your right about my project. Thank you for the info will go though it this weekend and attempt to get this thing to work
    – Anthony
    Sep 9, 2021 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.