Hello, I stopped my project at the point, when I need to display current position at my OLED. Im not able to make any working code. When "acivate" void updateDisplay, program stops. Last I tried to update it each 500 millis, this doesnot help me too. I cant slow down whole program because of rotary encoder too, it is skipping steps if I put any display function into the main loop too frequently. Thank you for any help.
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <AccelStepper.h>
#include <EZButton.h>
// PIN definitions motor driver
#define STEP_PIN 2 //step
#define DIR_PIN 5 //direction
#define EN_PIN 6 //stepper driver enable pin
// Define a stepper and the pins it will use 6400 steps per round with TCM2208 and NEMA 17 Creality 42-34
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); // (Type of driver: with 2 pins, STEP, DIR - for TMC2208 Aliexpres driver)
int motorMaxSpeed = 150; //50 maximum steps per second (about 3rps / at 16 microsteps)
int motorAccel = 10; //steps/second/second to accelerate
// rotary encoder
#define ENCODER_CLK 8
#define ENCODER_DT 3
#define BTN_1_PIN 9
#define BTN_1 0
int lastClk = HIGH;
int counterEnc = 0;
int pulse_cnt = 0;
unsigned long previousMillis = 0;
unsigned long previousPulseMillis = 0;
float filtered_rpm = 0.0;
int counter2 = 0;
void ReadButtons(bool *states, int num) {
//Read all button states however you want
states[BTN_1] = !digitalRead(BTN_1_PIN);
}
EZButton _ezb(1, ReadButtons, 10000, 15000, 50);
int dock = 1;
// 1.3 OLED I2C displej Aliexpress
#define i2c_Address 0x3C //initialize with the I2C addr 0x3C Typically eBay OLED's
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // QT-PY / XIAO
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Banner
#define PROJEKT "Turnout v1.1"
//relay 2 module
int relay_2 = 4;
int relay_1 = 7;
//DCC orientation timer
unsigned long previousPolarityMillis;
// 'OK', 64x64px
const unsigned char epd_bitmap_OK [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xe0, 0x38, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x10, 0x1e, 0x00, 0x00, 0x00,
0x00, 0x00, 0x78, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x38, 0x00, 0x00, 0x00,
0x00, 0x00, 0x18, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x7f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xe3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc3, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc1, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0xe0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0xc0, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x70, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0xc0, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x3c, 0x03, 0xff, 0x80,
0x00, 0x00, 0x00, 0xe0, 0x1e, 0x0f, 0xff, 0xc0, 0x00, 0x00, 0x00, 0xe0, 0x0f, 0x1f, 0xff, 0xc0,
0x00, 0x00, 0x00, 0xfe, 0x07, 0xfc, 0x01, 0xc0, 0x00, 0x0f, 0xff, 0xff, 0x03, 0xf8, 0x01, 0xc0,
0x00, 0x1f, 0xff, 0xff, 0x01, 0xf8, 0x01, 0xc0, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0,
0x00, 0x3c, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x38, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0,
0x00, 0x3c, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x1f, 0xe0, 0x00, 0x00, 0x38, 0x01, 0xc0,
0x00, 0x1f, 0xe0, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x1f, 0xe0, 0x00, 0x00, 0x38, 0x01, 0xc0,
0x00, 0x3c, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x38, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0,
0x00, 0x38, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0,
0x00, 0x1f, 0xe0, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x1f, 0xe0, 0x00, 0x00, 0x38, 0x01, 0xc0,
0x00, 0x3c, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x38, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0,
0x00, 0x38, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0,
0x00, 0x1f, 0xe0, 0x00, 0x00, 0x38, 0xe1, 0xc0, 0x00, 0x0f, 0xe0, 0x00, 0x00, 0x38, 0xe1, 0xc0,
0x00, 0x07, 0xc0, 0x00, 0x00, 0x38, 0xe1, 0xc0, 0x00, 0x07, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0,
0x00, 0x07, 0x00, 0x00, 0x01, 0xf8, 0x01, 0xc0, 0x00, 0x03, 0x80, 0x00, 0x0f, 0xf8, 0x01, 0xc0,
0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x01, 0xff, 0xff, 0xfe, 0x1f, 0xff, 0xc0,
0x00, 0x00, 0xff, 0xff, 0xf0, 0x0f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'gears-6281075_640', 128x64px
const unsigned char bitmap_gears [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x30, 0xc3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x70, 0xe7, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x77, 0xf0, 0xfe, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xe3, 0xc0, 0x3c, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xc1, 0x00, 0x08, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x1f, 0x83, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x3f, 0xe3, 0x80, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xf0, 0xf3, 0x80, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0xe1, 0xc0, 0x3b, 0x80, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0f, 0xc1, 0x83, 0x1b, 0x80, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0f, 0xc3, 0x87, 0xdb, 0x80, 0xc1, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0c, 0x03, 0x0c, 0x7f, 0x00, 0xf3, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0c, 0x03, 0x18, 0x3c, 0x00, 0x1e, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0c, 0x03, 0x30, 0x10, 0x00, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0c, 0x01, 0xe0, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0f, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0xe1, 0xe0, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xf0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x78, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x18, 0x00, 0x3e, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x38, 0x00, 0xff, 0x80, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x38, 0x03, 0xf8, 0xe0, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x30, 0x07, 0xff, 0x30, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x83, 0xf0, 0x0f, 0xc1, 0x90, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0xff, 0xff, 0x80, 0x58, 0x07, 0xf0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0xff, 0xff, 0x00, 0x4c, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0xff, 0xff, 0xc0, 0xd8, 0x03, 0xf0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x81, 0xf0, 0x0c, 0x73, 0x90, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x10, 0x06, 0x1c, 0x30, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x10, 0x03, 0x00, 0x60, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x08, 0x01, 0xc1, 0xc0, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x08, 0x00, 0x7e, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x18, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x30, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x60, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0xc0, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x60, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x30, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x18, 0x3c, 0x00, 0x1e, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x0c, 0x67, 0x00, 0xf3, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x06, 0xc1, 0x00, 0xc1, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x83, 0x81, 0x00, 0xc0, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0xc0, 0x01, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x60, 0x01, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x20, 0x01, 0x80, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x20, 0x00, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void setup() {
Serial.begin(115200);
//OLED power up
display.begin(i2c_Address, true);
display.display();
delay(2000);
//relay position set
pinMode(relay_1, OUTPUT);
pinMode(relay_2, OUTPUT);
digitalWrite(relay_1, LOW);
digitalWrite(relay_2, LOW);
// Set encoder pins as inputs
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
//initialize pins
pinMode(BTN_1_PIN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
//subscribe to needed events
_ezb.Subscribe(BTN_1, Btn1Pressed, PRESSED);
_ezb.Subscribe(BTN_1, Btn1Released, RELEASED);
_ezb.Subscribe(BTN_1, Btn1Hold, HOLD);
_ezb.Subscribe(BTN_1, Btn1HoldReleased, HOLD_RELEASED);
// pinMode(PWMPin, INPUT_PULLUP);
pinMode (EN_PIN, OUTPUT);
digitalWrite(EN_PIN, LOW);
stepper.setMaxSpeed(motorMaxSpeed);
stepper.setAcceleration(motorAccel);
stepper.moveTo(0);
delay(1000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SH110X_WHITE);
display.setCursor(30, 30);
display.print(PROJEKT);
display.display();
delay(1000);
//init done information
Serial.print("Init done");
delay(10);
display.clearDisplay();
delay(2000);
display.drawBitmap(32, 0, epd_bitmap_OK, 64, 64, SH110X_WHITE);
display.display();
display.setTextSize(8);
}
void loop() {
/* unsigned long currentMillis = millis();*/
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
// There was a change on the CLK pin
lastClk = newClk;
int dtValue = digitalRead(ENCODER_DT);
// Rotating clockwise causes the CLK pin to go low first, and then the DT pin goes low too.
if (newClk == LOW && dtValue == HIGH) {
counterEnc--;
if (counterEnc <= 1 ) {
counterEnc = 1;
}
Serial.print("Rotated clockwise ⏩ Pos:");
Serial.println(counterEnc);
}
// Rotating counterclockwise causes the DT pin to go low first, and then the CLK pin go low.
if (newClk == LOW && dtValue == LOW) {
counterEnc++;
if (counterEnc >= 20 ) {
counterEnc = 20;
}
Serial.print("Rotated counterclockwise ⏪ Pos:");
Serial.println(counterEnc);
}
//pulse_cnt++;
counter2 = counterEnc;
}
/*
// Calculate speed by counting the pulses during a second
if (currentMillis-previousMillis >= 1000) {
// After counting the pulses we have to calculate the speed(revolutions per minute),
// by knowing that KY-040 Rotary Encoder module has 20 steps per revolution.
// We derive the equation: RPM =(counts_second / 20)* 60s = counts_second * 3
int RPM = pulse_cnt * 3;
previousMillis = currentMillis;
pulse_cnt = 0;
Serial.print("RPM = ");
Serial.println(RPM);
}*/
_ezb.Loop();
/*
Serial.println(counter2);
dock = counter;
if(counter <= 9) {
display.clearDisplay();
display.setCursor(40, 3);
display.println(dock);
display.display();
}
else {
display.clearDisplay();
display.setCursor(15, 3);
display.println(dock);
display.display();
}*/
//correct DCC polarity to position
unsigned long currentPolarityMillis = millis();
if(currentPolarityMillis - previousPolarityMillis >= 500 ) { //mala hodnota seka program
previousPolarityMillis = currentPolarityMillis;
checkPolarity();
updateDisplay();
}
}
void updateDisplay() {
/*dock = counterEnc;
if(counterEnc <= 9) {
display.clearDisplay();
display.setCursor(40, 3);
display.println(dock);
display.display();
}
else {
display.clearDisplay();
display.setCursor(15, 3);
display.println(dock);
display.display();
} */
}
// button pressed
void Btn1Pressed() {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("pressed");
}
// button released
void Btn1Released() {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("released");
}
// button hold
bool state = true;
void Btn1Hold() {
state = !state;
digitalWrite(LED_BUILTIN, state);
Serial.println("hold");
}
// button released after hold
void Btn1HoldReleased() {
for (int i = 0; i < 6; i++) {
state = !state;
digitalWrite(LED_BUILTIN, state);
delay(50);
}
digitalWrite(LED_BUILTIN, LOW);
Serial.println("hold released");
}
void checkPolarity() {
if (counter2 >= 5 && counter2 < 15) {
//Serial.println("Normal Polarity");
digitalWrite(relay_1, LOW);
digitalWrite(relay_2, LOW);
}
else {
digitalWrite(relay_1, HIGH);
digitalWrite(relay_2, HIGH);
}
}
The code works perfect until serial monitoring, immidiatelly I want to send the encoder position to the display, it is not stabil.