I have a project that basically takes input from some pushbuttons and if the combination is correct it drives a DC motor in one direction for one second, then waits, then drives it in the other direction for one second. This project works fine without the motor attached, but once the motors are attached the pro mini does erratic things. I have disabled the regulator and LED on the pro mini as this is a battery-operated application and therefore needs ultralow power. The wires on the motor feed are twisted and only about 4 inches long.
Any suggestions?
unsigned long pTime=0; //timer start compared to mTime/millis() time for long keypress
unsigned long mTime=0; // holds value from millis() time for long keypress
unsigned long SpTime=0; //Session timer start compared to mTime/millis() time
unsigned long SmTime=0; // holds value from millis() time for session
int but1C=0; // button 1 counter
int but2C=0;
int but3C=0;
int but1;
int but2;
int but3;
int unlock=0;
const int longPress=1500; //time needed to press all 3 buttons at end of key sequence to unlock (@16mhz)
const int timeRLYon=1000; //leave on relay time to run motor either direction
const int openDoorTime=2000; // time between unlock relay run and lock run
const int sessionTime=8000; //once woke, time to run script and enter door code before sleep
#include <LowPower.h>
void wakeUp(){
// Just a handler for the pin interrupt.
}
void setup() { // put your setup code here, to run once:
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
Serial.begin(9600);
}
void loop() { // put your main code here, to run repeatedly:
if (SmTime-SpTime >= sessionTime){
attachInterrupt(0, wakeUp, LOW); // Allow wake up pin 0 to trigger interrupt on LOW.
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); // Enter power down state with ADC and BOD module disabled.
detachInterrupt(0); // Disable external pin interrupt on wake up pin.
//delay(5);
//but1C=0; //when wake, 0 out button counters and:
//but2C=0;
//but3C=0;
SmTime=millis(); // move clock to SmTime
SpTime=SmTime; // move clock time (millis/SmTime) to SpTime for comparison
}
but1=digitalRead (2); //read button state every clock cycle
but2=digitalRead (3);
but3=digitalRead (4);
if ((but1==0) && (but2==1) && (but3==1) && (but1C==0)){ //if but1 and only 1 LOW, increment but1C and wait for next keypress
but1C=1;
but2C=0;
but3C=0;
}
if ((but1== 1) && (but2==1) && (but3==0) && (but1C==1)){ //if but3 pressed not but2, start over
but1C=0;
but2C=0;
but3C=0;
}
if ((but1== 1) && (but2==0) && (but3==1) && (but1C==1)){ //if but2 pressed proceed
but1C=0;
but2C=1;
but3C=0;
}
if ((but1==1) && (but2==1) && (but3==0) && (but2C==1)){ //if but3 pressed proceed
but1C=0;
but2C=0;
but3C=1;
}
if ((but1== 1) && (but2==0) && (but3==1) && (but3C==1)){ //if but2 is pressed instead of all 3 start over
but1C=0;
but2C=0;
but3C=0;
}
if ((but1==0) || (but2==0) || (but3==0) && (but3C==1 ) && (unlock==0)){ //allow time for all 3 buttons to make contact before next
//delay (500);
}
if ((but1==0) && (but2==0) && (but3==0) && (but3C==1 ) && (unlock==0)){ //if all 3 buttons pressed set unlock to 1 and:
pTime=mTime; //move mTime (millis) to pTime for comparison with mTime below
SpTime=SmTime; // restart session time counter
unlock=1;
}
if ((but1==1 || but2==1 || but3==1) && (unlock==1) && (mTime-pTime < longPress)){ //if all buttons not held for longPress set unlock=0 and start over
unlock=0;
but1C=0;
but2C=0;
but3C=0;
}
if ((but1==0) && (but2==0) && (but3==0) && (but3C==1) && (mTime-pTime >= longPress) && (unlock==1)){ //if all buttons held for >= longPress
digitalWrite(5, HIGH); //then start open door sequence
delay(timeRLYon);
digitalWrite(5, LOW);
delay (openDoorTime);
digitalWrite(6, HIGH);
delay(timeRLYon);
digitalWrite(6, LOW);
but1C=0;
but2C=0;
but3C=0;
unlock=0;
}
delay(20);
mTime=millis(); //move clock current time to mTime
SmTime=millis();
//delay (10);
Serial.print ("but1C");
Serial.print (" ");
Serial.print ( but1C);
Serial.print (" ");
Serial.print ("but2C");
Serial.print (" ");
Serial.print ( but2C);
Serial.print (" ");
Serial.print ("but3C");
Serial.print (" ");
Serial.print ( but3C);
Serial.print (" ");
Serial.print ("Unlock");
Serial.print (" ");
Serial.println ( unlock);
} //loop end
