Hi, i have this sketch with 1 lcd, 3 relays and 3 pushbuttons, and it work fine...
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#define TFT_CS 10
#define TFT_RST 9 // you can also connect this to the Arduino reset
#define TFT_DC 8
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
#define TFT_SCLK 13 // set these to be whatever pins you like!
#define TFT_MOSI 11 // set these to be whatever pins you like!
const int pushButton[] ={2,3,4};// define push button inputs
const int relayPin[]={14,15,16};// output pins where 3 relays will be connected
int pushed[] ={0,0,0};// status of each buttons
int relayStatus[] ={LOW,LOW,LOW};// initial status of relay
//leds
int led1 = 5;
void setup() {
pinMode(led1,OUTPUT);
Serial.begin(9600);// initialize serial monitor
tft.initR(INITR_GREENTAB); // Farbmarke Schutzfolie achten
tft.setCursor(0,0);
tft.fillScreen(ST7735_BLACK); // Display loeschen
tft.setTextColor(ST7735_YELLOW); // Textfarbe waehlen
tft.setTextSize(1); // kleinste Schriftgroeße
tft.setCursor(0,0);
tft.println();
tft.println(F(" Controlo de Aquario")); // Infotext ausgeben
tft.drawLine(0, 20, tft.width()-1, 20, ST7735_WHITE);
tft.setTextColor(ST7735_GREEN);
tft.setTextSize(1);
tft.setCursor(0, 30);
tft.println("Bomba Esq");
tft.setCursor(0, 40);
tft.println("Bomba Dir");
tft.setCursor(0, 50);
tft.println("CO2");
tft.drawLine(0, 64, tft.width()-1, 64, ST7735_WHITE);
tft.setCursor(0, 70);
tft.setTextColor(ST7735_WHITE);
/*quadro luzes*/
tft.setTextColor(ST7735_YELLOW);
tft.println(" Estado das Luzes");
tft.setTextColor(ST7735_GREEN);
tft.setCursor(10, 85);
tft.println("Luar");
tft.setCursor(10, 95);
tft.println("Nascer do Sol");
tft.setCursor(10, 105);
tft.println("Meio Dia");
tft.setCursor(10, 115);
tft.println("Por do Sol");
for(int i=0; i<3; i++)
{
pinMode(pushButton[i], INPUT_PULLUP);
pinMode(relayPin[i], OUTPUT);
digitalWrite(relayPin[i], HIGH);// initial relay status to be OFF
}
}
void loop() {
for(int i=0; i<3; i++)
{
int val = digitalRead(pushButton[i]);
if(val == HIGH && relayStatus[i] == LOW){
pushed[i] = 1-pushed[i];
delay(100);
}// if
relayStatus[i] = val;
if(pushed[2] == HIGH){
tft.fillCircle(70,34,3,ST7735_GREEN);
digitalWrite(relayPin[2], LOW);
}else{
tft.fillCircle(70,34,3,ST7735_RED);
digitalWrite(relayPin[2], HIGH);
if(pushed[1] == HIGH){
tft.fillCircle(70,44,3,ST7735_GREEN);
digitalWrite(relayPin[1], LOW);
}else{
tft.fillCircle(70,44,3,ST7735_RED);
digitalWrite(relayPin[1], HIGH);
if(pushed[0] == HIGH){
tft.fillCircle(70,54,3,ST7735_GREEN);
digitalWrite(relayPin[0], LOW);
}else{
tft.fillCircle(70,54,3,ST7735_RED);
digitalWrite(relayPin[0], HIGH);
}
}
}
}
when i try to had one led to light up with a specific time the pushbuttons don´t work, or depending on the led delay they "kind" off work.
void acendeleds() {
//para leds
analogWrite(led1,10); //luar
tft.fillCircle(110,86,3,ST7735_GREEN);
delay(100);
tft.fillCircle(110,86,3,ST7735_RED);
analogWrite(led1,127); // nascer do sol
tft.fillCircle(110,96,3,ST7735_GREEN);
delay(100);
tft.fillCircle(110,96,3,ST7735_RED);
analogWrite(led1,250); // meio dia
tft.fillCircle(110,106,3,ST7735_GREEN);
delay(100);
tft.fillCircle(110,106,3,ST7735_RED);
analogWrite(led1,63); // por do sol
tft.fillCircle(110,116,3,ST7735_GREEN);
delay(100);
tft.fillCircle(110,116,3,ST7735_RED);
/*analogWrite(led1,255);
tft.fillCircle(110,96,3,ST7735_GREEN);
delay(5000);
tft.fillCircle(110,96,3,ST7735_RED);
*/
}
The full code
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#define TFT_CS 10
#define TFT_RST 9 // you can also connect this to the Arduino reset
#define TFT_DC 8
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
#define TFT_SCLK 13 // set these to be whatever pins you like!
#define TFT_MOSI 11 // set these to be whatever pins you like!
const int pushButton[] ={2,3,4};// define push button inputs
const int relayPin[]={14,15,16};// output pins where 3 relays will be connected
int pushed[] ={0,0,0};// status of each buttons
int relayStatus[] ={LOW,LOW,LOW};// initial status of relay
//leds
int led1 = 5;
void setup() {
pinMode(led1,OUTPUT);
Serial.begin(9600);// initialize serial monitor
tft.initR(INITR_GREENTAB); // Farbmarke Schutzfolie achten
tft.setCursor(0,0);
tft.fillScreen(ST7735_BLACK); // Display loeschen
tft.setTextColor(ST7735_YELLOW); // Textfarbe waehlen
tft.setTextSize(1); // kleinste Schriftgroeße
tft.setCursor(0,0);
tft.println();
tft.println(F(" Controlo de Aquario")); // Infotext ausgeben
tft.drawLine(0, 20, tft.width()-1, 20, ST7735_WHITE);
tft.setTextColor(ST7735_GREEN);
tft.setTextSize(1);
tft.setCursor(0, 30);
tft.println("Bomba Esq");
tft.setCursor(0, 40);
tft.println("Bomba Dir");
tft.setCursor(0, 50);
tft.println("CO2");
tft.drawLine(0, 64, tft.width()-1, 64, ST7735_WHITE);
tft.setCursor(0, 70);
tft.setTextColor(ST7735_WHITE);
/*quadro luzes*/
tft.setTextColor(ST7735_YELLOW);
tft.println(" Estado das Luzes");
tft.setTextColor(ST7735_GREEN);
tft.setCursor(10, 85);
tft.println("Luar");
tft.setCursor(10, 95);
tft.println("Nascer do Sol");
tft.setCursor(10, 105);
tft.println("Meio Dia");
tft.setCursor(10, 115);
tft.println("Por do Sol");
for(int i=0; i<3; i++)
{
pinMode(pushButton[i], INPUT_PULLUP);
pinMode(relayPin[i], OUTPUT);
digitalWrite(relayPin[i], HIGH);// initial relay status to be OFF
}
}
void loop() {
for(int i=0; i<3; i++)
{
int val = digitalRead(pushButton[i]);
if(val == HIGH && relayStatus[i] == LOW){
pushed[i] = 1-pushed[i];
delay(100);
}// if
relayStatus[i] = val;
if(pushed[2] == HIGH){
tft.fillCircle(70,34,3,ST7735_GREEN);
digitalWrite(relayPin[2], LOW);
}else{
tft.fillCircle(70,34,3,ST7735_RED);
digitalWrite(relayPin[2], HIGH);
if(pushed[1] == HIGH){
tft.fillCircle(70,44,3,ST7735_GREEN);
digitalWrite(relayPin[1], LOW);
}else{
tft.fillCircle(70,44,3,ST7735_RED);
digitalWrite(relayPin[1], HIGH);
if(pushed[0] == HIGH){
tft.fillCircle(70,54,3,ST7735_GREEN);
digitalWrite(relayPin[0], LOW);
}else{
tft.fillCircle(70,54,3,ST7735_RED);
digitalWrite(relayPin[0], HIGH);
}
}
}
}
//delay(100);
acendeleds();
}
/*
//---------------------//
0 = 0%
25 = 10%
63 = 25%
127 = 50%
190 = 75%
255 = 100%
//-------------------//
1000=1 seg
60000=1min
3600000=1hora
//
*/
void acendeleds() {
//para leds
analogWrite(led1,10); //luar
tft.fillCircle(110,86,3,ST7735_GREEN);
delay(100);
tft.fillCircle(110,86,3,ST7735_RED);
analogWrite(led1,127); // nascer do sol
tft.fillCircle(110,96,3,ST7735_GREEN);
delay(100);
tft.fillCircle(110,96,3,ST7735_RED);
analogWrite(led1,250); // meio dia
tft.fillCircle(110,106,3,ST7735_GREEN);
delay(100);
tft.fillCircle(110,106,3,ST7735_RED);
analogWrite(led1,63); // por do sol
tft.fillCircle(110,116,3,ST7735_GREEN);
delay(100);
tft.fillCircle(110,116,3,ST7735_RED);
/*analogWrite(led1,255);
tft.fillCircle(110,96,3,ST7735_GREEN);
delay(5000);
tft.fillCircle(110,96,3,ST7735_RED);
*/
}
Can anyone give a view to see what am i doing wrong?
Thank you