Keypad not working

Hey all,

I have the standard 12-button keypad from Sparkfun. It has been working with this code I found online, but now it just stopped working! i know it's in the code. Only the # and * buttons work, so I can't get past the main screen. I know it isn't a hardware problem.

/*
  loldialer - A dialer of lulz
  Copyright (c) 2011 David Huerta. Distributed under the CDL license: http://supertunaman.com/cdl/
 
  Inspired heavily by David A. Mellis's WebClient example and Alexander Brevig's Keypad library example.
*/

#include <SPI.h>
#include <Ethernet.h>
#include <Keypad.h>
#include <EthernetClient.h>
const byte ROWS = 4;
const byte COLS = 3;
const int DELAY_VAL = 2;
const int MAX_SHIFT_VAL = 1023;
const int SOFT_RESET_PIN = 12;

char keys[ROWS][COLS] = {
   {'1','2','3'}
  ,{'4','5','6'}
  ,{'7','8','9'}
  ,{'*','0','#'}
}; // map out the keys

// map out the pins
byte rowPins[ROWS] = {5,6,7,8};
byte colPins[COLS] = {2,3,4};

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

byte mac[] = {0x90,0xA2,0xDA,0x00,0x17,0x1A}; // Replace with your own MAC address
//byte ip[] = {172,16,0,77}; // replace with your own static IP
//byte gateway[] = {172,16,0,1}; // ...
//byte subnet[] = {255,255,252,0}; // ...
//byte ip[] = {192,168,34,190}; // casa de Giles/Huerta/Rix/Futurist Mike
//byte gateway[] = {192,168,34,1};
//byte subnet[] = {255,255,255,0};
byte ip[] = {192,168,2,2}; // OS X Cybersauce sharing
byte gateway[] = {192,168,2,1};
byte subnet[] = {255,255,255,0};
byte server[] = {192,168,2,1}; // replace with your web server address

// instantiate a network client
EthernetClient client;
int buttonState = 0; // soft reset button state
String phoneNumber = "";
String clip = "";

void setup()
{
  // start the serial library:
  Serial.begin(9600);
  // set display brightness
  Serial.write(0x7C);
  Serial.write(157);
  // set screen size in case LCD gets derpy...
  Serial.write(0xFE);
  Serial.write(6);
  Serial.write(0xFE);
  Serial.write(4);
  delay(4000);
  // start the Ethernet connection:
  Ethernet.begin(mac, ip, gateway, subnet);
  // set up soft reset button
  pinMode(SOFT_RESET_PIN, INPUT);
  ////attachInterrupt(0, softReset, CHANGE);
  // clear screen
  clearDisplay();
}

void loop()
{
  ////digitalWrite(softResetPin, LOW);
  // read the state of the reset button value:
  buttonState = digitalRead(SOFT_RESET_PIN);
  
  // check if the pushbutton is pressed (HIGH)
  if (buttonState == HIGH)
  {
    // reset softly
    softReset();
  }
  
  delay(70); // delay loop to keep the LCD from redrawing stuff too much
  
  char keyPressed = keypad.getKey(); // need to check press selection on keypad

  if (keyPressed)
  {
    if (keyPressed == '#') // # and * are reveresed for some reason
    {
      clearDisplay();
      selectFirstLine();
      Serial.print("THE GAME");
      softReset();
      delay(500);
    }
    else if (keyPressed == '*')
    {
      clearDisplay();
      selectFirstLine();
      Serial.print("i said wut wut");
      selectSecondLine();
      Serial.print("in the *");
      delay(1000);
    }
    else
    {
      // check for complete number
      if (phoneNumber.length() == 10)
      {
        selectSecondLine();
        // determine which sound clip to play
        switch (keyPressed) 
        {
        case '1':
          Serial.print("Rickroll");
          clip = "rickroll";
          break;
        case '2':
          Serial.print("Lavaroll");
          clip = "lavaroll";
          break;
        case '3':
          Serial.print("Keyboard Cat");
          clip = "keyboardcat";
          break;
        case '4':
          Serial.print("Pirate Song");
          clip = "piratesong";
          break;
        case '5':
          Serial.print("Fridayroll");
          clip = "fridayroll";
          break;
        case '6':
          Serial.print("Trololol");
          clip = "trololol";
          break;
        case '7':
          Serial.print("Banana Phone");
          clip = "bananaphone";
          break;
        case '8':
          Serial.print("Boxxy"); // OH GOD WHAT HAVE I DONE
          clip = "boxxy";
          break;
        case '9':
          Serial.print("Badger Badger");
          clip = "badgerbadgermushroom";
          break;
        }
        
        delay(3000);
      }
      else
      {
        phoneNumber = phoneNumber + keyPressed;
      }
    }
  }
  else
  {
    clearDisplay();
    if (phoneNumber.length() == 10)
    {
      if (clip == "")
      {
        selectFirstLine();
        Serial.print("Trolling option:");
      }
      else
      {
        selectFirstLine();
        Serial.print("Connecting...");
        // proceed to lulz
        if (client.connect(server, 80))
        {
          selectFirstLine();
          Serial.print("Connected.");
          // replace with your own cgi path...
          client.println("GET /~huertanix/cgi-bin/invoke_lulz.cgi?phone=" + phoneNumber + "&clip=" + clip + " HTTP/1.0");
          client.println();

          selectSecondLine();
          Serial.print("Great Success!");
          delay(5000);
          phoneNumber = "";
          clip = "";
        }
        else
        {
          selectSecondLine();
          Serial.print("Trying...");
        }
        // close connection to ensure proper re-connect
        client.stop();
      }
    }
    else
    {
      selectFirstLine();
      Serial.print("Phone number:");
      
      if (phoneNumber.length() > 0)
      {
        selectSecondLine();
        Serial.print(phoneNumber);
      }
    }
  }
}

