Jump to content

Recommended Posts

Hello,

i want to control relays with my keypad. I work with the Launchpad TM4C123 80MHz.

i wrote a script but the relay isn´t working. The relay is alright and i don´t know the problem.

Can anyone help me?

Quote


#include <Keypad.h>
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {PF_3,PB_3,PC_4,PC_5}; //Rows 0 to 3
byte colPins[numCols]=  {PC_7,PD_6,PD_7,PF_4}; //Columns 0 to 3

//initializes an instance of the Keypad class
Keypad ourKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

int REL = PA_7;

void setup()
{
pinMode(REL,OUTPUT);
  Serial.begin(9600);
  Serial.begin(9600);//sets baud rate for serial communication
}
char lastKey=0; 
void loop() 
{char keypressed = ourKeypad.getKey();
  if (keypressed != NO_KEY);
  {
    Serial.print(keypressed);
  }
  //} These two line are your problem
  //{
  //char key = keypad.getKey(); // keypad was not declared anywhere, I assume it was meant to be this:
  
  char key = ourKeypad.getKey();
  if (int(key) == 1) {
    digitalWrite(REL, LOW);
    delay(1000);
    digitalWrite(REL, HIGH);
    delay(1000);
  }
}

 

Share this post


Link to post
Share on other sites

Can you point us to the Keypad library that you are using? In the following ship do you ever see the key pressed being printed to the Serial Monitor?

  if (keypressed != NO_KEY);
  {
    Serial.print(keypressed);
  }

I am not sure how the Keypad library works but I have a feeling that when you call outKeypad.getKey(), you are taking it out of it's internal buffer. If you only press the key ones every loop which is very well possible, the second call to ourKeypad.getKey() will most likely return NO_KEY and hence if statement that follows will never be executed.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...