Arduino, keypad input to array

Hello,

I am looking for some help with a project, my project consists of Sparkfun Large Digit Drivers X 3, three large 7 segmentsLED’s and a keypad, I have successfully placed a keypad and been able to display the input to some degree via the keypad.

however, I have hit a wall, well somewhat, the issue is that I am unable to display the desired number I wish without some complications, also meddling with the code to achieve this still produces mix results.

Things I Tried:

  1. I tried arrays but having trouble understanding how to hold the value after it has been changed from ascii to digits, then output the total amount.

  2. reduced a for loop value, it will input the whole number but will not refresh the digits.

  3. reset the program using asm volatile (" jmp 0 ");, LED values do not reset.

  4. when placing the original value in the loop, it sets all values to 0, so perfect but I can only display the input to one LED.

My thoughts is that I need an array to hold two or three digit value, then output.

I’ll paste the code here, I have made a bit of a mess of it, but in my defence, I was trying to read solutions and then implementing them the best of my limited skill and knowladge.

#include <Key.h>
#include <Keypad.h>
#include <stdlib.h>
#include <stdio.h>

/*

Controlling large 7-segment displays
By: Nathan Seidle
SparkFun Electronics
Date: February 25th, 2015
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

This code demonstrates how to post two numbers to a 2-digit display usings two large digit driver boards.

Here’s how to hook up the Arduino pins to the Large Digit Driver IN

Arduino pin 6 -> CLK (Green on the 6-pin cable)
5 -> LAT (Blue)
7 -> SER on the IN side (Yellow)
5V -> 5V (Orange)
Power Arduino with 12V and connect to Vin -> 12V (Red)
GND -> GND (Black)

There are two connectors on the Large Digit Driver. ‘IN’ is the input side that should be connected to
your microcontroller (the Arduino). ‘OUT’ is the output side that should be connected to the ‘IN’ of addtional
digits.

Each display will use about 150mA with all segments and decimal point on.

*/
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 3; //number of columns on the keypad

