SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By Jimmy the geek
#179001
I am using the autodriver and your 125 oz/in stepper motor. The motor is used to open a door when motion is detected from a PIR sensor. I'm using an arduino mega 2560. The system works fine for several days then I notice it's no longer functoning. When I investigate by triggering the motion sensor I hear a faint jolt at the motor but it does not rotate 1800 steps as programmed. The only thing I can do is unplug the arduino power and the system functions fine after that. The entire sketch is below. You'll find the autodriver config commands at the top and the open door function below. Can someone help resolve this?
Code: Select all
/* Duke door */ 
#include "Wire.h"
#include "RTClib.h"
#include <Keypad.h>
#include "Arduino.h"
#include <LiquidCrystal.h>
#include <AutoDriver.h>
//#include "pitches.h"

/* notes in the Jeopardy melody:
  int jeopardy[] = {
  NOTE_C4, NOTE_F4, NOTE_C4, NOTE_F3, NOTE_C4, NOTE_F4, NOTE_C4,
  NOTE_C4, NOTE_F4, NOTE_C4, NOTE_F4,
  NOTE_A4, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_CS4,
  NOTE_C4, NOTE_F4, NOTE_C4, NOTE_F3, NOTE_C4, NOTE_F4, NOTE_C4,
  NOTE_F4, NOTE_D4, NOTE_C4, NOTE_AS3,
  NOTE_A3, NOTE_G3, NOTE_F3};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int jeopardyDurations[] = {
  4, 4, 4, 4, 4, 4, 2,
  4, 4, 4, 4,
  3, 8, 8, 8, 8, 8,
  4, 4, 4, 4, 4, 4, 2,
  3, 8, 4, 4,
  4, 4, 4 };*/

AutoDriver A(53,44,42);

RTC_DS1307 RTC;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {25,35,33,29}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {27, 23, 31}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

const int ledPin=45;
const int pirinPin=41;
const int piroutPin=43;
int pirinval=0;
int piroutval=0;
char lastmode = '1';
char currentmode = '1';
int val =1;

long previousMillis = 0;        
long interval = 10000;

