This code is bad

#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
#include <ADCTouch.h>
#include <lvgl.h>

#define TFT_CS   7
#define TFT_RST  5
#define TFT_DC   6
#define TFT_BL   8
#define BASE_PIN A5
#define COLLECTOR_PIN A6
#define EMITTER_PIN A7

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

// LVGL display buffer
static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[160 * 10]; // Buffer for 10 rows (160x128 display)

// Touch button variables
int ref1, ref2, ref3;
int freq = 1000;
int stepSize = 100;
int mode = 0; // 0: Menu, 1: Set Freq, 2: Set Step, 3: Reset, 4: Oscilloscope, 5: Transistor
int oscTimeBase = 10;
static bool osc_active = false;

// LVGL objects
lv_obj_t *menu_screen;
lv_obj_t *mode_screen;
lv_obj_t *label_status;

void setup() {
  pinMode(TFT_BL, OUTPUT);
  digitalWrite(TFT_BL, HIGH);

  // Initialize ST7735
  tft.initR(INITR_BLACKTAB);
  tft.setRotation(1);

  // Initialize LVGL
  lv_init();
  lv_disp_draw_buf_init(&draw_buf, buf, NULL, 160 * 10);
  static lv_disp_drv_t disp_drv;
  lv_disp_drv_init(&disp_drv);
  disp_drv.flush_cb = my_disp_flush;
  disp_drv.draw_buf = &draw_buf;
  disp_drv.hor_res = 160;
  disp_drv.ver_res = 128;
  lv_disp_drv_register(&disp_drv);

  // PWM setup on D9
  pinMode(9, OUTPUT);
  TCCR1A = _BV(COM1A1) | _BV(WGM11);
  TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
  ICR1 = 16000;
  OCR1A = 8000;

  pinMode(BASE_PIN, OUTPUT);
  pinMode(COLLECTOR_PIN, OUTPUT);
  pinMode(EMITTER_PIN, OUTPUT);

  ref1 = ADCTouch.read(A1, 500);
  ref2 = ADCTouch.read(A2, 500);
  ref3 = ADCTouch.read(A3, 500);

  create_menu_screen();
}

void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) {
  uint32_t w = area->x2 - area->x1 + 1;
  uint32_t h = area->y2 - area->y1 + 1;
  tft.setAddrWindow(area->x1, area->y1, w, h);
  tft.pushColors((uint16_t *)color_p, w * h, true);
  lv_disp_flush_ready(disp);
}

void loop() {
  if (!osc_active) lv_timer_handler(); // Skip LVGL in oscilloscope mode
  delay(5);

  int touch1 = ADCTouch.read(A1, 500) - ref1;
  int touch2 = ADCTouch.read(A2, 500) - ref2;
  int touch3 = ADCTouch.read(A3, 500) - ref3;
  const int threshold = 50;

  if (mode == 0) { // Menu mode handled by LVGL events
    return;
  } else if (mode == 1) { // Set Frequency
    if (touch1 > threshold) {
      freq += stepSize;
      if (freq > 10000) freq = 10000;
      updateFrequency();
    }
    if (touch2 > threshold) {
      freq -= stepSize;
      if (freq < 0) freq = 0;
      updateFrequency();
    }
  } else if (mode == 2) { // Set Step Size
    if (touch1 > threshold) {
      stepSize *= 10;
      if (stepSize > 1000) stepSize = 1000;
      lv_label_set_text_fmt(label_status, "Step: %d Hz", stepSize);
    }
    if (touch2 > threshold) {
      stepSize /= 10;
      if (stepSize < 1) stepSize = 1;
      lv_label_set_text_fmt(label_status, "Step: %d Hz", stepSize);
    }
  } else if (mode == 4) { // Oscilloscope
    runOscilloscope(touch1, touch2, touch3, threshold);
  } else if (mode == 5) { // Transistor Tester
    testTransistor();
  }

  if (touch3 > threshold && mode != 0) {
    mode = 0;
    lv_scr_load(menu_screen);
  }
}

