# 4*4keypads to leds with PortManipulation

here is my question…
i use 4*4 keypads(0-7) which number as 0~f.
then output to 6 leds(8-13). For this, the last led(13) will light up (low power) whenever any keypads pressed. Others, will show as BIN from 0000 to 1111, depends on which keypads(number) i pressed.

First time, i use normal method to write these functions. All were right.
However, i change to PortManipulation, it gets wrong just like i write a PWM in. In other words, the leds can’t light up stable, when pressing keypads.

i use arduino UNO with Internship board.
any one can help me?

``````/*********************************************
Prupose: read the the status of 4X4 Keypads,
if button 0 is pressed, binary pattern  (01 1111B) is displayed,
if buttom 1 is pressed, binary pattern  (01 1110B) is displayed,
...
if buttom A is pressed, binary pattern  (01 0101B) is displayed,
...
if buttom F is pressed, binary pattern  (01 0000B) is displayed,
otherwise,  binary pattern  (00 1111B) is displayed.

*********************************************/

int ledPin[ ]  = { 8,9,10,11,12,13};  // binary digits
int scanPin[4] = { 0,1,2,3 };       // scan lines
int dataPin[4] = { 4,5,6,7 } ;     // data lines being fetched
int readValue;                       // number in relation to binary digits

int i,j;
int displayBinaryData (int dBvalue);

void setup()
{

DDRB=B11111111;
PORTB=B11111111;
for(i=0; i< 6; i++)
{  bitClear(PORTB,i);
delay(500);
}
PORTB=B11111111;

DDRD=B00001111;
}

void loop()
{  DDRD=B00001111;
DDRB=B11111111;
}

{  int value, value4LED=31;
int  data[4];
// the first row (scanline)
PORTD= B11111110;
value= PIND >>4;

switch(value)
{ case 14: value4LED=3;              break;
case 13: value4LED=2;              break;
case 11: value4LED=1;              break;
case 7:  value4LED=0;              break;
default: value4LED=31;
}

PORTD= B11111111;
if (value4LED!= 31)  return  value4LED;

// the second row (scanline)
PORTD= B11111101;
value= PIND >>4;

switch(value)
{ case 14: value4LED=7;              break;
case 13: value4LED=6;              break;
case 11: value4LED=5;              break;
case 7:  value4LED=4;              break;
default: value4LED=31;
}
PORTD=B11111111;
if (value4LED!= 31)  return  value4LED;

// the third row (scanline)
PORTD= B11111011;
value= PIND >>4;

switch(value)
{ case 14: value4LED=11;              break;
case 13: value4LED=10;              break;
case 11: value4LED=9;              break;
case 7:  value4LED=8;              break;
default: value4LED=31;
}
PORTD=B11111111;
if (value4LED!= 31)  return  value4LED;

// the fourth row (scanline)
PORTD= B11110111;
value= PIND >>4;
switch(value)
{ case 14: value4LED=15;              break;
case 13: value4LED=14;              break;
case 11: value4LED=13;              break;
case 7:  value4LED=12;              break;
default: value4LED=31;
}
PORTD=B11111111;
return  value4LED;
}

int   displayBinaryData (int value4LED)
{ switch (value4LED)
{ case 0:
PORTB=B11011111;
break;
case 1:
PORTB=B11011110;
break;
case 2:
PORTB=B11011101;
break;
case 3:
PORTB=B11011100;
break;
case 4:
PORTB=B11011011;
break;
case 5:
PORTB=B11011010;
break;
case 6:
PORTB=B11011001;
break;
case 7:
PORTB=B11011000;
break;
case 8:
PORTB=B11010111;
break;
case 9:
PORTB=B11010110;
break;
case 10:
PORTB=B11010101;
break;
case 11:
PORTB=B11010100;
break;
case 12:
PORTB=B11010011;
break;
case 13:
PORTB=B11010010;
break;
case 14:
PORTB=B11010001;
break;
case 15:
PORTB=B11010000;
break;
default:
PORTB=B11111111;
}
}
``````

Just use the code tags to show us your sketch rather than attaching an ino.

LarryD: Just use the code tags to show us your sketch rather than attaching an ino.

Here's code above.

Why have you used port manapulation when you clearly do not understand it at all. The setup function sets port B to be all ones, then it one at a time sets the first six bits to be zero and then sets all the bits to ones again. Clearly this is just plane crazy.

Have you been told to use port manapulation? Is this an assignment?

The only reasion to use port manapulation is for speed and as you are dealing with a keyboard that people press the speed increase is totally irrelevant.

Remember not all pins are capable of having a PWM signal and you can not set up PWM with port manapulation very easily.

Grumpy_Mike: Have you been told to use port manipulation? Is this an assignment?

November 2014 - some assignment then!

Paul__B: November 2014 - some assignment then!

No it just means that it is due shortly and he should have started it long ago. It was not uncommon to give assignments that could be done anytime during a course when I was at University ( both sides of the lectrum ).