programming problems

why these code didn’t display keypad
#include <Keypad.h>
#include <Password.h>
Password password = Password( “1234” );
const int button = 13;
const int alarm = 12;
const int led_alarm = 11;
const int led_pass = 10;
int b=1;
//const int del_time = A0;
int val_b;
int val_a;
//int val_d;
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] =
{
{’#’,’*’,‘0’},
{‘1’,‘4’,‘7’},
{‘2’,‘5’,‘8’},
{‘3’,‘6’,‘9’}
};
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {8, 7, 6};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
pinMode(button, INPUT);
pinMode(alarm, INPUT);
pinMode(led_alarm, OUTPUT);
pinMode(led_pass, OUTPUT);
Serial.begin(9600);
keypad.addEventListener(keypadEvent);
}
void loop()
{
keypad.getKey();
void keypadEvent();

val_b= digitalRead(button);
val_a= digitalRead(alarm);
// val_d= analogRead(del_time);

if (val_a == LOW)
{
delay (5000);
if (b == 1){
digitalWrite (led_alarm, HIGH);
delay (5000);
}
else if (b == 0){
digitalWrite (led_alarm, LOW);
}
}
else if (val_a == HIGH){
digitalWrite (led_alarm, LOW);
}
}

//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
Serial.print("Pressed: ");
Serial.println(eKey);
switch (eKey){
case ‘5’: checkPassword(); break;
case ‘6’: password.reset(); break;
default: password.append(eKey);
}
}
}

void checkPassword(){
if (password.evaluate()){
Serial.println(“Success”);
digitalWrite(led_pass, HIGH);
if (val_b == HIGH){
b=1;
}
else {
b=0;
}

delay(2000);
}else{
Serial.println(“Wrong”);
digitalWrite(led_pass, LOW);
b=1 ;

}
}

but the same code with outing all from void loop and let void loop(){
keypad.getKey();

} display keypad but doesn't display othe programing steps :slightly_frowning_face:

You can't use Serial.print within an interrupt service routine. I'd suggest you save the details of which key has been pressed THEN in your loop function, keep a check for this value changing and print it from there instead.

if (val_b == HIGH){
      b=1;
      }
      else {
        b=0;
      }

In spite of the piss-poor indentation, and multiple lines of code, this is doing nothing more than

b = val_b;

Why do you need 6 lines to do that?

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the # button above the posting area.

How to use this forum

PaulS:
Why do you need 6 lines to do that?

At last! You’re starting to think like me :wink:

i change keypad commands put still have problem that keypad ddin’t work when do command delay(5000)

void loop() 
{
  valb= digitalRead(button);
  vala= digitalRead(alarm);
  if (vala == LOW){


    if (valb == HIGH){
      digitalWrite(led_alarm,LOW);
      //delay (5000);
      digitalWrite(led_alarm,HIGH);
      //delay (5000);
    }
  }
  else if (vala == HIGH){
    digitalWrite (led_alarm,LOW);
  }
  if (valb == LOW){
    digitalWrite(led_alarm,LOW);
  }
  char key = keypad.getKey();
  if (key == '9') {
    position = 0;
    setLocked(true);
  }

  if (key == secretCode[position]) {
    position++;
  }

  if (position == 3) {
    setLocked(false);
  }
  delay(50);

}

void setLocked(int locked)
{
  if (locked) {
    digitalWrite(led_pass, LOW);

  }
  else {
    digitalWrite(led_pass,HIGH);
  }

}

Moderator edit: [code][/code] tags added. (Nick Gammon)

Also ran through auto-formatter.

On my system it hasn’t looked like a # button for a few days now. When I use a magnifying glass it looks like a scroll with <> on top of it - 2nd from right on the top line and just left or the quote bubble.
I wonder if the HOW TO … needs updating ?

Or maybe someone could change it back to a # button to make it easy to explain to people.

…R

If you are so arrogant as to ignore our requests for you to post your code between code tags what makes you think you deserve an answer?