// Rest of your functions (create_menu_screen, btnmatrix_event_cb, etc.) remain unchanged
void create_menu_screen() {
  menu_screen = lv_obj_create(NULL);
  lv_obj_set_style_bg_color(menu_screen, lv_color_hex(0x1E1E1E), 0);

  static const char *btn_labels[] = {"Set Freq", "Step Size", "Reset", "Oscilloscope", "Transistor", ""};
  lv_obj_t *btnm = lv_btnmatrix_create(menu_screen);
  lv_btnmatrix_set_map(btnm, btn_labels);
  lv_obj_set_size(btnm, 140, 100);
  lv_obj_align(btnm, LV_ALIGN_CENTER, 0, 0);
  lv_obj_add_event_cb(btnm, btnmatrix_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
  lv_obj_set_style_bg_color(btnm, lv_color_hex(0x3C3C3C), 0);
  lv_obj_set_style_text_color(btnm, lv_color_hex(0xFFFFFF), 0);

  lv_scr_load(menu_screen);
}

void btnmatrix_event_cb(lv_event_t *e) {
  lv_obj_t *obj = lv_event_get_target(e);
  uint32_t id = lv_btnmatrix_get_selected_btn(obj);
  mode = id + 1;
  create_mode_screen();
}

void create_mode_screen() {
  mode_screen = lv_obj_create(NULL);
  lv_obj_set_style_bg_color(mode_screen, lv_color_hex(0x1E1E1E), 0);

  label_status = lv_label_create(mode_screen);
  lv_obj_align(label_status, LV_ALIGN_TOP_MID, 0, 10);
  lv_obj_set_style_text_color(label_status, lv_color_hex(0xFFFFFF), 0);

  if (mode == 1) {
    lv_label_set_text(label_status, "Set Freq:");
    lv_obj_t *freq_label = lv_label_create(mode_screen);
    lv_label_set_text_fmt(freq_label, "%d Hz", freq);
    lv_obj_align(freq_label, LV_ALIGN_CENTER, 0, 0);
  } else if (mode == 2) {
    lv_label_set_text(label_status, "Step Size:");
    lv_label_set_text_fmt(label_status, "Step: %d Hz", stepSize);
  } else if (mode == 3) {
    lv_label_set_text(label_status, "Reset Done");
    freq = 1000;
    stepSize = 100;
    updateFrequency();
    mode = 0;
    lv_scr_load(menu_screen);
    return;
  } else if (mode == 4) {
    lv_label_set_text(label_status, "Oscilloscope:");
  } else if (mode == 5) {
    lv_label_set_text(label_status, "Transistor:");
  }

  lv_scr_load(mode_screen);
}

void updateFrequency() {
  if (freq > 0 && freq <= 10000) {
    ICR1 = 16000000UL / freq;
    OCR1A = ICR1 / 2;
  } else {
    digitalWrite(9, LOW);
  }
  if (mode == 1) {
    lv_obj_t *freq_label = lv_obj_get_child(mode_screen, 1);
    lv_label_set_text_fmt(freq_label, "%d Hz", freq);
  }
}

void runOscilloscope(int touch1, int touch2, int touch3, int threshold) {
  osc_active = true;
  int samples[128];
  int triggerLevel = 512;
  bool triggered = false;
  unsigned long timeout = millis();

  if (touch3 > threshold) {
    mode = 0;
    lv_scr_load(menu_screen);
    osc_active = false;
    return;
  }

  int lastSample = analogRead(A0);
  while (millis() - timeout < 1000) {
    int currentSample = analogRead(A0);
    if (lastSample < triggerLevel && currentSample >= triggerLevel) {
      triggered = true;
      break;
    }
    lastSample = currentSample;
    touch3 = ADCTouch.read(A3, 500) - ref3;
    if (touch3 > threshold) {
      mode = 0;
      lv_scr_load(menu_screen);
      osc_active = false;
      return;
    }
  }

  for (int i = 0; i < 128; i++) {
    samples[i] = analogRead(A0);
    if (oscTimeBase > 0) delayMicroseconds(oscTimeBase);
    touch3 = ADCTouch.read(A3, 500) - ref3;
    if (touch3 > threshold) {
      mode = 0;
      lv_scr_load(menu_screen);
      osc_active = false;
      return;
    }
  }

  tft.fillScreen(ST7735_BLACK);
  for (int y = 32; y < 128; y += 32) {
    tft.drawFastHLine(0, y, 128, ST7735_GREEN);
  }

  for (int x = 0; x < 127; x++) {
    int y1 = map(samples[x], 0, 1023, 127, 0);
    int y2 = map(samples[x + 1], 0, 1023, 127, 0);
    tft.drawLine(x, y1, x + 1, y2, ST7735_GREEN);
  }

  lv_label_set_text_fmt(label_status, "Osc: %d us %s", oscTimeBase, triggered ? "Trig" : "NoTrig");
  osc_active = false;
}

void testTransistor() {
  String result = "Unknown";
  int collectorVoltage;

  digitalWrite(BASE_PIN, HIGH);
  digitalWrite(EMITTER_PIN, LOW);
  digitalWrite(COLLECTOR_PIN, HIGH);
  delay(10);
  collectorVoltage = analogRead(A6);
  if (collectorVoltage > 400) {
    result = "NPN";
  }

  digitalWrite(BASE_PIN, LOW);
  digitalWrite(EMITTER_PIN, HIGH);
  digitalWrite(COLLECTOR_PIN, LOW);
  delay(10);
  collectorVoltage = analogRead(A6);
  if (collectorVoltage < 200) {
    result = "PNP";
  }

  lv_label_set_text_fmt(label_status, "Transistor: %s\nC: %.1fV", result.c_str(), (float)collectorVoltage * 5.0 / 1023.0);
}

error code
In file included from c:\users\nelson\documents\arduino\libraries\lvgl\src/lv_init.h:16:0,
from c:\users\nelson\documents\arduino\libraries\lvgl\lvgl.h:21,
from c:\Users\Nelson\Documents\Arduino\libraries\lvgl\src/lvgl.h:16,
from C:\Users\Nelson\Desktop\sketch_feb26a\sketch_feb26a.ino:5:
c:\users\nelson\documents\arduino\libraries\lvgl\src/lv_conf_internal.h:60:18: fatal error: ../../lv_conf.h: No such file or directory
#include "../../lv_conf.h" /Else assume lv_conf.h is next to the lvgl folder/
^~~~~~~~~~~~~~~~~
compilation terminated.
exit status 1

Compilation error: exit status 1

I don't know whst things is error

Hi, @nil921922
What model Arduino is your microcontroller?

Have you loaded the lvgl.h display properly?

Tom.... :smiley: :+1: :coffee: :australia:

To help you understand errors like this, you just need to read carefully what's written. It says the file "lv_conf_internal.h", one of the lvgl library source codes, at row 60 tries to include "../../lv_conf.h" and this file doesn't exist at that location (2 levels below the one containing "lv_conf_internal.h").
I don't know that library, but you get this kind of error when a library hasn't been correctly/fully installed or some kind of misconfiguration.
Go to your library path "c:\users\nelson\documents\arduino\libraries\lvgl\src", open the "lv_conf_internal.h" file with a text editor and see what happens at line 60:

Giving that code, I think you haven't (for any reason I don't know) defined the "LV_CONF_INCLUDE_SIMPLE". If you do it (read the library instructions to know why and how), that code won't ever try to include a non-existent header file.
Good luck!

