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();
}