Trouble Initiating Sleep within an If...else statement

Hello,

I have been attempted to encode my arduino to rotate a motor and when a PhotoInterrupter is not blocked, and to sleep when the PhotoInterrupter is blocked. While the motor stops moving when the PhotoInterrupter is blocked, the arduino still draws current as if it were idle instead of sleeping.

How can I remedy this? Thanks for any assistance you can provide!

#include <avr/sleep.h>
#include <avr/power.h>
#include <Wire.h>
#include <Stepper.h>
#include <SPI.h>
#include <avr/interrupt.h>
#define INT0 0 
#define INT0_PIN 2


int in1Pin = 7 ;
int in2Pin = 6;
int in3Pin = 5;
int in4Pin = 4;
int steps = 64;//512;


int PIState = 1;
int lastState = 1;
int PIPin = 2;

Stepper motor(512, in1Pin, in2Pin, in3Pin, in4Pin);



void setup()
{

  Serial.begin(9600);
  Serial.println("Starting up...");


  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(in3Pin, OUTPUT);
  pinMode(in4Pin, OUTPUT);
  pinMode(PIPin, INPUT);

  motor.setSpeed(35);

 attachInterrupt(0, pinInterrupt, RISING);

  delay (500);
}



void loop()
{

  PIState = digitalRead(PIPin);
  Serial.print("Photointerrupter State: ");
  Serial.println(PIState);


  if (PIState == 1) {
    Serial.println("Turning motor...");
    motor.step(-steps / 2);
    motor.step(steps);
  }



  else  {
    //Serial.println("Yes");
    Serial.println("Ready to nap");
   sleepNow();

  }


  delay(1000);

}
void sleepNow()
{

 
  delay(100);

  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();

  sleep_mode();

}

void pinInterrupt(void)
{
  detachInterrupt(0);

  /* The program will continue from here after the WDT timeout*/
  sleep_disable(); /* First thing to do is disable sleep. */

  power_all_enable();
}

What does the serial monitor say? Before you answer that question, I'd put a Serial.flush() before going to sleep.

The photointerrupter will be using some current, are you measuring that?

Serial Monitor Output

Starting up...
Photointerrupter State: 1
Turning motor...
Photointerrupter State: 1
Turning motor...
Photointerrupter State: 1
Turning motor...
Photointerrupter State: 0
Ready to nap

Nothing is printed to the serial monitor when the photointerrupter is blocked (Photointerrupter State: 0), otherwise it is as shown above.

Adding Serial.flush() before the sleep line appeared to freeze the arduino once the photointerrupter was blocked.

Any thoughts?

You have a stepper motor that draw a lot of current, all the time. You have an Arduino that draw a little bit of current. It seems to me that you are looking in the wrong place to save current.

Thank you both for your input!

Prior to "sleepNow();", I inserted

digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, LOW);
digitalWrite(in3Pin, LOW);
digitalWrite(in4Pin, LOW);

and

digitalWrite(in1Pin, HIGH);
digitalWrite(in2Pin, HIGH);
digitalWrite(in3Pin, HIGH);
digitalWrite(in4Pin, HIGH);

prior to "motor.step(-steps / 2);".

The stepper motor no longer draws current while the arduino is sleeping.

Thanks again!