LCD cursor replacing item that was overwritten

Hello! I know the title isn't clear but I didn't know how to phrase it properly. Basically, I'm using this code that I made to display a cursor on screen. Only problem is that whenever you hover your cursor over an item on the LCD, that item gets replaced with your cursor (intended) but when you remove your cursor, the item stays as a cursor (not intended). I know why it's leaving behind a cursor and I know how to solve it if I need to, but I was wondering if it's possible to dynamically save everything on screen into an array or list and then use that to replace what was there after the cursor leaves. Is it even possible with arrays? It doesn't need to but that's the only thing I can think of. Here's the code:

#include <LiquidCrystal.h>

#define joyX A0
#define joyY A1

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
const int buzzer = 8;
const int button = 7;
const int led = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

//Custom Characters

byte keyWhite[] = {
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111
};

byte keyWhiteContinued[] = {
  B11111,
  B11011,
  B11011,
  B11011,
  B11011,
  B11011,
  B11011,
  B11111
};

byte keyBlack[] = {
  B11111,
  B10001,
  B10001,
  B10001,
  B10001,
  B10001,
  B10001,
  B11111
};

byte keyBlackContinued[] = {
  B11111,
  B10101,
  B10101,
  B10101,
  B10101,
  B10101,
  B10101,
  B11111
};

byte blank[] = {
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
};

byte settings[] = {
  B00000,
  B10101,
  B01110,
  B11011,
  B01110,
  B10101,
  B00000,
  B00000
};

byte settingsSelected[] = {
  B00000,
  B10101,
  B01110,
  B11011,
  B01110,
  B10101,
  B00000,
  B11111
};

byte selectionBox[] = {
  B11011,
  B10001,
  B00000,
  B10001,
  B10001,
  B00000,
  B10001,
  B11011
};

float xValue = 0;
float yValue = 0;

int buttonState = 0;
int prevButtonState = 0;

int selectionX = 0;
int selectionY = 0;

int prevselectionX = 0;
int prevselectionY = 0;

int octaveNum = 1;
int scroll = 0;

int bpm = 120;

void setup() {

  lcd.createChar(0, keyWhite);
  lcd.createChar(1, keyBlack);
  lcd.createChar(2, keyWhiteContinued);
  lcd.createChar(3, keyBlackContinued);
  lcd.createChar(5, blank);
  lcd.createChar(6, settings);
  lcd.createChar(7, selectionBox);

  pinMode(buzzer, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);

  lcd.begin(16, 2);
  tone(buzzer, 440, 100);

  PrintWhite();
  PrintBlack();

  lcd.setCursor(15, 0);
  lcd.write(6);

  lcd.setCursor(selectionX, selectionY);
  lcd.write(7);

  lcd.setCursor(15, 1);
  lcd.print(octaveNum);
}

void loop() {

  xValue = analogRead(joyX) / 1023.0 * 2 - 1;
  yValue = analogRead(joyY) / 1023.0 * 2 - 1;

  buttonState = digitalRead(button);

  //LEFT AND RIGHT

  if (xValue > 0.9 || xValue < -0.9)
  {
    if (xValue < -0.9) //Right
    {
      selectionX++;
      if (selectionX > 15 && !(octaveNum > 4))
      {
        selectionX = 0;
        octaveNum++;
      }
    }
    if (xValue > 0.9) //Left
    {
      selectionX--;
      if (selectionX < 0 && !(octaveNum < 2))
      {
        selectionX = 15;
        octaveNum--;
      }
    }

    lcd.setCursor(selectionX, selectionY);
    lcd.write(7);
    lcd.createChar(7, selectionBox);

    delay(250);
  }

  //Up and Down

  if (yValue > 0.9 || yValue < -0.9)
  {
    if (yValue < -0.9 && !(scroll == 0)) //Down
    {
      selectionY++;
      if (selectionY > 1)
      {
        selectionY = 0;
        scroll--;
      }
    }
    if (yValue > 0.9) //Up
    {
      selectionY--;
      if (selectionY < 0)
      {
        selectionY = 1;
        scroll++;
      }
    }

    lcd.setCursor(selectionX, selectionY);
    lcd.write(7);
    lcd.createChar(7, selectionBox);

    delay(250);
  }

  if (buttonState != prevButtonState)
  {
    if (buttonState == HIGH)
    {
      AddNote(selectionX - 2, octaveNum, (600 / bpm) * 100);
    }
    delay(250);
  }

  prevButtonState = buttonState;
}

