loading

Arduino BUTTON_PIN?

I am having trouble with the BUTTON_PIN to work the servo. If i use the BUTTON_PIN sketch as a stand alone it will operate the servo. When i try to incorporate the button sketch into the keypad sketch i can not get the servo to operate.
The button is supposed to bypass the keypad and operate the servo. How could i get this to operate correctly?
I will post the library files if needed.  

Thanks
W




#include <Password.h>
#include <Keypad.h>
#include <Servo.h> //tells to use servo library


Servo myservo; //declares servo
Password password = Password( "4444" ); //password to unlock door, can be changed

const byte ROWS = 4; // Four rows
const byte COLS = 3; // columns
// 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.


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

unsigned long offtime;
boolean  helper_light_is_on = false; // helper light off
#define BUTTON_PIN        A0  // Button
#define DELAY            20  // Delay per loop in ms



boolean handle_button()
{
  int button_pressed = !digitalRead(BUTTON_PIN); // pin low -> pressed
  return button_pressed;
}

void setup()
{
  pinMode(BUTTON_PIN, INPUT);
  digitalWrite(BUTTON_PIN, HIGH); // pull-up
  Serial.begin(9600);
  Serial.print(254, BYTE);
  Serial.print(0x01, BYTE);
  delay(200);
  pinMode(10, OUTPUT);  //Helper light
  pinMode(11, OUTPUT);  //green light
  pinMode(12, OUTPUT);  //red light
  myservo.attach(9); //servo on digital pin 9 //servo
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}

void loop()
{
  // handle button
  boolean button_pressed = handle_button();

  // do other things
  Serial.print(button_pressed ? "^" : ".");

  // add newline sometimes
  static int counter = 0;
  if ((++counter & 0x3f) == 0)
    Serial.println();

  delay(DELAY);


  keypad.getKey();
  myservo.write(0);
  process_helper_light();
}

//take care of some special events
void keypadEvent(KeypadEvent eKey)
{
  switch (keypad.getState())
  {
    case PRESSED:
      // a key is pressed so light the helper light
      helper_light_is_on = true;
      digitalWrite(10,HIGH);
      offtime = millis() + 5000;  // set the offtime for 30 seconds in the future
     
      Serial.print(" enter: ");
      Serial.println(eKey);
      delay(DELAY);
      Serial.print(254, BYTE);
      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.print(254, BYTE);
    delay(DELAY);
    //Add code to run if it works
    myservo.write(90); //160deg
    digitalWrite(11, HIGH);//turn on
    delay(2000); //wait 5 seconds
    digitalWrite(11, LOW);// turn off
  }
  else
  {
    Serial.println(" Denied"); //if passwords wrong keep door locked
    Serial.print(254, BYTE);
    delay(DELAY);
    //add code to run if it did not work
    myservo.write(0);
    digitalWrite(12, HIGH); //turn on
    delay(2000); //wait 5 seconds
    digitalWrite(12, LOW);//turn off
  }
   if (handle_button == LOW)
  {
    myservo.write(90); //160deg
    delay(DELAY); //wait 5 seconds
     }
  else
  {
    myservo.write(0);
    delay(DELAY); //wait 5 seconds
   }
}

// this routine turns off the light when the timer expires
void process_helper_light(void)
{
  if (helper_light_is_on)
  {
    if (millis() >= offtime)
    {
      digitalWrite(10,LOW);  //turn off the helper light
      helper_light_is_on = false;
    }
  }
}

sort by: active | newest | oldest
In this code:

boolean button_pressed = handle_button(); // do other things Serial.print(button_pressed ? "^" : "."); // add newline sometimes
static int counter = 0;
if ((++counter & 0x3f) == 0)
     Serial.println();
delay(DELAY);
keypad.getKey();
myservo.write(0);
process_helper_light();


...when do you actually use the state of button pressed to do anything ?
WWC (author)  steveastrouk4 years ago
Yes It shows in the serial monitor if the button is being read or not with the serial print and is supposed to work with my IF ELSE to operate the servo
So it has to wait for you to hold the pass button WHILE you press a " * " key on the keypad ?
WWC (author)  steveastrouk4 years ago
Should / supposed to be able to use the button independent of the keypad. The * is used after the code is entered to rotate the servo.
That's what I thought you were trying to do, but the only time it checks password is after a * is pressed. on the keypad.

How about forcing "check password" when the button is pressed in your main loop ?
WWC (author)  steveastrouk4 years ago
I need another hint on that.

Doesn't the code follow the IF ELSE statements until one of the conditions are met?

BTW I revisited my stepper boards and changed some things, reference voltage, voltage sensing values and pulled everything else high and have been able to get performance that i never could before.
L297 L298 seem to be very sensitive to these small changes, better to use small pliers instead of a big hammer.
Yes, but the if else is INSIDE the pressed statement, so it only gets acted on when there's a press.
WWC (author)  steveastrouk4 years ago
ah, So i can change that to have the second if else separated? With a break or what?
It strikes me it may as well be in the main loop.
if button_pressed check_password();
WWC (author)  steveastrouk4 years ago
Ok i am going to work on this
Don't forget to add a while button_pressed ; //do nothing AFTER the button is pressed.

SO.

If (button pressed) ==true
{check_password();
while button_pressed ;};




WWC (author)  steveastrouk4 years ago
So i should replace my if (handle_button == LOW)

With yours?
That code was synactically wrong, this looks better.

if (handle_button==LOW)
{check_password();
while (handle_button==LOW) ; };

? Yes.
WWC (author)  steveastrouk4 years ago
This may have fixed it i can't tell. Ever scene i put this button pin in the keypad sketch it throws an error on the process helper light. If i remove all the button pin stuff then no error on process helper light.

So i am kinda chasing my tail