void setup() 
{ 
Serial.begin( 9600); 
//Autodriver config commands
A.configSyncPin(BUSY_PIN, 0);
A.configStepMode(STEP_FS);
A.setMaxSpeed(400);
//A.setMinSpeed(10);
A.setFullSpeed(600);
A.setAcc(100);
A.setDec(200);
A.setOCThreshold(OC_1875mA);
A.setPWMFreq(PWM_DIV_2, PWM_MUL_2);
A.setSlewRate(SR_290V_us);
//A.setOCShutdown(int OCShutdown);
A.setOscMode(INT_16MHZ_OSCOUT_2MHZ);

Wire.begin();//from fetch time
RTC.begin();
pinMode(ledPin, OUTPUT);      // declare LED as output
pinMode(pirinPin, INPUT);     // declare inside pir sensor as input
pinMode(piroutPin, INPUT);     // declare outside pir sensor as input

// set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);

if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    //RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}

void loop() 
{ 

pinMode (ledPin, OUTPUT);
char key = keypad.getKey();//char key = getKey(); 
if( key !=0) 
{
 // if the character is not 0 then 
// it's a valid key press 
currentmode=key;
}
else if (lastmode=='5')
{
currentmode='1';
}
else
{
currentmode=lastmode;
}
pirinval==LOW;
piroutval==LOW;

lcd.setCursor(0, 0);
lcd.print("System ON");
DateTime now = RTC.now();
    
    /*Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);*/
        
    lcd.print(' ');
    lcd.print(now.hour(), DEC);
    lcd.print(':');
    lcd.print(now.minute(), DEC);
    lcd.print(' ');
if(currentmode=='3'){    
if (now.hour()==12 && now.minute()==1 && now.second()<5 ){
  openDoor();
}

if (now.hour()==18 && now.minute()==1 && now.second()<5){
  openDoor();
}}

switch(currentmode)
{
  case '1':
  {
  lcd.setCursor(0, 1);
  lcd.print("In or Out   "); 
  lcd.print(currentmode);
  
  pirinval = digitalRead(pirinPin);  // read input value
  piroutval = digitalRead(piroutPin);  // read input value
  if (pirinval || piroutval == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    openDoor();
    digitalWrite(ledPin, LOW);
  }
  else
  {digitalWrite(ledPin, LOW);
  }
  break;
  }
  case '2':
  {
  lcd.setCursor(0, 1);
  lcd.print("In only     "); 
  lcd.print(currentmode); 
  pirinval = digitalRead(pirinPin);  // read input value
  piroutval = digitalRead(piroutPin);  // read input value
  if (pirinval || piroutval == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    if (piroutval==HIGH){
    openDoor();    
  }
  }
  else
  {digitalWrite(ledPin, LOW);
  }
  break;
  }
  case '3':
  {
  lcd.setCursor(0, 1);
  lcd.print("Timer       "); 
  lcd.print(currentmode);
  pirinval = digitalRead(pirinPin);  // read input value
  piroutval = digitalRead(piroutPin);  // read input value
  if (pirinval || piroutval == HIGH) // check if the input is HIGH
  {            
    digitalWrite(ledPin, HIGH);  // turn LED ON
    val =int(now.hour());
    if(val>5 && val<12){
       if(piroutval==HIGH){
        openDoor();
        }
    }
    else if(val==12){  
      if (pirinval || piroutval == HIGH){
        openDoor();
    }
    }
    else if(val>12 && val<18){  
      if(piroutval==HIGH){
        openDoor();
    }
    }
    else if(val==18){  
      if (pirinval || piroutval == HIGH){
        openDoor();
      }
    }
    else if(val>18 && val<23){  
      if(piroutval==HIGH){
        openDoor();
      }
    }
    else if(val>=23 || val<6){  
    lcd.setCursor(0, 1);
  lcd.print("Restricted  ");
  lcd.print(currentmode);
      }
  }
  else
  {digitalWrite(ledPin, LOW);
  }
break;
}
case '4':
 {
  lcd.setCursor(0, 1);
  lcd.print("Restricted  "); 
  lcd.print(currentmode); 
  pirinval = digitalRead(pirinPin);  // read input value
  piroutval = digitalRead(piroutPin);  // read input value
  if (pirinval || piroutval == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    }  
  else
  {digitalWrite(ledPin, LOW);
  }
  break;
  /*case '5':
  {
    lcd.setCursor(0, 1);
    lcd.print("Training Mode       "); 
    lcd.print(currentmode);
    //play_jeopardyMelody();
  }  */
  default :
  lcd.setCursor(0, 1);
  lcd.print("Restricted  ");
  lcd.print(currentmode); 
  digitalWrite(ledPin,LOW);
  break;
 }
}
lastmode=currentmode; 
//Serial.print(lastmode);    
} 
//Door function
void openDoor()
{
//digitalWrite(ledPin, HIGH);  // turn LED ON
lcd.setCursor(0, 1);
lcd.print("Opening Door!     ");
A.move(FWD, 1800);
delay(30000);
/*
unsigned long currentMillis = millis();
previousMillis = currentMillis; 
  while(currentMillis - previousMillis < interval) {
    lcd.setCursor(0, 1);
    Serial.println(currentMillis - previousMillis);     
}*/
lcd.setCursor(0, 1);
lcd.print("Closing Door!    ");
A.move(REV, 1800);
delay(10000);
//digitalWrite(ledPin, LOW);
}
//End Door Function

/*void play_jeopardyMelody(){
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < (sizeof(jeopardyDurations)/2); thisNote++) {

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int jeopardyDuration = 1000/jeopardyDurations[thisNote];
    tone(8, jeopardy[thisNote], jeopardyDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = jeopardyDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}*/
By Jimmy the geek
#179002
Power: 24V selectable walwart, arduino is powered by a wall adapter as well.
Load: 2 lbs winding

Load should not be a problem. I worked through this by reducing max speed and providing a counter balance on the door.
User avatar
By Ross Robotics
#179013
Although I just skimmed through the code, it looks like it could the millis() function overflowing and causing issues.

http://www.gammon.com.au/millis
By Jimmy the geek
#179164
The milis code is commented out. I tried to use it to replace the delay() function but I never got it working. Other thoughts?