If you have failed to understand the request what makes you think you will understand any answer we give?

Robin2:
Or maybe someone could change it back to a # button to make it easy to explain to people.

I raised this issue with the forum admins.


@ahmedhesham: For the second time:

Please edit your post, select the code, and put it between [code][/code] tags.

Maybe read this, it’s a good read: How to use this forum

I hate to tell you this Nick, but that’s no longer possible in this forum. He needs to put it in a new post. :frowning:

I hate to tell you this Nick ...

You can tell me, I'm a doctor moderator!

I'm editing my posts, why can't he/she?

In any case, the OP has made more than one post with code in it, so my earlier request was ignored.

Maybe I should just lock the thread?

[quote author=Nick Gammon link=msg=1935634 date=1414284967]
I'm editing my posts, why can't he/she?
[/quote]Because he's NOT a moderator :slight_smile: but I only brought this issue up as an asside.

I'm not defending his blatant disregard of your perfectly reasonable request. By now we should have some code on the page within the appropriate tags. For that he has no excuse.

Because he's NOT a moderator

Raised a bug report about that.

PaulS:

if (val_b == HIGH){

b=1;
     }
     else {
       b=0;
     }



In spite of the piss-poor indentation, and multiple lines of code, this is doing nothing more than


b = val_b;



Why do you need 6 lines to do that?

::) :stuck_out_tongue:

I am sorry for misunderstanding , i hope i do the rules through this post
delay command make a problem with keypad displaying

void loop() 
{
  valb= digitalRead(button);
  vala= digitalRead(alarm);
  if (vala == LOW){


    if (valb == HIGH){
      digitalWrite(led_alarm,LOW);
      //delay (5000);
      digitalWrite(led_alarm,HIGH);
      //delay (5000);
    }
  }

else if (vala == HIGH){
digitalWrite (led_alarm,LOW);
}
if (valb == LOW){
digitalWrite(led_alarm,LOW);
}
char key = keypad.getKey();
if (key == '9') {
position = 0;
setLocked(true);
}

if (key == secretCode[position]) {
position++;
}

if (position == 3) {
setLocked(false);
}
delay(50);

}

void setLocked(int locked)
{
if (locked) {
digitalWrite(led_pass, LOW);

}
else {
digitalWrite(led_pass,HIGH);
}

}[/code]

PaulS:

if (val_b == HIGH){

b=1;
      }
      else {
        b=0;
      }



In spite of the piss-poor indentation, and multiple lines of code, this is doing nothing more than


b = val_b;



Why do you need 6 lines to do that?

thanks for your help i change it :slight_smile:

Moderator edit: [code][/code] tags added. (Nick Gammon)
[/quote]

If anyone wants to see how Nick made that, quote this post and see.

You don’t need edit window buttons to make code tags, you can type them in by yourself.

The rules are that the tag word has to be in brackets to start the mode and the same word with / in front in brackets to end the mode.

There’s a tag to turn tags off but I forget! With that one I could type what is needed in the clear.

There's a tag to turn tags off but I forget! With that one I could type what is needed in the clear.

Would that be nobbc?

 [code] foo bar [/code]

i hope i do the rules through this post

Look at the post, you failed. So I don't know if you are going to understand this.

delay command make a problem with keypad displaying

What delay is it the ones commented out here?

digitalWrite(led_alarm,LOW);
      //delay (5000);
      digitalWrite(led_alarm,HIGH);
      //delay (5000);

If so it will not stop anything from displaying. What it will do is give a 10 seconds delay before anything else does happen.

This is because the delay stops the processor doing anything else.
The way round this is to use the technique in the blink without delay example found in the examples part of the Arduino IDE.
Basically when you turn on the led_alarm you make a note of the time it has turned on by putting the millis() timer into a variable. Then at the start of the loop function you check if the current value of the millis timer minus the time you turned it on is greater than the interval you want it on. If so you turn the led_alarm off.

This sort of thing is called a state machine, for further reading see:-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html