LilyGO T-Display-S3 and photoelectric sensor

plotto
Posts: 1
Joined: Sat Nov 09, 2024 4:25 am

LilyGO T-Display-S3 and photoelectric sensor

Postby plotto » Sat Nov 09, 2024 4:31 am

Hi, new to the forum.
I have a LilyGO T-Display-S3 and have the touch screen and the UI all functioniung correctly.
I am strugling with the pins and connecting the photoelectric sensor and getting it to work.
Which pins to use seems a little confusing, or it may be my noob coding skills.

If any one has some advise or help on getting this to work it would be much appreciative.

Code: Select all

#include <lvgl.h>
#include <TFT_eSPI.h>
#include <ui.h>
#include <CST816S.h>




static const uint16_t screenWidth  = 170;
static const uint16_t screenHeight = 320;

static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[ screenWidth * screenHeight / 10 ];

unsigned long previousMillis = 0;

const long interval = 1000;

int current=0;
int number=0;
int sensor=18;
int actuator=20;
int count=0;

TFT_eSPI tft = TFT_eSPI(); 
CST816S myTouch(18, 17, 21, 16); 

#if LV_USE_LOG != 0



void my_print(const char * buf)
{
    Serial.printf(buf);
    Serial.flush();
}
#endif


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.startWrite();
    tft.setAddrWindow( area->x1, area->y1, w, h );
    tft.pushColors( ( uint16_t * )&color_p->full, w * h, true );
    tft.endWrite();

    lv_disp_flush_ready( disp );
}


void my_touchpad_read( lv_indev_drv_t * indev_driver, lv_indev_data_t * data )
{
    uint16_t touchX = 0, touchY = 0;

  if (myTouch.available()) {
    touchX = myTouch.data.x;
    touchY = myTouch.data.y;
    data->state = LV_INDEV_STATE_PR;
  } else {
    data->state = LV_INDEV_STATE_REL;
   }


  data->point.x = touchX;
  data->point.y = touchY;


  if (touchX != 0) {
    Serial.print("Data x ");
    Serial.println(touchX);
  }

  if (touchY != 0) {
    Serial.print("Data y ");
    Serial.println(touchY);
  }
}

void setup()
{
    Serial.begin( 115200 ); 

    String LVGL_Arduino = "Hello Arduino! ";
    LVGL_Arduino += String('V') + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();

    Serial.println( LVGL_Arduino );
    Serial.println( "I am LVGL_Arduino" );

    lv_init();

pinMode(sensor, INPUT);
pinMode(actuator, OUTPUT);

#if LV_USE_LOG != 0
    lv_log_register_print_cb( my_print ); 
#endif

    tft.begin();          
    tft.setRotation( 0 ); 
    myTouch.begin(); 
    lv_disp_draw_buf_init( &draw_buf, buf, NULL, screenWidth * screenHeight / 10 );

   
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init( &disp_drv );
   
    disp_drv.hor_res = screenWidth;
    disp_drv.ver_res = screenHeight;
    disp_drv.flush_cb = my_disp_flush;
    disp_drv.draw_buf = &draw_buf;
    lv_disp_drv_register( &disp_drv );

    
    static lv_indev_drv_t indev_drv;
    lv_indev_drv_init( &indev_drv );
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = my_touchpad_read;
    lv_indev_drv_register( &indev_drv );


    ui_init();

    Serial.println( "Setup done" );
}
void increase(lv_event_t * e)
{
  number++;
}

void reset(lv_event_t * e)
{
  number=0;
  count=0;
  current=0;
}
void loop()
{
   unsigned long currentMillis = millis();
    lv_timer_handler(); 
    delay(5);
    _ui_label_set_property(ui_Label1, _UI_LABEL_PROPERTY_TEXT, String(number).c_str());
    _ui_label_set_property(ui_Label5, _UI_LABEL_PROPERTY_TEXT, String(current).c_str());
int buttonState = digitalRead(sensor);

if (buttonState = LOW)
{
count++;
current++;}


if (count>=number)
{
digitalWrite(actuator, 1);
 
}
  if (currentMillis - previousMillis >= interval) {
    
    previousMillis = currentMillis;
    if (digitalRead(actuator == 1)) {
digitalWrite(actuator, 0);} 
else {digitalWrite(actuator, 1);
count=0;}

  }
}

