week 4

February 27, 2025
microcontrollers arduino
...

This week's assignment was to program an arduino to do something. I decided to choose compnents for my circuit before coming up with an idea——I grabbed a TFT display, some buttons, an ESP32, and a generous handful of wires. Shortly after, I set off to make a device that translates morse code to english.

the process

My first step was to make the display turn on and run a grapics test. As someone with very little experience with circuitry and even less with cs, this turned out to be a difficult task. I soon learned that I would need two breadboards, as the dimensions of the ESP32 only allow for use of one row on the breadboard, but I needed access to both sides of pins. I wired everything up according to a tutorial I found online, but the screen did not turn on. With Bobby's help, who told me I mirrored the wiring for the pins on the TFT, I was able to run a graphics test on the display. He also helped me get rid of a row of dead pixels by chnaging the dimensions in the code to match the display.

With a working display, I placed buttons in the breadboard and wired them up so that the display would show when each button is pressed. Then, I made strings of the letters in morse code and the alphabet, as well as a string that encodes if the dot button or dash button was pressed. Jessica was a huge help here, especially in making loops that would cycle through morse code letters until matching with the corresponding Engish letter.

I'm not sure if I want to incorporate morse code in my final project, but if I do, I would need to figure out how to create strings of letters and add an additional button for spaces between words.

my code



/**************************************************************************
  This is a library for several Adafruit displays based on ST77* drivers.

  Written by Limor Fried/Ladyada for Adafruit Industries.
  MIT license, all text above must be included in any redistribution
 **************************************************************************/

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>
#include <string>

#define TFT_CS         15
#define TFT_RST        -1
#define TFT_DC         4

#define BUTTON_DASH 12
#define BUTTON_DOT 14
#define BUTTON_ENTER 26

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

float p = 3.1415926;

int currentString[4] = {-1, -1, -1, -1};
int currentChar = 0;

void setup(void) {
  Serial.begin(9600);
  Serial.print(F("Hello! ST77xx TFT Test"));

  tft.initR(INITR_144GREENTAB); // Init ST7735R chip, green tab

  Serial.println(F("Initialized"));

  uint16_t time = millis();
  tft.fillScreen(ST77XX_BLACK);
  time = millis() - time;

  Serial.println(time, DEC);
  delay(500);

  // large block of text
  tft.fillScreen(ST77XX_BLACK);
  testdrawtext("Code uploaded", ST77XX_WHITE);
  delay(1000);
  tft.fillScreen(ST77XX_BLACK);

  Serial.println("done");
  delay(1000);

  pinMode(BUTTON_DASH, INPUT_PULLUP);
  pinMode(BUTTON_DOT, INPUT_PULLUP);
  pinMode(BUTTON_ENTER, INPUT_PULLUP);
}

void loop() {

  if (digitalRead(BUTTON_ENTER) == LOW) {
    tft.fillScreen(ST77XX_BLACK);
    testdrawtext(translateCurrentEntry(), ST77XX_WHITE); 
    delay(2000);
    tft.fillScreen(ST77XX_BLACK);
    resetCurrentEntry();
  }

  if (digitalRead(BUTTON_DASH) == LOW) {
    currentString[currentChar] = 1;
    currentChar += 1;
    testdrawtext("-", ST77XX_WHITE);
    delay(1000);
    tft.fillScreen(ST77XX_BLACK);
  }
  else if (digitalRead(BUTTON_DOT) == LOW) {
    currentString[currentChar] = 0;
    currentChar += 1;
    testdrawtext(".", ST77XX_WHITE);
    delay(1000);
    tft.fillScreen(ST77XX_BLACK);
  }
}

String translateCurrentEntry() {  // Placeholder script, eventually return the character from morse
  String letters[26] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
  String alphabet[26] = {"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"};
  String entry = "";
  
  for (int i = 0; i < 4; i += 1) {
    if (currentString[i] == 1) {
      entry = entry += "-";
    }
    else if (currentString[i] == 0) {
      entry = entry += ".";
    }
  }

  String output = "Translation: ";

  for (int i = 0; i < 26; i += 1) {
    if (entry.equals(letters[i])) {
      output = output + alphabet[i];
    }
  }
  return output;
}

void resetCurrentEntry() {
  for (int i = 0; i < 4; i += 1) {
    currentString[i] = -1;
  }
  currentChar = 0;
}

void testdrawtext(String text, uint16_t color) {
  tft.setCursor(0, 0);
  tft.setTextColor(color);
  tft.setTextWrap(true);
  tft.print(text);
}

void tftPrintTest() {
  tft.setTextWrap(false);
  tft.fillScreen(ST77XX_BLACK);
  tft.setCursor(0, 30);
  tft.setTextColor(ST77XX_RED);
  tft.setTextSize(1);
  tft.println("Hello World!");
  tft.setTextColor(ST77XX_YELLOW);
  tft.setTextSize(2);
  tft.println("Hello World!");
  tft.setTextColor(ST77XX_GREEN);
  tft.setTextSize(3);
  tft.println("Hello World!");
  tft.setTextColor(ST77XX_BLUE);
  tft.setTextSize(4);
  tft.print(1234.567);
  delay(1500);
  tft.setCursor(0, 0);
  tft.fillScreen(ST77XX_BLACK);
  tft.setTextColor(ST77XX_WHITE);
  tft.setTextSize(0);
  tft.println("Hello World!");
  tft.setTextSize(1);
  tft.setTextColor(ST77XX_GREEN);
  tft.print(p, 6);
  tft.println(" Want pi?");
  tft.println(" ");
  tft.print(8675309, HEX); // print 8,675,309 out in HEX!
  tft.println(" Print HEX!");
  tft.println(" ");
  tft.setTextColor(ST77XX_WHITE);
  tft.println("Sketch has been");
  tft.println("running for: ");
  tft.setTextColor(ST77XX_MAGENTA);
  tft.print(millis() / 1000);
  tft.setTextColor(ST77XX_WHITE);
  tft.print(" seconds.");
}

void testButtons() {
  if (digitalRead(BUTTON_DASH) == LOW) {
      tft.fillScreen(ST77XX_BLACK);
      testdrawtext("dash pressed", ST77XX_WHITE);
      delay(1000);
      tft.fillScreen(ST77XX_BLACK);
  }
  else if (digitalRead(BUTTON_DOT) == LOW) {
      tft.fillScreen(ST77XX_BLACK);
      testdrawtext("dot pressed", ST77XX_WHITE);
      delay(1000);
      tft.fillScreen(ST77XX_BLACK);
  }
  else if (digitalRead(BUTTON_ENTER) == LOW) {
      tft.fillScreen(ST77XX_BLACK);
      testdrawtext("enter pressed", ST77XX_WHITE);
      delay(1000);
      tft.fillScreen(ST77XX_BLACK);
  }
}
            
...