Did you write this code, or is it generated by something else like AI?

Hi,
Where did you get the code?

Tom.... :smiley: :+1: :coffee: :australia:

Shhh don't ask... :sunglasses:

You need to install the 'lvgl' library.

You can do it from the Library Manager,

He already have it, otherwise he'd have immediately received an error on line 5 ("#include <lvgl.h>").
The problem is that he either didn't install it correctly, or he needs to configure it correctly (see my post #4)

Where are your post? (link)

My board is Arduino NANO

Scroll up, then search "post #4".
I can't believe I had to explain this...

At the moment this is irrelevant. The first thing you need to figure out is whether the library was installed correctly (and if so uninstall and reinstall it from Library Manager), then how it is to be used. And if it's compatible with Arduino Nano.
If you wrote such kinda complicated code, I think you'll be able to perform this quite simple task. If not, and you don't know what tyou're doing, well, you better ask ChatGPT.

Not likely to run on a Nano

https://docs.lvgl.io/master/intro/introduction.html#requirements

Yes, but for someone's help:

libraries
β”œβ”€β”€ Adafruit_BusIO
β”œβ”€β”€ Adafruit_GFX_Library
β”œβ”€β”€ Adafruit_ILI9341
...
β”œβ”€β”€ TFT_eSPI
β”œβ”€β”€ XPT2046_Touchscreen
β”œβ”€β”€ lv_conf.h               <=== Here!!
└── lvgl
    β”œβ”€β”€ CMakeLists.txt
    β”œβ”€β”€ CMakePresets.json
    β”œβ”€β”€ Kconfig
    β”œβ”€β”€ LICENCE.txt
    β”œβ”€β”€ README.md
    β”œβ”€β”€ SConscript
    β”œβ”€β”€ component.mk
    β”œβ”€β”€ demos
    β”œβ”€β”€ docs
    β”œβ”€β”€ env_support
    β”œβ”€β”€ examples
    β”œβ”€β”€ idf_component.yml
    β”œβ”€β”€ library.json
    β”œβ”€β”€ library.properties
    β”œβ”€β”€ lv_conf_template.h  ==> Configure & Save as `lv_conf.h` next to `lvgl`
    β”œβ”€β”€ lv_version.h
    β”œβ”€β”€ lv_version.h.in
    β”œβ”€β”€ lvgl.h
    β”œβ”€β”€ lvgl.mk
    β”œβ”€β”€ lvgl.pc.in
    β”œβ”€β”€ scripts
    β”œβ”€β”€ src
    β”œβ”€β”€ tests
    └── zephyr

OK! Thank you :grinning:

Nope. Flat out won't run on a Nano.
Believe me, I tried :frowning:

1 Like

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