8-digit-7-segment-LED displays plus 16 keys

This module is ideal for an old-fashioned pocket calculator.

All you need is the library TM1638QYF.h - and that's it.

There are three lines to connect: Dio, Strobe, and Clock. You can connect them either to the SPI pins or whatever you want.

Well, there is something you need: there is no way to fix labels to the keys of the QYF module. Print something like this and remove the green rectangles.

Well 16 keys are way too less? One solution might be declaring one of the keys to a shift key.


#include <TM1638QYF.h>

#define USE_SPI

#ifdef USE_SPI
const byte PIN_STROBE = 11; 
const byte PIN_DATA   = 12; 
const byte PIN_CLOCK  = 13; 
#else
const byte PIN_STROBE =  7;
const byte PIN_DATA   =  8;
const byte PIN_CLOCK  =  9;
#endif

TM1638QYF module(PIN_DATA, PIN_CLOCK, PIN_STROBE);

String operand[2];
byte status = 0;
char operation = ' ';
float result = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("\n" __FILE__);
  module.setupDisplay(true, 7);
  module.setDisplayToString("CALCPLUS");
  reset();
}

void loop() {
  char key = getKey();
  // wenn KEINE Taste: zurueck
  if (!key) return;
  // wenn TASTE, dann warte bis Taste losgelassen
  do {} while (getKey());
  Serial.println(key);
  String s;
  switch (key) {
    case '0' ... '9': // not standard C
    case '.': operand[status] = operand[status] + key;
      print(operand[status]);
      break;
    case '/':
    case 'x':
    case '-':
    case '+': operation = key; status = 1; break;
    case '=': calculate(); break;
  }
}

void print(String s) {
  String t = "        ";
  byte dots = 0;
  for (int i = 0, j = 0, k = 0x100; i < s.length(); i++, k = k / 2) {
    char c = s.charAt(i);
    if (c == '.') dots = dots | k;
    else t.setCharAt(j++, c);
  }
  module.setDisplayToString(t, dots);
}

void calculate() {
  float op0 = operand[0].toFloat();
  float op1 = operand[1].toFloat();
  switch (operation) {
    case '/': result = op0 / op1; break;
    case 'x': result = op0 * op1; break;
    case '-': result = op0 - op1; break;
    case '+': result = op0 + op1; break;
  }
  String s = String(result, 7);
  print(s);
  reset();
}

char getKey() {
  word buttons = module.getButtons();
  char myKeys[] = "789/456x123-0.=+";
  if (buttons) {
    for (word i = 0, b = 1; b; i++, b = b * 2) {
      if (buttons == b) {
        return myKeys[i];
      }
    }
  }
  return 0;
}

void reset() {
  operand[0] = "";
  operand[1] = "";
  operation = ' ';
  status = 0;
}






contact: nji(at)gmx.de