364Views14Replies

Author Options:

Arduino BUTTON_PIN? Answered

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;
    }
  }
}

Discussions

0
steveastrouk
steveastrouk

7 years ago

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 ?

0
WWC
WWC

Answer 7 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

0
steveastrouk
steveastrouk

Answer 7 years ago

So it has to wait for you to hold the pass button WHILE you press a " * " key on the keypad ?

0
WWC
WWC

Answer 7 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.