void PrintWhite() {

  lcd.setCursor(2,1);
  lcd.write(byte(0));

  lcd.setCursor(4,1);
  lcd.write(byte(0));

  lcd.setCursor(6,1);
  lcd.write(byte(0));

  lcd.setCursor(7,1);
  lcd.write(byte(0));

  lcd.setCursor(9,1);
  lcd.write(byte(0));

  lcd.setCursor(11,1);
  lcd.write(byte(0));

  lcd.setCursor(13,1);
  lcd.write(byte(0));
}

void PrintBlack() {

  lcd.setCursor(3,1);
  lcd.write(byte(1));

  lcd.setCursor(5,1);
  lcd.write(byte(1));

  lcd.setCursor(8,1);
  lcd.write(byte(1));

  lcd.setCursor(10,1);
  lcd.write(byte(1));

  lcd.setCursor(12,1);
  lcd.write(byte(1));
}

void AddNote(int key, int oct, int length) {

  if (oct == 1)
  {
    if (key == 0)
    {
      tone(buzzer, 65.40639, length);
    }
    if (key == 1)
    {
      tone(buzzer, 69.29566, length);
    }
    if (key == 2)
    {
      tone(buzzer, 73.41619, length);
    }
    if (key == 3)
    {
      tone(buzzer, 77.78175, length);
    }
    if (key == 4)
    {
      tone(buzzer, 82.40689, length);
    }
    if (key == 5)
    {
      tone(buzzer, 87.30706, length);
    }
    if (key == 6)
    {
      tone(buzzer, 92.49861, length);
    }
    if (key == 7)
    {
      tone(buzzer, 97.99886, length);
    }
    if (key == 8)
    {
      tone(buzzer, 103.8262, length);
    }
    if (key == 9)
    {
      tone(buzzer, 110.0000, length);
    }
    if (key == 10)
    {
      tone(buzzer, 116.5409, length);
    }
    if (key == 11)
    {
      tone(buzzer, 123.4708, length);
    }
  }

  if (oct == 2)
  {
    if (key == 0)
    {
      tone(buzzer, 130.8128, length);
    }
    if (key == 1)
    {
      tone(buzzer, 138.5913, length);
    }
    if (key == 2)
    {
      tone(buzzer, 146.8324, length);
    }
    if (key == 3)
    {
      tone(buzzer, 155.5635, length);
    }
    if (key == 4)
    {
      tone(buzzer, 164.8138, length);
    }
    if (key == 5)
    {
      tone(buzzer, 174.6141, length);
    }
    if (key == 6)
    {
      tone(buzzer, 184.9972, length);
    }
    if (key == 7)
    {
      tone(buzzer, 195.9977, length);
    }
    if (key == 8)
    {
      tone(buzzer, 207.6523, length);
    }
    if (key == 9)
    {
      tone(buzzer, 220.0000, length);
    }
    if (key == 10)
    {
      tone(buzzer, 233.0819, length);
    }
    if (key == 11)
    {
      tone(buzzer, 246.9417, length);
    }
  }

  if (oct == 3)
  {
    if (key == 0)
    {
      tone(buzzer, 261.6256, length);
    }
    if (key == 1)
    {
      tone(buzzer, 277.1826, length);
    }
    if (key == 2)
    {
      tone(buzzer, 293.6648, length);
    }
    if (key == 3)
    {
      tone(buzzer, 311.1270, length);
    }
    if (key == 4)
    {
      tone(buzzer, 329.6276, length);
    }
    if (key == 5)
    {
      tone(buzzer, 349.2282, length);
    }
    if (key == 6)
    {
      tone(buzzer, 369.9944, length);
    }
    if (key == 7)
    {
      tone(buzzer, 391.9954, length);
    }
    if (key == 8)
    {
      tone(buzzer, 415.3047, length);
    }
    if (key == 9)
    {
      tone(buzzer, 440.0000, length);
    }
    if (key == 10)
    {
      tone(buzzer, 466.1638, length);
    }
    if (key == 11)
    {
      tone(buzzer, 493.8833, length);
    }
  }

  if (oct == 4)
  {
    if (key == 0)
    {
      tone(buzzer, 523.2511, length);
    }
    if (key == 1)
    {
      tone(buzzer, 554.3653, length);
    }
    if (key == 2)
    {
      tone(buzzer, 587.3295, length);
    }
    if (key == 3)
    {
      tone(buzzer, 622.2540, length);
    }
    if (key == 4)
    {
      tone(buzzer, 659.2551, length);
    }
    if (key == 5)
    {
      tone(buzzer, 698.4565, length);
    }
    if (key == 6)
    {
      tone(buzzer, 739.9888, length);
    }
    if (key == 7)
    {
      tone(buzzer, 783.9909, length);
    }
    if (key == 8)
    {
      tone(buzzer, 830.6094, length);
    }
    if (key == 9)
    {
      tone(buzzer, 880.0000, length);
    }
    if (key == 10)
    {
      tone(buzzer, 932.3275, length);
    }
    if (key == 11)
    {
      tone(buzzer, 987.7666, length);
    }
  }

  if (oct == 5)
  {
    if (key == 0)
    {
      tone(buzzer, 1046.502, length);
    }
    if (key == 1)
    {
      tone(buzzer, 1108.731, length);
    }
    if (key == 2)
    {
      tone(buzzer, 1174.659, length);
    }
    if (key == 3)
    {
      tone(buzzer, 1244.508, length);
    }
    if (key == 4)
    {
      tone(buzzer, 1318.510, length);
    }
    if (key == 5)
    {
      tone(buzzer, 1396.913, length);
    }
    if (key == 6)
    {
      tone(buzzer, 1479.978, length);
    }
    if (key == 7)
    {
      tone(buzzer, 1567.982, length);
    }
    if (key == 8)
    {
      tone(buzzer, 1661.219, length);
    }
    if (key == 9)
    {
      tone(buzzer, 1760.000, length);
    }
    if (key == 10)
    {
      tone(buzzer, 1864.655, length);
    }
    if (key == 11)
    {
      tone(buzzer, 1975.533, length);
    }
  }
}