char keymap[numRows][numCols]=
{
{‘1’, ‘2’, ‘3’},
{‘4’, ‘5’, ‘6’},
{‘7’, ‘8’, ‘9’},
{’*’, ‘0’, ‘#’}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {7,6,5,4}; //Rows 0 to 3
byte colPins[numCols]= {3,2,9}; //Columns 0 to 3

Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
//GPIO declarations
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
byte statLED = 13; //On board status LED
byte segmentClock = 11;
byte segmentLatch = 10;
byte segmentData = 12;

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

void setup()
{

Serial.begin(9600);
Serial.println(“Large Digit Driver Example”);

pinMode(segmentClock, OUTPUT);
pinMode(segmentData, OUTPUT);
pinMode(segmentLatch, OUTPUT);

pinMode(13, OUTPUT);

digitalWrite(segmentClock, LOW);
digitalWrite(segmentData, LOW);
digitalWrite(segmentLatch, LOW);

}

int a = 0;
int c;
int num = 0;
int d[3]={0,0,0};

int j;

void loop()
{

char keypressed = myKeypad.getKey();
a = keypressed;

if (keypressed != NO_KEY )
{
c = a -‘0’; // changes ascii to decimal value.

for (j = 0; j < 2 ; j++) 
{
  scanf("%d", &c);
  d[j] = c;
  
}
Serial.println(d[j]); 
 // showPvalue(c);

}

showPvalue©;

//diagnostic use

// Serial.println((String)"Cycle: " + c + " /keypressed " + keypressed); // prints the output of two variables in one line.
/

delay (50);

}
void digitInput()
{

}
//void resetCounter()
//{
// asm volatile (" jmp 0 ");
//}

//Takes a number and displays 2 numbers. Displays absolute value (no negatives)
void showPvalue(float value)
{
int number = abs(value); //Remove negative signs and any decimals
int b;
b = value;

for (byte x = 0 ; x < 3 ; x++)
{
int remainder = b % 10;

postNumber(remainder, false);

b /= 10;

}

//Latch the current segment data
digitalWrite(segmentLatch, LOW);
digitalWrite(segmentLatch, HIGH); //Register moves storage register on the rising edge of RCK

}

//Given a number, or ‘-’, shifts it out to the display
void postNumber(byte number, boolean decimal)
{

// - A
// / / F/B
// - G
// / / E/C
// -. D/DP

#define a 1<<0
#define b 1<<6
#define c 1<<5
#define d 1<<4
#define e 1<<3
#define f 1<<1
#define g 1<<2
#define dp 1<<7

byte segments;

switch (number)
{
case 1: segments = b | c; break;
case 2: segments = a | b | d | e | g; break;
case 3: segments = a | b | c | d | g; break;
case 4: segments = f | g | b | c; break;
case 5: segments = a | f | g | c | d; break;
case 6: segments = a | f | g | e | c | d; break;
case 7: segments = a | b | c; break;
case 8: segments = a | b | c | d | e | f | g; break;
case 9: segments = a | b | c | d | f | g; break;
case 0: segments = a | b | c | d | e | f; break;
case ’ ': segments = 0; break;
case ‘c’: segments = g | e | d; break;
case ‘-’: segments = g; break;
case ‘*’: segments = b | c | e | f | g; break;
}

if (decimal) segments |= dp;

//Clock these bits out to the drivers

for (byte x = 0 ; x < 8 ; x++)
{

digitalWrite(segmentClock, LOW);
digitalWrite(segmentData, segments & 1 << (7 - x));
digitalWrite(segmentClock, HIGH); //Data transfers to the register on the rising edge of SRCK

int z;
if (z == 3)
{
digitalWrite(segmentClock, LOW);
digitalWrite(segmentData, segments & 0 << (7 - x));
digitalWrite(segmentClock, HIGH); //Data transfers to the register on the rising edge of SRCK
}

}

}

Thank you in advanced for any help.

I’m not 100% sure on what you are trying to do. But I assume display a 3 digit number to the7segs.
To start with I would recommend changing your variable names (use meaningful variable names), because I think that your #define a,b,c… will be clashing with int a,b,c,d which will probably give you some weird results.

Hello Clinton,

Thank you for your response I have successfully achieved it, I used switches and if conditions to output to the drivers. cheers :slight_smile:

Awesome :smile: Great to hear you got it working! I’d be interested to see the modifications you made to the code to get it working.

Here is my simple attempt,
// Libraries
#include <TM1637.h>
#include <TM1637Display.h>
#include <Key.h>
#include <Keypad.h>
#include <stdlib.h>
#include <stdio.h>

//Initialisation of pins for Small LED
#define CLK 24
#define DIO 22

//Initialisation of Small LED chip driver(TM1637).
TM1637 tm1637(CLK, DIO);

/*
Controlling large 7-segment displays
By: Nathan Seidle
SparkFun Electronics
Date: February 25th, 2015
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

This code demonstrates how to post two numbers to a 2-digit display usings two large digit driver boards.

Here’s how to hook up the Arduino pins to the Large Digit Driver IN

Arduino pin 6 -> CLK (Green on the 6-pin cable)
5 -> LAT (Blue)
7 -> SER on the IN side (Yellow)
5V -> 5V (Orange)
Power Arduino with 12V and connect to Vin -> 12V (Red)
GND -> GND (Black)

There are two connectors on the Large Digit Driver. ‘IN’ is the input side that should be connected to
your microcontroller (the Arduino). ‘OUT’ is the output side that should be connected to the ‘IN’ of addtional
digits.

Each display will use about 150mA with all segments and decimal point on.

*/

/*

  • Original code by: Nathan Seidle, (SparkFun Electronics)
  • Date: February 25th, 2015
  • Modified by: Fr Theodore St Shenouda & Amir Ashamalla
  • Date: February 3rd, 2019;
  • Purpose: Page Number Display / Manual digit output via keypad.
  • Licence remains the same except no need for the beer, well sort of. LOL
    */

//Keypad matrix assignment
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 3; //number of columns on the keypad

char keymap[numRows][numCols]=
{
{‘1’, ‘2’, ‘3’},
{‘4’, ‘5’, ‘6’},
{‘7’, ‘8’, ‘9’},
{’*’, ‘0’, ‘#’}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {7,6,5,4}; //Rows 0 to 3
byte colPins[numCols]= {3,2,9}; //Columns 0 to 3

// Keypad Initialisation
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

//GPIO declarations
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
byte statLED = 13; //On board status LED
byte segmentClock = 11;
byte segmentLatch = 10;
byte segmentData = 12;
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

// Data Types
int num = 0;
int d = 0;

// Used for Array conversion from ascii to digit value.
int c = 0;

//KeyPad
char keypressed;
// carries value of Keypressed
int a = 0;

// Large LED Segment Modules / integer & Array
int j = 0;
int arrayOfThree[] = {0,0,0};

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

void setup()
{
tm1637.init();
tm1637.set(BRIGHT_TYPICAL);
//TM1637Display display(CLK, DIO);
Serial.begin(9600);
Serial.println(“Large Digit Driver Example”);

pinMode(segmentClock, OUTPUT);
pinMode(segmentData, OUTPUT);
pinMode(segmentLatch, OUTPUT);

pinMode(13, OUTPUT);

digitalWrite(segmentClock, LOW);
digitalWrite(segmentData, LOW);
digitalWrite(segmentLatch, LOW);

}

void loop()
{

keypressed = myKeypad.getKey();
a = keypressed;
d = a -‘0’;

if (keypressed != NO_KEY)
{

Serial.println ("Please Enter Number ");

// Large LED Array Input & Cancatanation ouput & Small LED Display
c = a -‘0’; // changes ascii to decimal value.

if (keypressed != '#') //# being used as enter key.
{
switch (j)
{
  case 0: arrayOfThree[0] = c;
  break; 
  case 1: arrayOfThree[1] = c;
  break;
  case 2: arrayOfThree[2] = c;
  break;
}
}

// Condition, String output to Large LED. (# is being used as the enter key)
if(keypressed == ‘#’)
{
switch (j)
{
case 1:
arrayOfThree[2] = arrayOfThree[0];
arrayOfThree[1] = 0;
arrayOfThree[0] = 0;
j = 3;
break;
case 2:
arrayOfThree[2] = arrayOfThree[1];
arrayOfThree[1] = arrayOfThree[0];
arrayOfThree[0] = 0;
j = 3;
break;
case 3:
j = 3;
break;

}

}
// Small LED Display
tm1637.display(1,arrayOfThree[0]);
tm1637.display(2,arrayOfThree[1]);
tm1637.display(3,arrayOfThree[2]);

// condition that will run if more than 3 values entered, superseded by the # key (Used as Enter key)
if ( j == 3 )
{
char myVal[3];
sprintf(myVal,"%d%d%d", arrayOfThree[0],arrayOfThree[1],arrayOfThree[2]);
String string3(myVal);

Serial.println(string3);
int val = 0;
val = string3.toInt();

//Output to LED Driver.    
showPvalue(val);

j = 0;
arrayOfThree[0] = 0 ;
arrayOfThree[1] = 0 ;
arrayOfThree[2] = 0 ;

}

else

{
j++;

}

}

}

//Takes a number and displays 2 numbers. Displays absolute value (no negatives)
void showPvalue(float value)
{
int number = abs(value); //Remove negative signs and any decimals
int b;
b = value;

for (byte x = 0 ; x < 3 ; x++)
{
int remainder = b % 10;

postNumber(remainder, false);

b /= 10;

}

//Latch the current segment data
digitalWrite(segmentLatch, LOW);
digitalWrite(segmentLatch, HIGH); //Register moves storage register on the rising edge of RCK

}

//Given a number, or ‘-’, shifts it out to the display
void postNumber(byte number, boolean decimal)
{

// - A
// / / F/B
// - G
// / / E/C
// -. D/DP

#define a 1<<0
#define b 1<<6
#define c 1<<5
#define d 1<<4
#define e 1<<3
#define f 1<<1
#define g 1<<2
#define dp 1<<7

byte segments;

switch (number)
{
case 1: segments = b | c; break;
case 2: segments = a | b | d | e | g; break;
case 3: segments = a | b | c | d | g; break;
case 4: segments = f | g | b | c; break;
case 5: segments = a | f | g | c | d; break;
case 6: segments = a | f | g | e | c | d; break;
case 7: segments = a | b | c; break;
case 8: segments = a | b | c | d | e | f | g; break;
case 9: segments = a | b | c | d | f | g; break;
case 0: segments = a | b | c | d | e | f; break;
case ’ ': segments = 0; break;
case ‘c’: segments = g | e | d; break;
case ‘-’: segments = g; break;
case ‘*’: segments = g; break;

}

if (decimal) segments |= dp;

//Clock these bits out to the drivers

for (byte x = 0 ; x < 8 ; x++)
{

digitalWrite(segmentClock, LOW);
digitalWrite(segmentData, segments & 1 << (7 - x));
digitalWrite(segmentClock, HIGH); //Data transfers to the register on the rising edge of SRCK

}

}

1 Like

ignore some functions, I still need to clean up some of the code.