Backspace Function

Good day, everyone.

My groupmates and I are thinking of an Arduino project, and we found this code on YouTube/Github: Arduino Morse Code Decoder.

[EDIT] We're thinking to make it our base code and add a function which is TTS using a button. The problem is we don't know why the backspace didn't work even though the wirings are correct, and when we try to incorporate Talkie, it conflicts with other parts of the code.

Here's the code:

// vim: ft=cpp
#include <Arduino.h>
#include <LiquidCrystal.h>
#include <Debouncer.h>
#include "letter.h"
                
const int RS = 2, E = 3, D4 = 4, D5 = 5, D6 = 6, D7 = 7;
const int LED = 8, INPUT_BTN = 9, BS_BTN = 10, S_BTN = 11;
const int tick = 100;

LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
Debouncer debouncer(BS_BTN, 100);
Letter letter;
bool pressed = false;
bool led = false;
unsigned long last = 0;
int cursor = 0;

void setup() {
    lcd.begin(16, 2);
    for (int pin = 2; pin <= 8; pin++) 
        pinMode(pin, OUTPUT);
    pinMode(INPUT_BTN, INPUT);
    pinMode(BS_BTN, INPUT);
    debouncer.subscribe(Debouncer::Edge::FALL, [](){
        if (letter.bits[0] != 0) {
            lcd.setCursor(cursor, 0);
            lcd.print("         ");
            letter = Letter();
        } else {
            lcd.setCursor(--cursor, 0);
            lcd.print("         ");
        }
        lcd.setCursor(cursor, 0);
        last = millis();
    });
    last = millis();
    lcd.cursor();
}

void loop() {
    unsigned long current = millis();
    unsigned long gap = current - last;
    int state1 = digitalRead(INPUT_BTN);
    int state2 = digitalRead(S_BTN);
    debouncer.update();
    if (current/tick % 2 != led) {
        led = !led;
        digitalWrite(8, led ? HIGH : LOW);
    }
    if (!pressed && state1 == LOW) {
        pressed = true;
        last = current;
        if (gap > 3*tick) {
            char c = letter.decode();
            if (c != 0) {
                lcd.setCursor(cursor, 0);
                lcd.print("     ");
                lcd.setCursor(cursor++, 0);
                lcd.print(c);
            }
            if (gap > 18*tick) {
                lcd.print(" ");
                cursor++;
            }
            letter = Letter();
        }
    } else if (pressed && state1 == HIGH) {
        pressed = false;
        last = current;
        if (gap > tick) {
            char sym = (gap > 3*tick ? '-' : '.');
            lcd.print(sym);
            letter.append(sym);
        }
    }
}

The materials we are using in this project:
Arduino Uno
Jumper Wires
Passive Buzzer (for Morse code tone)
Button(s) (for morse code input & planned backspace function)
LCD 1602 display
10K Potentiometer
1K Resistor

Source video: Arduino Morse decoder - Album on Imgur

What is the point of this group project?
To implement some sort of Morse trainer device, or some sort of management project, to try to persuade others to do your work for you?

So what have you done to debug it?

Ask yourselves:

  1. What is it supposed to do?
  2. What is it actually doing?
  3. What tests/investigations can you do to explain the difference?

Debugging is a key skill - whether it's your own code, or somebody else's that's not working:

https://www.avrfreaks.net/s/topic/a5C3l000000UaFXEA0/t153137?comment=P-1212908

This seems to be how characters get onto the screen:

if (c != 0) {
                lcd.setCursor(cursor, 0);
                lcd.print("     ");
                lcd.setCursor(cursor++, 0);
                lcd.print(c);
            }

So… when someone hits the backspace button, pseudocode:

   if there are any characters (cursor > zero, maybe?)
      reduce cursor by one
      position at the cursor and print a space character 

HTH

a7

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