Using 1.6.5 on pro mini 16mHz
I have 3 PIR connected with only pin-hole lenses to obtain clockwise/anticlockwise hand sweeps around circular table.
All code works fine up to implementing sleep mode which is to save power when nobody is around.
I’ve lost a lot of references when my PC motherboard kaput, but I know there is no definitive answer to Atmega waking with level change interrupt HIGH or LOW only in power down mode.
The PIR in any case outputs a pulse, ie LOW to HIGH and back again, therefore fulfilling both conditions at some point in the detection.
So I have tried both level changes.
I could hardwire in an inverter transistor, but I want to diode logic all 3 PIR signals to the one interrupt pin ultimately and the voltage drop will be larger with both.
I really want to just bugsolder a component or two onto the board, not expand into a satellite PCB with logic inverters etc.
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
int state [2];//detected movement flags
void motorcw(void)
{
state[0]=0;//clear detected movement flag array
state[1]=0;
state[2]=0;
digitalWrite(6,LOW);//de-activate motor
digitalWrite(7,HIGH);
for (int x=1;x<=2;x++)//slow flash diagnostic indicator
{digitalWrite(13,HIGH);
delay(250);
digitalWrite(13,LOW);
delay(250);}
digitalWrite(6,HIGH);
digitalWrite(7,LOW);
}
void motorccw(void)
{
state[0]=0;//clear detected movement flag arrary
state[1]=0;
state[2]=0;
digitalWrite(8,LOW);//de-activate motor
digitalWrite(9,HIGH);
for (int x=1;x<=4;x++)//fast flash diagnostic indicator
{digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
delay(100);}
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
}
void sleepNow(void)
{
// Set pin 2 as interrupt and attach handler:
attachInterrupt(0, pinInterrupt, LOW);
delay(100);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
// Set sleep enable (SE) bit:
sleep_enable();
sleep_mode();
// Upon waking up, sketch continues from this point.
sleep_disable();
}
void pinInterrupt(void)
{
detachInterrupt(0);
}
void setup()
{
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(5,INPUT);
//bridge motor drive outputs as follows
//write inactive values to bridge first to avoid shoot-through
digitalWrite(6,HIGH);
digitalWrite(7,LOW);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
pinMode(6,OUTPUT);//pnp cw
pinMode(7,OUTPUT);//npn cw
pinMode(8,OUTPUT);//pnp ccw
pinMode(9,OUTPUT);//npn ccw
}
void loop()
{
for (int long awake=0;awake<1000000;awake++)
{
if(digitalRead(3)==HIGH){state[0]=1;}
if(state[0]==1 && state[1]==1){motorccw();}
if(state[0]==1 && state[2]==1){motorcw();}
if(digitalRead(4)==HIGH){state[1]=1;}
if(state[1]==1 && state[2]==1){motorccw();}
if(state[1]==1 && state[0]==1){motorcw();}
if(digitalRead(5)==HIGH){state[2]=1;}
if(state[2]==1 && state[1]==1){motorcw();}
if(state[2]==1 && state[0]==1){motorccw();}
}
sleepNow();
}