Sto realizzando un piccolo programmino per pilotare una saldatrice per batterie con le ultime parti che ho a disposizione.
Il sistema è cosi composto:
- NodeMCU ESP-12F
- Display TFT 1.14' ST7789
- Encoder rotativo
- Rele 5V optoisolato
Il programma consente di impostare i millisecondi di durata della saldatrice, routando l'encoder si aumentano o diminuiscono i millisecondi, premendo il pulsante dell'encoder si aumenta/diminuisce il contenggio di 1 in 1 o di 10 in 10.
Ho la necessità di avere un ulteriore pulsante che è quello che avvia la saldatrice per il numero di secondi impostato.
Purtroppo non riesco ad aggiungere un secondo pulsante in quanto non appena utilizzo uno dei pin "liberi" il programma si resetta in continuazione.
Ho provato in mille modi ma senza ottenere un risultato, dove sbaglio ?.
#include <TFT_eSPI.h> // Graphics and font library for ILI9341 driver chip
#include <SPI.h>
TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h
#define CLK 4 // D2 - GPIO4
#define DT 5 // D1 - GPIO5
#define SW 16 // D0 - GPIO16
#define RLY 2 // D4 - GPIO2
#define WB 10 // SD3 - GPIO10
bool pressed=false;
int row, col, lastStateCLK, currentStateCLK, address, counter, m, n;
unsigned long lastButtonPress = 0;
unsigned long lastButtonPress2 = 0;
// Start reading from the first byte (address 0) of the EEPROM
byte value;
char numero[4];
void setup() {
Serial.begin(115200);
delay(3000);
address = 0; // read eeprom then assign counter value
counter = 0;
// Set encoder pins as inputs
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW, INPUT_PULLUP);
//pinMode(WB, INPUT_PULLUP);
//pinMode(9,INPUT_PULLUP);
//pinMode(8,INPUT_PULLUP);
// pinMode(11,INPUT_PULLUP);
// pinMode(7,INPUT_PULLUP);
// pinMode(6,INPUT_PULLUP);
pinMode(RLY,OUTPUT);
digitalWrite(RLY, HIGH);
// Read the initial state of CLK
lastStateCLK = digitalRead(CLK);
tft.init();
tft.begin();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_BLACK, TFT_BLACK);
// Print splash message to the LCD.
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
col = 20;
row = 30;
tft.drawString("Welcome to xxxxx", col, row, 4);
row = 80;
tft.drawString("Welding Machine", col, row, 4);
delay(5000);
col = 10;
row = 30;
m = sprintf(numero, " %3d ", counter,n);
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.drawString(numero, col, row, 8);
}
void loop() {
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
// Read the button state
int btnState = digitalRead(SW);
int WeldState = digitalRead(WB);
// If last and current state of CLK are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK) {
if (pressed){
counter ++;}
else {
counter = counter + 10;
}
// Set maximum admitted values
if (counter >= 240) {counter = 240;}
m = sprintf(numero, " %3d ", counter,n);
tft.drawString(numero, col, row, 8);
} else {
if (pressed){
// Encoder is rotating CW so increment
counter --;}
else {
counter = counter - 10;
}
// Set minimum admitted values
if (counter <= 0) {counter = 0;}
m = sprintf(numero, " %3d ", counter,n);
tft.drawString(numero, col, row, 8);
}
Serial.println(counter);
}
// If we detect LOW signal, button is pressed
if (btnState == LOW) {
// if 50ms have passed since last LOW pulse, it means that the
// button has been pressed, released and pressed again
if (millis() - lastButtonPress >= 50) {
if (pressed == false) {
Serial.println("Counter x 1");
pressed = true;}
else {
Serial.println("Counter x 10");
pressed = false;
}
}
// Remember last button press event
lastButtonPress = millis();
}
// If we detect LOW signal, welding button is pressed
if (WeldState == LOW) {
// if 50ms have passed since last LOW pulse, it means that the
// button has been pressed, released and pressed again
if (millis() - lastButtonPress2 >= 50) {
Serial.println("Welding done");
if (value != counter){
value = counter;
digitalWrite(RLY, LOW);
delay(counter);
digitalWrite(RLY, HIGH);
Serial.println("WRITE EEPROM");
// One simple call, with the address first and the object second.
// EEPROM.put(address, value);
}
}
}
lastButtonPress2 = millis();
// Remember last CLK state
lastStateCLK = currentStateCLK;
// Put in a slight delay to help debounce the reading
delay(1);
}
// END