aliarifat794
Posts: 198
Joined: Sun Jun 23, 2024 6:18 pm

Re: LilyGO T-Display-S3 and photoelectric sensor

Postby aliarifat794 » Sat Nov 09, 2024 11:06 am

It looks like there are some errors in the code. Please correct it:

Code: Select all

#include <lvgl.h>
#include <TFT_eSPI.h>
#include <ui.h>
#include <CST816S.h>

static const uint16_t screenWidth  = 170;
static const uint16_t screenHeight = 320;

static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[ screenWidth * screenHeight / 10 ];

unsigned long previousMillis = 0;
const long interval = 1000;

int current = 0;
int number = 0;
int sensor = 18;       // Adjust if needed
int actuator = 20;     // Adjust if needed
int count = 0;

TFT_eSPI tft = TFT_eSPI(); 
CST816S myTouch(18, 17, 21, 16); 

#if LV_USE_LOG != 0
void my_print(const char * buf) {
    Serial.printf(buf);
    Serial.flush();
}
#endif

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.startWrite();
    tft.setAddrWindow(area->x1, area->y1, w, h);
    tft.pushColors((uint16_t *) &color_p->full, w * h, true);
    tft.endWrite();

    lv_disp_flush_ready(disp);
}

void my_touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) {
    uint16_t touchX = 0, touchY = 0;

    if (myTouch.available()) {
        touchX = myTouch.data.x;
        touchY = myTouch.data.y;
        data->state = LV_INDEV_STATE_PR;
    } else {
        data->state = LV_INDEV_STATE_REL;
    }

    data->point.x = touchX;
    data->point.y = touchY;

    if (touchX != 0) {
        Serial.print("Data x ");
        Serial.println(touchX);
    }

    if (touchY != 0) {
        Serial.print("Data y ");
        Serial.println(touchY);
    }
}

void setup() {
    Serial.begin(115200); 
    lv_init();

    pinMode(sensor, INPUT);
    pinMode(actuator, OUTPUT);

#if LV_USE_LOG != 0
    lv_log_register_print_cb(my_print); 
#endif

    tft.begin();          
    tft.setRotation(0); 
    myTouch.begin(); 
    lv_disp_draw_buf_init(&draw_buf, buf, NULL, screenWidth * screenHeight / 10);

    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.hor_res = screenWidth;
    disp_drv.ver_res = screenHeight;
    disp_drv.flush_cb = my_disp_flush;
    disp_drv.draw_buf = &draw_buf;
    lv_disp_drv_register(&disp_drv);

    static lv_indev_drv_t indev_drv;
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = my_touchpad_read;
    lv_indev_drv_register(&indev_drv);

    ui_init();
    Serial.println("Setup done");
}

void increase(lv_event_t * e) {
    number++;
}

void reset(lv_event_t * e) {
    number = 0;
    count = 0;
    current = 0;
}

void loop() {
    unsigned long currentMillis = millis();
    lv_timer_handler();
    delay(5);

    _ui_label_set_property(ui_Label1, _UI_LABEL_PROPERTY_TEXT, String(number).c_str());
    _ui_label_set_property(ui_Label5, _UI_LABEL_PROPERTY_TEXT, String(current).c_str());

    int buttonState = digitalRead(sensor);

    if (buttonState == LOW) {
        count++;
        current++;
    }

    if (count >= number) {
        digitalWrite(actuator, HIGH);
    }

    if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;

        if (digitalRead(actuator) == HIGH) {
            digitalWrite(actuator, LOW);
        } else {
            digitalWrite(actuator, HIGH);
            count = 0;
        }
    }
}

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], pepgra and 166 guests