Is the problem in the code or in the OLED screen?

First off, 'idx' is a parameter passed into the addCharInput() function...
void addCharInput(int idx) {...

The above code just assigns the index into the num[] array and then puts the character into the InputArray[]. The shorthand of post-incrementing the index InputArrayPos save a line of code. It would be the same as

  num[InputArrayPos] = idx; // store index for later computation
  InputArray[InputArrayPos] = Input[idx];
  InputArrayPos++;  // or InputArrayPos = InputArrayPos + 1
  InputArray[InputArrayPos] = '\0';

'uint8_t is a standard C/C++ type. It means an 8 bit, unsigned value (aka byte or unsigned char) If you want a specific amount of storage, independent of which platform you are working on, you use uint8_t or uint16_t or uint32_t, etc. As an example, an Uno treats an int as 16 bits, but on an ESP32 an int is 32 bits. bool is a boolean variable that can be true or false

As for debugging, it is nothing more than inserting some Serial.print() statements into the code to inform you about what is happening...

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels

// On an arduino UNO:       A4(SDA), A5(SCL)
#define OLED_RESET 4         // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C  ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const int maxInput = 100;

char InputArray[maxInput]; // stores the selected value from char Input
char Output[maxInput]; // stores the final result to show on screen
char Input[36] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; // shows value on screen and allows selection to store in char InputArray
char AlphaNumChart[6][6] = { { 'a', 'b', 'c', 'd', 'e', 'f' },
  { 'g', 'h', 'i', 'j', 'k', 'l' },
  { 'm', 'n', 'o', 'p', 'q', 'r' },
  { 's', 't', 'u', 'v', 'w', 'x' },
  { 'y', 'z', '0', '1', '2', '3' },
  { '4', '5', '6', '7', '8', '9' }
}; // based on coordinates calculated by the formula, will select characters from this matrix to store in char Output

uint8_t num[maxInput];  //indicies of InputArray

const int keyPin = 2; // key to allow selection of the value to store in char InputArray
const int selectionPin = A0;  // rotary pot
int prevKeyState;
int prevSelectionState;
int prevSelectionIdx;

int InputArrayPos = 0; //this is just for word positioning on the display

void setup() {
  pinMode(keyPin, INPUT);
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  display.setTextWrap(false);
  display.clearDisplay();

  Serial.println(F("Ready"));
}

void loop() {

  int selectionIdx = map(analogRead(selectionPin), 0, 1023, 0, 36);
  int keyState = digitalRead(keyPin);

  if ( selectionIdx == prevSelectionIdx && keyState == prevKeyState ) {
    // nothing has changed, so nothing to do, exit loop at try again
    return;
  }

  // something has changed, either the character selected or the state of the key

  if ( selectionIdx != prevSelectionIdx ) {
    updateCharSelection(selectionIdx);
    prevSelectionIdx = selectionIdx;
  }

  if ( keyState != prevKeyState ) {
    prevKeyState = keyState;
    if ( selectionIdx < 36 ) {
      // process a key selection
      if (keyState == HIGH) {
        // key was pressed, add it to input
        addCharInput(selectionIdx);
      }
      else {
        // key was released, do nothing
      }
    }
    else {
      // deal with final "Show output" selection
      if (keyState == HIGH) {
        // key was pressed, show output
        calculateOutput();
        displayOutput();
      }
      else {
        // key was released, do nothing
      }
    }
    delay(20);  // debounce key
  }
}

void calculateOutput() {

  Serial.print(F("calculating output. Char count= ")); Serial.println(InputArrayPos);

  for (int i = 0; i < InputArrayPos; i++) {
    int row = num[i] / 6;
    int col = num[InputArrayPos - 1 - i] % 6;
    Output[i] = AlphaNumChart[row][col];
    Serial.print(Input[num[i]]); Serial.print(F("->")); Serial.println(Output[i]);
  }
  Output[InputArrayPos] = '\0';
}

void displayOutput() {

  int CursorEnd = (-12 * InputArrayPos) + display.width();
  int CursorStart = 0;
  int CursorRef = 0;
  
  while (digitalRead(keyPin) == HIGH) {

    if (InputArrayPos <= 10) {
      displayPrint(0, 27, WHITE, 2);
      display.println(Output);
      display.display();
    }
    else {
      display.clearDisplay();
      displayPrint(0, 0, WHITE, 1);
      display.println("Your Output:");
      displayPrint(CursorStart, 27, WHITE, 2);
      display.println(Output);
      display.display();
      if (CursorStart == 0) {
        CursorStart = CursorStart - 2;
        CursorRef = 0;
        delay(1000);
      }
      if ((CursorStart < 0) && (CursorRef == 0)) CursorStart = CursorStart - 2;
      if (CursorStart == CursorEnd) {
        CursorStart = CursorStart + 2;
        CursorRef = 1;
        delay(1000);
      }
      if ((CursorStart < 0) && (CursorRef == 1)) CursorStart = CursorStart + 2;
    }
  }
}

void displayPrint(int x, int y, int color, int size) {
  display.setCursor(x, y);
  display.setTextColor(color);
  display.setTextSize(size);
}

void updateCharSelection(int idx) {
  char ch;
  bool onChar;    // true when selection is on a char, false if "Show Output"

  if (idx < 36) {
    ch = Input[idx];
    onChar = true;
  }
  else {
    onChar = false;
  }

  display.clearDisplay();
  if ( onChar ) {
    displayPrint(0, 0, WHITE, 1);
    display.println("Your selection:");
    displayPrint(55, 15, WHITE, 4);
    display.print(ch);
    Serial.print(F("Selection is ")); Serial.println(ch);
  }
  else {
    displayPrint(0, 22, WHITE, 2);
    display.println("SHOW Output");
    Serial.println(F("Selection is OUTPUT"));
  }
  displayInput();
}

void addCharInput(int idx) {

  Serial.print(F("Adding ")); Serial.println(Input[idx]);
  
  num[InputArrayPos] = idx; // store index for later computation
  InputArray[InputArrayPos++] = Input[idx];
  InputArray[InputArrayPos] = '\0';
  if ( InputArrayPos >= maxInput - 1 ) {
    InputArrayPos = maxInput - 2;
  }
  display.clearDisplay();
  displayPrint(0, 0, WHITE, 1);
  display.println("Your selection:");
  displayPrint(50, 22, WHITE, 2);
  display.println("OK!");

  displayInput();
}

void displayInput() {
  int cursor = 0;

  if (InputArrayPos > 10) {
    cursor = (10 - InputArrayPos) * 12;
  }
    displayPrint(cursor, 47, WHITE, 2);
    display.println(InputArray);
    display.display();
}
1 Like