user80195: 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;

}


user10794: 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)

80195: <p>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.</p>
<p>Being a novice to programing and after many attempts to use his code and intergrade it with mine. Was unsuccessful.</p>
<p>so if anyone can help intergrade the gammon's sketch below into mine it would be greatly appreciated. As I'm out of ideas.</p>
<p><strong>HERE IS MY SKETCH</strong></p>
<pre class="lang-cpp prettyprint-override"><code>#include &lt;Servo.h&gt;

// 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 &amp;&amp; lastMotionState == HIGH) { // pin state change: LOW -&gt; HIGH
Serial.println(&quot;OFF&quot;);
servo.write(0);
delay(100); // waits (X)ms for the servo to reach the position
}
else
if (currentMotionState == HIGH &amp;&amp; lastMotionState == LOW) { // pin state change: HIGH -&gt; LOW
Serial.println(&quot;ACTIVE&quot;);
servo.write(180);
delay(2000); // waits (x)ms for the servo to reach the position
}
delay(10); // repeats program - larger delay - trigger is
}
</code></pre>
<p><strong>______________________________________</strong></p>
<p><strong><strong>Author:Ab Kurk</strong>  SLEEP SKETCH</strong></p>
<p>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.</p>
<p>*/</p>
<pre class="lang-cpp prettyprint-override"><code>#include &lt;avr/sleep.h&gt;//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(&quot;just woke up!&quot;);//next line of code executed after the interrupt
digitalWrite(LED_BUILTIN,HIGH);//turning LED on
}

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

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

}
</code></pre>
 10794: <p>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.</p>
<p>You need to rework somewhat.</p>
<ul>
<li><p>An interrupt to detect a switch press. Then the processor can sleep, using very little power.</p>
</li>
<li><p>The interrupt, when it happens, wakes the processor</p>
</li>
<li><p>You activate the motor to unlock the door or whatever-it-is.</p>
</li>
<li><p>You put the processor into low power mode, and go back to sleep</p>
</li>
</ul>
<p>Since my name was mentioned in the question, tips for doing low-power stuff are <a href="http://www.gammon.com.au/power" rel="nofollow noreferrer">at this link</a>. And <a href="http://www.gammon.com.au/interrupts" rel="nofollow noreferrer">here for stuff about interrupts</a>.</p>
<p>Plus a slightly abridged version (due to space limitations) <a href="https://arduino.stackexchange.com/questions/30968/how-do-interrupts-work-on-the-arduino-uno-and-similar-boards/30969#30969">here on this site</a>.</p>
<p><em>(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)</em></p>
