loading

Stepper Enable

Seems my completed code is not completed yet. I need to enable the stepper. I added enable_pin = 13 so it will spin free. It works with the push button until the keypad is used the no more button pin, but will continue to work with the keypad properly.  Is there a correct way to use enable for 2 inputs, button and keypad?




#include <Password.h>
#include <Keypad.h>
#include <Stepper.h>
Password password = Password( "4444" ); //password to unlock door, can be changed

const byte ROWS = 4; // Four rows
const byte COLS = 3; // columns
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3',},
{'4','5','6',},
{'7','8','9',},
{'*','0','#',},
};

byte rowPins[ROWS] = { 5, 4, 3, 2 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 8, 7, 6,};// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
Stepper myStepper(stepsPerRevolution, 9,10,11,12);           
int enable_pin = 13;
int button_pin = A0;
int button_pinstate = 0; // variable to store current pir state
//int lastpirstate = 0; // variable to store last pir state
int lastbutton_pinstate = 0;
int pos = 60;
int pos2 = -60;

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

  unsigned long offtime;
  boolean  helper_light_is_on = false; // helper light off       

void setup(){                            
                              
  myStepper.setSpeed(60);  // set the speed at 60 rpm:
  pinMode( button_pin, INPUT);
  pinMode(enable_pin, OUTPUT);
  digitalWrite(enable_pin, LOW);
  digitalWrite(button_pin, HIGH); // pull-up
  Serial.begin(9600);
  Serial.write(254);
  Serial.write(0x01);
  delay(200);
  pinMode(15, OUTPUT);  //Helper light
  pinMode(16, OUTPUT);  //green light
  pinMode(17, OUTPUT);  //red light
 
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}
void loop()
{
button_pinstate = digitalRead(button_pin);
  if(button_pinstate != lastbutton_pinstate) {
    if(button_pinstate == LOW) {
      digitalWrite(enable_pin,HIGH);
    myStepper.step(pos);   
  } else{
       myStepper.step(pos);
       delay(2000);
       myStepper.step(pos2);
        digitalWrite(enable_pin,LOW);
    }
}
    static int counter = 0;
    if ((++counter & 0x3f) == 0)
    Serial.println();
    delay(100);
    keypad.getKey();
    myStepper.step(0);
    process_helper_light();
}
void keypadEvent(KeypadEvent eKey)
{
  switch (keypad.getState())
  {
    case PRESSED:
      // a key is pressed so light the helper light
      helper_light_is_on = true;
      digitalWrite(15,HIGH);
      offtime = millis() + 5000;  // set the offtime for 30 seconds in the future
     
      Serial.print(" enter: ");
      Serial.println(eKey);
      delay(10);
      Serial.write(254);
      switch (eKey)
      {
        case '*':
          checkPassword();
          delay(1);
          break;
        case '#':
          password.reset();
          delay(1);
          break;
        default:
          password.append(eKey);
          delay(1);    
      }
  }
}

void checkPassword()
{
  if (password.evaluate())  //if password is right unlock door
  { 
    Serial.println(" Accepted");
    Serial.write(254);
    delay(10);
     digitalWrite(enable_pin,HIGH);
    myStepper.step(pos);
    Serial.print(" open ");
    digitalWrite(16, HIGH);//turn on green led
    delay(2000); //wait 5 seconds
    digitalWrite(16, LOW);// turn offgreen led
    myStepper.step(pos2);
     digitalWrite(enable_pin,LOW);
  } 
  else
  {
    Serial.println(" Denied"); //if passwords wrong keep door locked
    Serial.write(254);
    delay(10);
    myStepper.step(0);
    Serial.println(" locked ");    
    digitalWrite(17, HIGH); //turn on red led
    delay(2000); //wait 5 seconds
    digitalWrite(17, LOW);//turn off red led
  digitalWrite(enable_pin,LOW);  
  }  //Closes first else
}  //Closes Check password
// this routine turns off the light when the timer expires
void process_helper_light(void)
{
  if (helper_light_is_on)
  {
    if (millis() >= offtime)
    {
      digitalWrite(15,LOW);  //turn off the helper light
      helper_light_is_on = false;
    }
  }
}

sort by: active | newest | oldest
If (button | keypad) enable= high;
WWC: FYI, double pipe not single. "||" not "|" for boolean OR. A single pipe is used for bitwise OR. Spent ages debugging a similar problem recently.
WWC (author)  Jayefuu4 years ago
Would i need to add something such as: boolean button_pin_||_keypad_is_on = false;


Then:{
if
(button_pin || keypad == true);
digitalWrite (enable_pin,HIGH);
}else{
digitalWrite(enable_pin,LOW);

}

This not actually correct as it doesn't compile.
Try (button_pin==HIGH) | (keypad==HIGH)
WWC (author)  steveastrouk4 years ago
I can turn them on no problem its the turning them back off that it gets stuck. When i use digitalWrite(enable,HIGH); and digitalWrite(enable,LOW); for both the button and keypad what happens is the button will work correctly, but if the keypad is used the button will no longer work Then the key pad will work correctly and won't go back to the button again unless i reset to start the code again

Seams to me you are correct to use an or for them, but haven't been sucesfull in implementing that corroctly
Look carefully at what should happen if you KEEP the button pressed. It shouldn't do ANYTHING in that state, after activating the stepper.
WWC (author)  steveastrouk4 years ago
This:?

if(button_pinstate == LOW) {
digitalWrite(enable_pin,HIGH);
myStepper.step(pos);
} else{
myStepper.step(pos);
delay(2000);
myStepper.step(pos2);
digitalWrite(enable_pin,LOW);

Better like this:

if(button_pinstate == LOW);
digitalWrite(enable_pin,HIGH);
myStepper.step(pos);
delay(2000);
myStepper.step(pos2);
digitalWrite(enable_pin,LOW);
delay(10);

Better like this:
if(button_pinstate == LOW);
{ digitalWrite(enable_pin,HIGH);
myStepper.step(pos);
delay(2000);
myStepper.step(pos2);
digitalWrite(enable_pin,LOW);
delay(10);
while (button_pinstate==low);

}

//which is assuming that the code is updating button_pinstate in an //interrupt routine !!
//otherwise you need while (digitalread(button_pin)==LOW);
WWC (author)  steveastrouk4 years ago
Working but i owe you an apology , The enable line had an extra wire there that wasn't supposed to be, causing all the confusion. Enable really is as simple as digitalWrite(enable_pin,HIGH and digitalWrite(enable_pin,LOW).

Leason here is after checking the wireing 3 times check it 3 more. Then the next day check it again.
Seams thats 2 lunches i owe now.

Thanks