7segment cap?

ok, i'm using a 4x4 keypad to control outputs on a 7 segment diode, but i can only make it show 1,2 and 3. if i try to push buttons above that, the 7seg goes crazy .

heres my code:

//http://www.arduino.cc/playground/Code/Keypad
#include <Keypad.h> // Download Keypad.zip

//define the cymbols on the buttons of the keypads
char hexaKeys[4][4] = {
 'A', 'B', 'C', 'D',
 'E', 'F', 'G', 'H',
 'I', 'J', 'K', 'L',
 'M', 'N', 'O', 'P'
};
byte rows = 4; //four rows
byte cols = 4; //four columns
byte rowPins[] = {2,3,4,5}; //connect to the row pinouts of the keypad
byte colPins[] = {6,7,8,9}; //connect to the column pinouts of the keypad


Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, rows, cols);

void setup(){
 Serial.begin(9600);
 pinMode(32, OUTPUT); 
  pinMode(33, OUTPUT);
  pinMode(34, OUTPUT);
  pinMode(35, OUTPUT);
  pinMode(36, OUTPUT);
  pinMode(37, OUTPUT);
  pinMode(38, OUTPUT);
  pinMode(39, OUTPUT);
  digitalWrite(39, 1);
}
 
void loop(){
 char key = keypad.getKey();
 
 if (key != NO_KEY) {
   key -= 'A'-1;
   Serial.println(key,DEC);
   if (key == 1){ // 1
digitalWrite(32, 1);
 digitalWrite(33, 0);
 digitalWrite(34, 0);
 digitalWrite(35, 1);
 digitalWrite(36, 1);
 digitalWrite(37, 1);
 digitalWrite(38, 1);
   }
 if (key == 2){ // 2
  digitalWrite(32, 0);
 digitalWrite(33, 0);
 digitalWrite(34, 1);
 digitalWrite(35, 0);
 digitalWrite(36, 0);
 digitalWrite(37, 1);
 digitalWrite(38, 0);
 }
 if (key == 3) { // 3
  digitalWrite(32, 0);
 digitalWrite(33, 0);
 digitalWrite(34, 0);
 digitalWrite(35, 0);
 digitalWrite(36, 1);
 digitalWrite(37, 1);
 digitalWrite(38, 0);
 }
 if (key == 5) { //4
    digitalWrite(2, 1);
 digitalWrite(3, 0);
 digitalWrite(4, 0);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 digitalWrite(7, 0);
 digitalWrite(8, 0);
 }
 if (key == 6) { //5
   digitalWrite(2, 0);
 digitalWrite(3, 1);
 digitalWrite(4, 0);
 digitalWrite(5, 0);
 digitalWrite(6, 1);
 digitalWrite(7, 0);
 digitalWrite(8, 0);
 }
 if (key == 7) { //6
   digitalWrite(2, 0);
 digitalWrite(3, 1);
 digitalWrite(4, 0);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 digitalWrite(7, 0);
 digitalWrite(8, 0);
 }
 if (key == 9) { // 7
   digitalWrite(2, 0);
 digitalWrite(3, 0);
 digitalWrite(4, 0);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 digitalWrite(7, 1);
 digitalWrite(8, 1);
 }
 if (key == 10) { // 8
   digitalWrite(2, 0);
 digitalWrite(3, 0);
 digitalWrite(4, 0);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 digitalWrite(7, 0);
 digitalWrite(8, 0);
 }
 if (key == 11) { // 9
    digitalWrite(2, 0);
 digitalWrite(3, 0);
 digitalWrite(4, 0);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 digitalWrite(7, 0);
 digitalWrite(8, 0);
 }
 }
}

i'm not sure what i'm doing wrong because it compiles just fine. :confused:

Could it be that beyond 3, you appear to use a different set of pins?

i'm not sure what i'm doing wrong because it compiles just fine

The compiler has no way of knowing what you write is sensible or possible, just that it is syntactically correct.

I use all the same pins for all the numbers, just turn on and turn off commands. and i'm litteraly clueless how my 1, 2 and 3 works just fine and rest doesent :confused:

 if (key == 3) { // 3
  digitalWrite(32, 0);
 digitalWrite(33, 0);
 if (key == 5) { //4
    digitalWrite(2, 1);
 digitalWrite(3, 0);
 digitalWrite(4, 0);

I use all the same pins for all the numbers

Right.

omg lol, i can't believe i made that fucking mistake AGAIN. thanks for pointing it out :slight_smile:

works like a charm :b

It could be considered overkill in such a short sketch, but any kind of human repetition (even if simply cut-and-paste) is prone to hard-to-spot bugs.
If you only ever reference the segment pins once, your code would be simpler to write and debug.
So, for instance, the digit patterns for a seven segment digit can be encoded in a single byte (with space left for a decimal point!), with one pit per segment.

A first stab at simplification could be like:

void displayDigit (byte pattern)
{
  for (int i = 0; i < 7;++i ) {
    digitalWrite (segmentPin [i], bitRead (pattern, i));
  }
}

all you need to do is set up the array "segmentPin", and define your digit patterns in a separate "byte" array.

This approach also has the advantage that if you decide to swap common anode for common cathode displays, you need change only one line!