void clearDisplay() {
  Serial.write(0xFE);
  Serial.write(0x01);
}

void selectFirstLine() {
  Serial.write(0xFE);
  Serial.write(128);
   //delay(10);
}

void selectSecondLine() {
  Serial.write(0xFE);
  Serial.write(192);
}

void softReset() {
  clearDisplay();
  phoneNumber = "";
  clip = "";
}

At what point in your project, did it work and then stop? Was it when you added the ethernet shield? Your using an ethernet shield, that use pins 10 - 13 and your button is connected to pin 12. Any problems there?

But that's not when it stopped working. How should I change the code?

Ok so when did it stop? It doesn't just stop when it wants to, you must have changed something. Try to use the serial monitor and do some debugging.

You really need to get rid of those delays, they are not good to use. Look into the blink without delay example.

Got it. It stops when I replace the Ethernet code on the original post with the following code.

//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x04, 0x00, 0x8D }; //physical mac address

char serverName[] = "api.tropo.com"; // zoomkat's test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /1.0/sessions?action=create&token=14b6200c7969ad49b03ad1f453b4001219cf646db2e68dde368e10430dab6ac6c0d18a7a90f859bb9b7d541f&number=6026884512 HTTP/1.0"); //download text
    client.println("Host: api.tropo.com");
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

It works without "NOKEY"? Strange. So what happens when you want to press 0? Also, I know someone is going to ask but why are you using Strings and not arrays?

I'm going to bed now, I have work in the morning. See ya in about 8 hours.

That code in reply #5 doesn't even reference the keypad. Of course the keypad doesn't work with that sketch.

@PaulS
I know the code in reply #5 has no reference to the keypad, thats what he took out of the code with the keypad. If you look at his first posting, there is no mention of NOKEY, but he claims it was working fine. Now you and I, both know without NOKEY the output will be constant zeros, and anything storing that data will be filled with just zeros.

OP, just start from the two sketches again, and make sure both work indepently and correctly, then try to put them together.

If you look at his first posting, there is no mention of NOKEY, but he claims it was working fine. Now you and I, both know without NOKEY the output will be constant zeros

I have to disagree:

  char keyPressed = keypad.getKey(); // need to check press selection on keypad

  if (keyPressed)
  {

While don't like this form, keyPressed has to have a value other than 0 (which is what NO_KEY is #defined to be) in order for the test to be true.

There is, however, no where near enough details about the problem in the first post, but there are opportunities to add debug statements to find out where the code starts failing.

OP, are you using a 328-based Arduino? If so, you may be running out of memory. Use the F() macro to keep the string constants out of SRAM:
Serial.print(F("THE GAME"));

While don't like this form, keyPressed has to have a value other than 0 (which is what NO_KEY is #defined to be) in order for the test to be true.

I overlooked that. Ok, so he will never be able to add a zero then, because it is looking for anything but zero.

OP look at the keypad code in this post, this code works great.
http://arduino.cc/forum/index.php/topic,137352.30.html

I overlooked that. Ok, so he will never be able to add a zero then, because it is looking for anything but zero.

The value returned by getKey() is a char. So, pressing the 0 button will return '0', which is not 0, so that will be dealt with just like a '1' or a '*'.

The value returned by getKey() is a char. So, pressing the 0 button will return '0', which is not 0, so that will be dealt with just like a '1' or a '*'.

Your right. I got to step back a bit and get my head straight. And maybe some coffee, I haven't had any yet.

PaulS:

If you look at his first posting, there is no mention of NOKEY, but he claims it was working fine. Now you and I, both know without NOKEY the output will be constant zeros

I have to disagree:

  char keyPressed = keypad.getKey(); // need to check press selection on keypad

if (keyPressed)
  {



While don't like this form, keyPressed has to have a value other than 0 (which is what NO_KEY is #defined to be) in order for the test to be true.

There is, however, no where near enough details about the problem in the first post, but there are opportunities to add debug statements to find out where the code starts failing.

OP, are you using a 328-based Arduino? If so, you may be running out of memory. Use the F() macro to keep the string constants out of SRAM:
Serial.print(<mark>F(</mark>"THE GAME"<mark>)</mark>);

Thanks so much for the help, both of you! I think I may be running out of memory as well, it's an Uno R3 with a 328. I will have to get back to you on how the F() works when I get home! Thanks!