#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int encoderPinA = 2;
const int encoderPinB = 3;
const int buttonPin = 4;
volatile int encoderPos = 0;
volatile int lastEncoderPos = 0;
volatile bool buttonPressed = false;
int function1Value = 0;
int function2Value = 0;
int function3Value = 0;
void setup() {
Serial.begin(9600);
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
pinMode(buttonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(encoderPinA), handleEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(buttonPin), handleButton, FALLING);
lcd.begin(16, 2);
lcd.backlight();
}
void loop() {
// Your main code goes here
int selectedFunction = encoderPos % 3; // 3 functions
lcd.clear();
lcd.setCursor(0, 0);
switch(selectedFunction) {
case 0:
lcd.print("Function 1: ");
lcd.print(function1Value);
break;
case 1:
lcd.print("Function 2: ");
lcd.print(function2Value);
break;
case 2:
lcd.print("Function 3: ");
lcd.print(function3Value);
break;
}
delay(500); // Delay for better readability
}
void handleEncoder() {
int MSB = digitalRead(encoderPinA);
int LSB = digitalRead(encoderPinB);
int encoded = (MSB << 1) | LSB;
int sum = (lastEncoderPos << 2) | encoded;
if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) {
encoderPos++;
} else if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) {
encoderPos--;
}
updateFunctionValue();
lastEncoderPos = encoded;
}
void updateFunctionValue() {
int selectedFunction = encoderPos % 3; // 3 functions
switch(selectedFunction) {
case 0:
function1Value += (encoderPos > lastEncoderPos) ? -1 : 1;
break;
case 1:
function2Value += (encoderPos > lastEncoderPos) ? -1 : 1;
break;
case 2:
function3Value += (encoderPos > lastEncoderPos) ? -1 : 1;
break;
}
}
void handleButton() {
buttonPressed = true;
}
/*
// ((Incrementalny potmeter))
const int SW = 2; // SW - in increment. rotary
const int PinA = 3; // Generating interrupts using CLK signal
const int PinB = 4; // Reading DT signal
// Updated by the ISR (Interrupt Service Routine)
int virtualPosition = 100; // nastavenie východzej teploty (C/0.25) ............................!!!
// --------------------------------------------------------------------------------------------------------
// volatile float potmeter; // pozadovana teplota - incremental (rotary)
int tlac; // SW
const int Speak = 5; // PWM pre reproduktor
const int Relay = 6; // Relay
// --------------------------------------------------------------------------------------------------------
int stav_tlacitka = 0; // východzia hodnota nastavovania menu ...
// --------------------------------------------------------------------------------------------------------
void tlacitko () {
Serial.print("spustil si podprogram cicík");
}
// --------------------------------------------------------------------------------------------------------
void isr () { // Spustí sa pri prerušení z incrementalu
static unsigned long lastInterruptTime = 0;
unsigned long interruptTime = millis();
// If interrupts come faster than 5ms, assume it's a bounce and ignore
if (interruptTime - lastInterruptTime > 10) {
if (digitalRead(PinB) == LOW)
{
virtualPosition --;
if (virtualPosition <= 40){
virtualPosition = 40;
}
}
else {
virtualPosition ++;
if (virtualPosition >= 399){
virtualPosition = 399;
}
}
// Keep track of when we were here last (no more than every 5ms)
lastInterruptTime = interruptTime;
}
}
// --------------------------------------------------------------------------------------------------------
void setup() {
Serial.begin(9600); // Serial monitor
pinMode(SW, INPUT_PULLUP); // preverit ten pull up ........................!!!!!!!!!
pinMode(PinA, INPUT); // Rotary pulses are INPUTs
pinMode(PinB, INPUT); // Rotary pulses are INPUTs
pinMode(Speak,OUTPUT); // PWM Speaker
pinMode(Relay, OUTPUT); // Relay
digitalWrite (Speak,HIGH);
attachInterrupt(digitalPinToInterrupt(PinA), isr, LOW); // Attach the routine to service the interrupts
}
void loop() {
tlac = digitalRead (SW);
if (tlac < 1) {
tlacitko;
//Serial.print("si kokot ");
}
else {
}
// Serial monitor -----------------------------------------------------------------------------------------
Serial.print("Incr_poz= ");
Serial.print(virtualPosition);
Serial.print(" ");
Serial.print("SW = ");
Serial.print(tlac);
// --------------------------------------------------------------------------------------------------------
//Serial.print("Nast_tep.= ");
//Serial.print(potmeter);
Serial.println(" ");
// --------------------------------------------------------------------------------------------------------
delay (10);
}
*/
/*
#include <Adafruit_MAX31865.h>
// Use software SPI: CS, DI, DO, CLK
Adafruit_MAX31865 thermo = Adafruit_MAX31865(9, 10, 11, 12);
// use hardware SPI, just pass in the CS pin
//Adafruit_MAX31865 thermo = Adafruit_MAX31865(10);
// The value of the Rref resistor. Use 430.0 for PT100 and 4300.0 for PT1000
#define RREF 430.0
// The 'nominal' 0-degrees-C resistance of the sensor
// 100.0 for PT100, 1000.0 for PT1000
#define RNOMINAL 100.0
void setup() {
Serial.begin(115200);
Serial.println("Adafruit MAX31865 PT100 Sensor Test!");
thermo.begin(MAX31865_3WIRE); // set to 2WIRE or 4WIRE as necessary
}
void loop() {
uint16_t rtd = thermo.readRTD();
Serial.print("RTD value: "); Serial.println(rtd);
float ratio = rtd;
ratio /= 32768;
Serial.print("Ratio = "); Serial.println(ratio,8);
Serial.print("Resistance = "); Serial.println(RREF*ratio,8);
Serial.print("Temperature = "); Serial.println(thermo.temperature(RNOMINAL, RREF));
// Check and print any faults
uint8_t fault = thermo.readFault();
if (fault) {
Serial.print("Fault 0x"); Serial.println(fault, HEX);
if (fault & MAX31865_FAULT_HIGHTHRESH) {
Serial.println("RTD High Threshold");
}
if (fault & MAX31865_FAULT_LOWTHRESH) {
Serial.println("RTD Low Threshold");
}
if (fault & MAX31865_FAULT_REFINLOW) {
Serial.println("REFIN- > 0.85 x Bias");
}
if (fault & MAX31865_FAULT_REFINHIGH) {
Serial.println("REFIN- < 0.85 x Bias - FORCE- open");
}
if (fault & MAX31865_FAULT_RTDINLOW) {
Serial.println("RTDIN- < 0.85 x Bias - FORCE- open");
}
if (fault & MAX31865_FAULT_OVUV) {
Serial.println("Under/Over voltage");
}
thermo.clearFault();
}
Serial.println();
delay(1000);
}
*/