Hi,
I want to control 4 relays via creating four virtual buttons in aTFT .
Can anyone help me in the process?
Thanks in advance,
Antonio
Sure, just put on a pot of coffee and I'll be right over...
In other words, you told us nearly nothing about your project or your needs. Or, what you have already researched or done.
Have you done any basic internet research on this?
Welcome! Sorry I am an old man with poor eyesight and I cannot see your project or the work you have done. Be my eyes and explain what it is supposed to do, what parts you have chosen including links to technical information on them, azon is just sales info, and your proposed schematic we can go a long way in helping you resolve your problem. I have worked with relays that will switch thousands of amps at industrial voltages, I believe yours are much smaller, which ones are you looking at, what are the loads?
Hello
Get an arduino uno or mega with a 3.5" ili9486 tft, and a 4 relays card + wires.
Download the arduino IDE, and the MCUFriend_kbv library.
Relays card
Ili9486
Inject this, you will have a good start:
#if 1
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 100
#define MAXPRESSURE 2000
const int XP = 6, XM = A2, YP = A1, YM = 7;
const int TS_LEFT = 924, TS_RT = 210, TS_TOP = 959, TS_BOT = 203;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button relais1_btn, relais2_btn, relais3_btn, relais4_btn;
int pixel_x, pixel_y;
bool Touch_getXY(void)
{
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT);
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH);
digitalWrite(XM, HIGH);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed) {
pixel_x = map(p.y, TS_LEFT, TS_RT, 0, 480);
pixel_y = map(p.x, TS_BOT, TS_TOP, 0, 320);
}
return pressed;
}
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
//RELAIS
const int relais1 = 14;
const int relais2= 15;
const int relais3 = 16;
const int relais4 = 17;
void setup(void)
{
Serial.begin(9600);
uint16_t ID = tft.readID();
Serial.print("TFT ID = 0x");
Serial.println(ID, HEX);
Serial.println("Calibrate for your Touch Panel");
if (ID == 0xD3D3) ID = 0x9486; // write-only shield
tft.begin(ID);
tft.setRotation(1); //PAYSAGE
tft.fillScreen(BLACK);
//SPLASHSCREEN
Serial.println("ECRAN DEMARRAGE");
tft.fillScreen(BLACK);
tft.setTextColor(WHITE, BLACK);
tft.setTextSize(3);
tft.setCursor(160, 60);
tft.print("MY PROGRAM");
tft.setTextColor(WHITE, BLACK);
tft.setTextSize(3);
tft.setCursor(223 , 110);
tft.print("BY");
tft.setTextColor(WHITE, BLACK);
tft.setTextSize(3);
tft.setCursor(185, 180);
tft.print("JABBER");
tft.setCursor(145, 280);
tft.print("V14/05/2022");
delay(5000);
tft.fillScreen(BLACK);
//MENU SCREEN
relais1_btn.initButton(&tft, 75, 154, 146, 104, BLUE, BLACK, WHITE, "R1", 2);
relais1_btn.drawButton(true);
relais2_btn.initButton(&tft, 75, 260, 146, 104, BLUE, BLACK, WHITE, "R2", 2);
relais2_btn.drawButton(true);
relais3_btn.initButton(&tft, 405, 154, 146, 104, BLUE, BLACK, WHITE, "R3", 2);
relais3_btn.drawButton(true);
relais4_btn.initButton(&tft, 405, 260, 146, 104, BLUE, BLACK, WHITE, "R4", 2);
relais4_btn.drawButton(true);
tft.setCursor(200, 40);
tft.setTextColor(WHITE, BLACK);
tft.setTextSize(3);
tft.print("MENU");
Serial.println("ATTRIBUTION DES PINS");
pinMode(relais1, OUTPUT);
digitalWrite(relais1, LOW);
pinMode(relais2, OUTPUT);
digitalWrite(relais2, LOW);
pinMode(relais3, OUTPUT);
digitalWrite(relais3, LOW);
pinMode(relais4, OUTPUT);
digitalWrite(relais4, LOW);
}
void loop(void)
{
bool down = Touch_getXY();
relais1_btn.press(down && relais1_btn.contains(pixel_x, pixel_y));
relais2_btn.press(down && relais2_btn.contains(pixel_x, pixel_y));
relais3_btn.press(down && relais3_btn.contains(pixel_x, pixel_y));
relais4_btn.press(down && relais4_btn.contains(pixel_x, pixel_y));
if (relais1_btn.justPressed()) {
relais1func();
}
if (relais2_btn.justPressed()) {
relais2func();
}
if (relais3_btn.justPressed()) {
relais3func();
}
if (relais4_btn.justPressed()) {
relais4func();
}
}
void relais1func(){
digitalWrite(relais1, HIGH);
delay(5000);
digitalWrite(relais1, LOW);
}
void relais2func(){
digitalWrite(relais2, HIGH);
delay(5000);
digitalWrite(relais2, LOW);
}
void relais3func(){
digitalWrite(relais3, HIGH);
delay(5000);
digitalWrite(relais3, LOW);
}
void relais4func(){
digitalWrite(relais4, HIGH);
delay(5000);
digitalWrite(relais4, LOW);
}
#endif
You will have a splashscreen, and a menu with your 4 buttons, controlling 4 relays:
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.