One way to refresh your keys is by calling PrintWhite();and PrintBlack();

Or store the keys in an array (single dimension for this example)

keys[12] = {0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0}; // 0 = black key, 1 = white key

and when your cursor moves away from a key, look up the key color and re-write the key.

By the way, you keep re-defining character 7:

    lcd.write(7);
    lcd.createChar(7, selectionBox);

...and never use the LED.

yeah, I added the LED at the very start of the project, and I keep it just in case. I'm quite new to Arduino so I thought it would be fun to mess around with a light (even if I never used it of course)

also I mean, I want to save everything, not just the keys. If I add a new element I want to be able to store and retrieve that element's position and value. I tried saving everything to an array and then using a for loop to get the information at the end of loop() but it doesn't work

Overwritten.

woops yeah my bad

Will the layout of the keyboard ever change? I think what you want is to make the "key" look the same as it was before the cursor moves over it. You know the cursor location with selectionX, selectionY, so before moving the cursor, save this overwritten location, get the key color of the overwritten location (from a constant array), move the cursor, re-write the key at the saved location.

Try doing this with test code on just the LCD with the joystick before moving that method into your main program, then post your code (the test code and the complete code).

yes it will, the keys are just there as a test, but later by pressing the button, you will be able to add notes which I also want to save, the saving part is good, I just don't how to replace it back to the original item

Yes, you do...

You know how to move a cursor by changing its x,y. Store the current cursor x,y, move the cursor, re-write the stored x,y with the key that belongs there, move the cursor as you already do.

Practice this (they include a nice video):

Ok so turns out I did something wrong, I don't know what. But I got another script to work and um yeah it works. So solved I guess?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.