#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    The code not working propley any help


    did any one help me with this code i have problem with c code
    when i hit the ' # ' key must swicth on beep and when i hit ' * ' switched off could any one tell me how to do it or fix for me my code and check whats wrong with it



    ============================================================
    Code:
    #include <LiquidCrystal.h>
    #include <Keypad.h>
    
    // Define the Keymap
    const byte ROWS = 4; //four rows
    const byte COLS = 3; //three columns
    char keys[ROWS][COLS] = {
      {'1','2','3'},
      {'4','5','6'},
      {'7','8','9'},
      {'*','0','#'}
    };
    byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
    byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
    Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
    
    // initialize the library with the numbers of the interface pins
    LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
    int ledpin = 13;
    void setup()  { 
      // declare pin 9 to be an output:
       pinMode(ledpin,OUTPUT);
      pinMode(10,OUTPUT);
      lcd.begin(16, 2);
      // Print a message to the LCD.
     //
      pinMode(9, OUTPUT);
      beep(50);
      beep(50);
      beep(50);
      delay(1000);
      lcd.begin(16, 2);
      Serial.begin(9600);
    
    } 
    
    void loop()  { 
      beep(200);
      lcd.setCursor(0, 1);
      // print the number of seconds since reset:
    
    
       char key = kpd.getKey();
       if(key)  // Check for a valid key.
      {
        switch (key)
        {
          case '*':
         
            digitalWrite(ledpin, LOW);
            delay(100);
            digitalWrite(10,HIGH);
             lcd.print("Alaram OFF");
             
           
            break;
          case '#':
         
            digitalWrite(ledpin, HIGH);
            digitalWrite(10,LOW);
            lcd.print(" Alaram ON ");
    
            break;
            default:
            Serial.println(key);
            
    }
      }
    }
    
    void beep(unsigned char delayms){
      analogWrite(9, 20);      // Almost any value can be used except 0 and 255
                               // experiment to get the best tone
      delay(delayms);          // wait for a delayms ms
      analogWrite(9, 0);       // 0 turns it off
      delay(delayms);          // wait for a delayms ms   
      }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,381
    Rep Power
    1871
    Please edit your post and put [code][/code] tags around your code.
    Copy again from your code editor so indentation is preserved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0
    i do it now could help me in this code
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,381
    Rep Power
    1871
    So what calls loop() then?

    Say
    Code:
    int main ( ) {
        setup();
        while ( 1 ) {
            loop();
        }
    }
    > Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
    > // initialize the library with the numbers of the interface pins
    > LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
    Are you writing C++ code?
    Because these kinds of things are not valid C code.

    Do you want it to beep constantly while # is in effect?
    Use a state variable.
    Code:
    int beepState = 0;
    
    ...
        switch (key)
        {
          case '*':
             beepState = 0;
            digitalWrite(ledpin, LOW);
            delay(100);
            digitalWrite(10,HIGH);
             lcd.print("Alaram OFF");
             
           
            break;
          case '#':
            beepState = 1;
            digitalWrite(ledpin, HIGH);
            digitalWrite(10,LOW);
            lcd.print(" Alaram ON ");
    
            break;
            default:
            Serial.println(key);
            
    }
    
    if ( beepState ) beep();
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo