Hello guys,
currently im working on a blink without delay sketch in combination with an LCD touchscreen.
The goal is to blinck 4 leds consecutive for the same period of time, if page 1 is the current page displayed at the screen.. Means i want to switch on the first LED at time period 0-200ms, second LED 201-400ms and so on.
The challenge is the following: If i test the sketch without Touchscreen, it works well!
Im using this one:
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long previousMillis4 = 0;// speichert den Zeitpunkt an dem zuletzt geschalten wurde
const long intervalon1 = 400;
const long intervaloff1 = 1200;
const long intervalon2 = 400;
const long intervaloff2 = 1200;
const long intervalon3 = 400;
const long intervaloff3 = 1200;
const long intervalon4 = 400;
const long intervaloff4 = 1200;
void setup() {
pinMode(A1, OUTPUT); // LED 1
pinMode(A2, OUTPUT); // LED 1
pinMode(A3, OUTPUT); // LED 1
pinMode(A4, OUTPUT); // LED 1
}
void loop(){
startventilate();
}
void startventilate()
{
unsigned long currentMillis = millis();
if(digitalRead(A1) == HIGH && currentMillis - previousMillis1 > intervalon1 ) {
previousMillis1 = currentMillis;
digitalWrite(A1,LOW);
digitalWrite(A1, digitalRead(A1));
}
if(digitalRead(A1) == LOW && currentMillis - previousMillis1 > intervaloff1) {
previousMillis1 = currentMillis;
digitalWrite(A1,HIGH);
digitalWrite(A1, digitalRead(A1));
}
if(digitalRead(A2) == HIGH && currentMillis+400 - previousMillis2 > intervalon2) {
previousMillis2 = currentMillis+400;
digitalWrite(A2,LOW);
digitalWrite(A2, digitalRead(A2));
}
if(digitalRead(A2) == LOW && currentMillis+400 - previousMillis2 > intervaloff2) {
previousMillis2 = currentMillis+400;
digitalWrite(A2,HIGH);
digitalWrite(A2, digitalRead(A2));
}
if(digitalRead(A3) == HIGH && currentMillis+800 - previousMillis3 > intervalon3) {
previousMillis3 = currentMillis+800;
digitalWrite(A3,LOW);
digitalWrite(A3, digitalRead(A3));
}
if(digitalRead(3) == LOW && currentMillis+800 - previousMillis3 > intervaloff3) {
previousMillis3 = currentMillis+800;
digitalWrite(A3,HIGH);
digitalWrite(A3, digitalRead(A3));
}
if(digitalRead(A4) == HIGH && currentMillis+1200 - previousMillis4 > intervalon4) {
previousMillis4 = currentMillis+1200;
digitalWrite(A4,LOW);
digitalWrite(A4, digitalRead(A4));
}
if(digitalRead(4) == LOW && currentMillis+1200 - previousMillis4 > intervaloff4) {
previousMillis4 = currentMillis+1200;
digitalWrite(A4,HIGH);
digitalWrite(A4, digitalRead(A4));
}
}
If we´re using the same code with the TFT touchscreen and all the librarys, it does not work. What happens is, that LED1, LED2, LED3 are blinking for about 400ms and then switching for about an second off.
Do you have any idea, why this error occurs? Could it be a timing problem caused by the processor, or is there a fault in the code?
Im really looking forward to your answers mates!
best regards
Gustav
Code:
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "URTouch.h"
#define TFT_DC 2
#define TFT_CS 10
#define TFT_RST 8
#define TFT_MISO 12
#define TFT_MOSI 11
#define TFT_CLK 13
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
#define t_SCK 3
#define t_CS 4
#define t_MOSI 5
#define t_MISO 6
#define t_IRQ 7
#define BLUE 0x001F
#define GREEN 0x07E0
#define WHITE 0xFFFF
#define BLACK 0x0000
#define YELLOW 0xFFF0
#define RED 0xF800
char currentpage;
URTouch ts(t_SCK, t_CS, t_MOSI, t_MISO, t_IRQ);
int x, y;
// definition Parameter for: void startventilate
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long previousMillis4 = 0;
const long intervalon1 = 400;
const long intervaloff1 = 1200;
const long intervalon2 = 400;
const long intervaloff2 = 1200;
const long intervalon3 = 400;
const long intervaloff3 = 1200;
const long intervalon4 = 400;
const long intervaloff4 = 1200;
//////////////////////////////
void setup(){
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT);
Serial.begin(9600);
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_WHITE);
ts.InitTouch();
ts.setPrecision(PREC_EXTREME);
currentpage = '0'; // standard starting page after booting is home screen
drawhomescreen();
}
void loop(){
// home screen setup
if (currentpage == '0'){
if(ts.dataAvailable()){
ts.read();
x = ts.getX();
y = ts.getY();
if((x>=50) && (x<=125) && (y>=20) && (y<=60)){ // check, if click is within coordinates
ventilate();
currentpage = '1';
}
}
}
// snippet zur entlüftung
if (currentpage == '1'){
if(ts.dataAvailable()){
ts.read();
x = ts.getX();
y = ts.getY();
if ((x>=0) && (x<=320) && (y>=0) && (y<=240)){
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
currentpage = '2';
}
}
startventilate(); // entlüftung der düsen
}
}
void ventilate(){
tft.fillScreen(ILI9341_WHITE);
tft.setTextColor(BLACK);
tft.setCursor(50,20);
tft.setTextSize(3);
tft.print("Einstellen");
tft.setCursor(50,50);
tft.setTextSize(2);
tft.print("3,0 Bar Druck ");
//fertig button
tft.fillRect (90,160,140,40, BLUE);
tft.drawRect (90,160,140,40, WHITE);
tft.setCursor(100,170);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("FERTIG");
}
void drawhomescreen(){
// creating home screen
tft.fillScreen(ILI9341_WHITE);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(1);
tft.setCursor(80,5);
tft.print("Duesengroesse waehlen");
tft.fillRect (50,20,75,40, BLUE);
tft.drawRect (50,20,75,40, BLACK);
tft.setCursor(65,30);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("400");
tft.fillRect (200,20,75,40, BLUE);
tft.drawRect (200,20,75,40, BLACK);
tft.setCursor(215,30);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("470");
tft.fillRect (50,90,75,40, BLUE);
tft.drawRect (50,90,75,40, BLACK);
tft.setCursor(65,100);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("600");
tft.fillRect (50,160,75,40, BLUE);
tft.drawRect (50,160,75,40, BLACK);
tft.setCursor(65,170);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("900");
}
void startventilate(){
unsigned long currentMillis = millis();
if(digitalRead(A1) == HIGH && currentMillis - previousMillis1 > intervalon1 ) {
previousMillis1 = currentMillis;
digitalWrite(A1,LOW);
digitalWrite(A1, digitalRead(A1));
}
if(digitalRead(A1) == LOW && currentMillis - previousMillis1 > intervaloff1) {
previousMillis1 = currentMillis;
digitalWrite(A1,HIGH);
digitalWrite(A1, digitalRead(A1));
}
if(digitalRead(A2) == HIGH && currentMillis+400 - previousMillis2 > intervalon2) {
previousMillis2 = currentMillis+400;
digitalWrite(A2,LOW);
digitalWrite(A2, digitalRead(A2));
}
if(digitalRead(A2) == LOW && currentMillis+400 - previousMillis2 > intervaloff2) {
previousMillis2 = currentMillis+400;
digitalWrite(A2,HIGH);
digitalWrite(A2, digitalRead(A2));
}
if(digitalRead(A3) == HIGH && currentMillis+800 - previousMillis3 > intervalon3) {
previousMillis3 = currentMillis+800;
digitalWrite(A3,LOW);
digitalWrite(A3, digitalRead(A3));
}
if(digitalRead(3) == LOW && currentMillis+800 - previousMillis3 > intervaloff3) {
previousMillis3 = currentMillis+800;
digitalWrite(A3,HIGH);
digitalWrite(A3, digitalRead(A3));
}
if(digitalRead(A4) == HIGH && currentMillis+1200 - previousMillis4 > intervalon4) {
previousMillis4 = currentMillis+1200;
digitalWrite(A4,LOW);
digitalWrite(A4, digitalRead(A4));
}
if(digitalRead(4) == LOW && currentMillis+1200 - previousMillis4 > intervaloff4) {
previousMillis4 = currentMillis+1200;
digitalWrite(A4,HIGH);
digitalWrite(A4, digitalRead(A4));
}
}