working on my first arduino project a spot welder to make battery packs.
ive put in functionality to change timings for "heat", "delay" and "weld" via an LCD menu.
i had quite some problems with a bouncy rotary encoder, then found some handy code on the web to debounce the encoder. all works well now, but as soon as i add the "write to lcd" code (currently commented out), the rotary encoder output starts bouncing again.
i think the write to lcd (or any other code) takes too long and this causes the problem.
im thinking of using an interupt to "write to lcd" with a delay of ~100ms after the encoder has stopped turning or something like that. (the normal if statements with delays dont seem to work)
full code below, any tips on this issue (or any other comments on my first cpp code) would be great!
thanks
S
//#define PinCLK 6
//#define PinDT 7
//#define PinSW 8 //<<< what do these do???!! xxx
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
// Rotary Encoder Module connections
const int PinSW=8; // Rotary Encoder Switch PIN number
const int PinDT=7; // DATA signal
const int PinCLK=6; // CLOCK signal
const int TriggerSwitch = 2;
const int RelayOutput = 12;
int EncoderButtonState = 0;
int TriggerSwitchState = 0 ;
int RelayState = 0 ;
int SelectedTimeIncrement = 1; //
int TimeIncrementOptions[] = {1,10,100,1000} ;
int Index1 = 0 ;
int Index2 = 0 ;
// Variables to debounce Rotary Encoder
unsigned long TimeOfLastDebounce = 0;
int DelayofDebounce = 1 ;//0.01;
//Variable to debounce LCD
unsigned long TimeOfLastLCDwrite;
int LCDdelay = 300;
// Store previous Pins state
int PreviousCLK;
int PreviousDATA;
int TimeVariableStartValues[] = {50, 20, 150} ;
//int x ; //dummy variable
int temp ;
unsigned long lastButtonPress = 0; //used to debounce rotary encoder BUTTON
void setup() {
// Put current pins state in variables
PreviousCLK=digitalRead(PinCLK);
PreviousDATA=digitalRead(PinDT);
Serial.begin (9600);
// Set the Switch pin to use Arduino PULLUP resistors
pinMode(PinSW, INPUT_PULLUP);
pinMode(PinDT, INPUT) ; //_PULLUP);
pinMode(PinCLK, INPUT) ; // _PULLUP);
//SET UP LCD
lcd.init();
lcd.backlight();
// WriteResultsToLCD (SelectedTimeIncrement , TimeVariableStartValues[0], TimeVariableStartValues[1], TimeVariableStartValues[2]) ;
}
void loop() {
// If enough time has passed check the rotary encoder
if ((millis() - TimeOfLastDebounce) > DelayofDebounce) {
check_rotary(); // Rotary Encoder check routine below
PreviousCLK=digitalRead(PinCLK);
PreviousDATA=digitalRead(PinDT);
TimeOfLastDebounce=millis();
}
}
// Check if Rotary Encoder was moved
void check_rotary() {
if ((PreviousCLK == 0) && (PreviousDATA == 1)) {
if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 0)) {
Toggle_UP_function() ;
}
if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 1)) {
Toggle_DOWN_function();
}
}
if ((PreviousCLK == 1) && (PreviousDATA == 0)) {
if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 1)) {
Toggle_UP_function();
}
if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 0)) {
Toggle_DOWN_function();
}
}
if ((PreviousCLK == 1) && (PreviousDATA == 1)) {
if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 1)) {
Toggle_UP_function();
}
if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 0)) {
Toggle_DOWN_function();
}
}
if ((PreviousCLK == 0) && (PreviousDATA == 0)) {
if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 0)) {
Toggle_UP_function();
}
if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 1)) {
Toggle_DOWN_function();
}
}
}
void Toggle_UP_function(){
if (Index1 == 0) {
if (Index2==3) (Index2=0);
else if (Index2 ++ );
}
else if (Index1 != 0) {
temp = Index1 -1 ;
TimeVariableStartValues[temp] ++ ;
}
if ((millis()- TimeOfLastLCDwrite) > LCDdelay){
// WriteResultsToLCD (SelectedTimeIncrement , TimeVariableStartValues[0], TimeVariableStartValues[1], TimeVariableStartValues[2]) ;
}
}
void Toggle_DOWN_function(){
if (Index1 == 0) {
if (Index2==0) (Index2=3);
else if (Index2 --
}
else if (Index1 != 0) {
temp = Index1 -1 ;
TimeVariableStartValues[temp] -- ;
}
if ((millis()- TimeOfLastLCDwrite) > LCDdelay){
// WriteResultsToLCD (SelectedTimeIncrement , TimeVariableStartValues[0], TimeVariableStartValues[1], TimeVariableStartValues[2]) ;
}
}
void WriteResultsToLCD (int a, int b, int c, int d) {
lcd.setCursor(0,0);
lcd.print("SET INCR,MSec ");
lcd.setCursor(0,1);
lcd.print("HEAT ");
lcd.setCursor(0,2);
lcd.print("DELAY ");
lcd.setCursor(0,3);
lcd.print("WELD ");
lcd.setCursor(14,0);
lcd.print(a);
lcd.setCursor(8,1);
lcd.print(b);
lcd.setCursor(8,2);
lcd.print(c);
lcd.setCursor(8,3);
lcd.print(d);
TimeOfLastLCDwrite = millis();
}
2020_06_04_SpotWelder_2.0.ino (7.76 KB)