#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <Arduino.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
const int encoderCLK = 27;
const int encoderDT = 14;
const int encoderSW = 12;
const int a1 = 34;
const int a2 = 35;
const int a3 = 32;
const int a4 = 33;
const int a5 = 25;
const int a6 = 26;
const int D1 = 19;
const int D2 = 18;
const int D3 = 16;
const int D4 = 17;
int mode=0;
volatile int lastCLKState = LOW;
volatile int encoderPos = 2048,encoderPos1 = 2048;
volatile bool clockwise = true;
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600);
pinMode(a1, INPUT);
pinMode(a2, INPUT);
pinMode(a3, INPUT);
pinMode(a4, INPUT);
pinMode(a5, INPUT);
pinMode(a6, INPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, INPUT);
pinMode(D4, INPUT);
pinMode(encoderCLK, INPUT_PULLUP);
pinMode(encoderDT, INPUT_PULLUP);
pinMode(encoderSW, INPUT_PULLUP);
lcd.setCursor(0,0);
lcd.print("V1: ");
lcd.setCursor(0,1);
lcd.print("C1: ");
lcd.setCursor(0,2);
lcd.print("V2: ");
lcd.setCursor(0,3);
lcd.print("C2: ");
lcd.setCursor(10,1);
lcd.print("SC1:");
lcd.setCursor(10,3);
lcd.print("SC2:");
analogReadResolution(12);
attachInterrupt(digitalPinToInterrupt(encoderCLK), updateEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderSW), DUTY, FALLING);
xTaskCreatePinnedToCore(taskCore0, "Task_Core0", 10000, NULL, 1, NULL, 0);
xTaskCreatePinnedToCore(taskCore1, "Task_Core1", 10000, NULL, 1, NULL, 1);
}
void CV(){
int c1 = analogRead(a1);
int c2 = analogRead(a2);
int c3 = analogRead(a3);
int c4 = analogRead(a4);
int c5 = analogRead(a5);
int c6 = analogRead(a6);
if(c3 < encoderPos){
if(c1 <= c2){
digitalWrite(D1, HIGH);
}
else if(c1 > c2){digitalWrite(D1, LOW);}
}
else
{digitalWrite(D1, LOW);}
if(c6 < encoderPos){
if(c4 <= c5){
digitalWrite(D2, HIGH);
}
else if(c4 > c5){digitalWrite(D2, LOW);}
}
else{digitalWrite(D2, LOW);}
}
void CC(){
int c3 = analogRead(a3);
int c6 = analogRead(a6);
if(c3 <= encoderPos){
digitalWrite(D1, HIGH);
}
else if(c3 > encoderPos){digitalWrite(D1, LOW);}
if(c6 <= encoderPos1){
digitalWrite(D2, HIGH);
}
else if(c6 > encoderPos1){digitalWrite(D2, LOW);}
}
void taskCore0(void *parameter) {
while (true) {
int b1 = analogRead(a1);
int b2 = analogRead(a3);
int b3 = analogRead(a4);
int b4 = analogRead(a6);
float V1 =b1*(3.3 / 4095.0);
float V2 =b2*(3.3 / 4095.0);
float V3 =b3*(3.3 / 4095.0);
float V4 =b4*(3.3 / 4095.0);
float V5 =encoderPos *(3.3 / 4095.0);
float V6 =encoderPos1*(3.3 / 4095.0);
lcd.setCursor(4,0);
lcd.print(V1,3);
lcd.setCursor(4,1);
lcd.print(V2,3);
lcd.setCursor(4,2);
lcd.print(V3,3);
lcd.setCursor(4,3);
lcd.print(V4,3);
lcd.setCursor(15,1);
lcd.print(V5,3);
lcd.setCursor(15,3);
lcd.print(V6,3);
if(digitalRead(D4) == HIGH){
lcd.setCursor(13,2);
lcd.print("CV");
}
else{
lcd.setCursor(13,2);
lcd.print("CC");
}
if (digitalRead(D3) == HIGH){
lcd.setCursor(12,0);
lcd.print("SINGLE");
}
else{
lcd.setCursor(12,0);
lcd.print("DUAL ");
}
}}
void taskCore1(void *parameter) {
while (true) {
delayMicroseconds(1);
if (digitalRead(D3) == HIGH){
function();
}
else{
function();
}}
}
void updateEncoder() {
int currentCLKState = digitalRead(encoderCLK);
int currentDTState = digitalRead(encoderDT);
if (lastCLKState != currentCLKState) {
if (currentCLKState == HIGH && currentDTState == LOW) {
clockwise = true;
} else if (currentCLKState == HIGH && currentDTState == HIGH) {
clockwise = false;
}
if( mode == 1){
if (clockwise) {
encoderPos += 10;
} else {
encoderPos -= 10;
}
}
else{
if (clockwise) {
encoderPos1 += 10;
} else {
encoderPos1 -= 10;
}
}
if(encoderPos > 4095){
encoderPos = 4095;}
else if(encoderPos < 0)
{
encoderPos =0 ;}
if(encoderPos1 > 4095){
encoderPos1 = 4095;}
else if(encoderPos1 < 0)
{
encoderPos1 =0 ;}
lastCLKState = currentCLKState;
}
}
void DUTY(){
static unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
unsigned long currentTime = millis();
if (currentTime - lastDebounceTime > debounceDelay) {
if(mode == 1){mode = 0 ;}
else{mode =1;}
lastDebounceTime = currentTime;
}
}
void function(){
if(digitalRead(D4) == HIGH){
CV();
}
else{
CC();
}
}
void loop() {
}