#define ENCODER_CLK 32
#define ENCODER_DT 33
#define SW 27
uint8_t select_digit = 0;
uint8_t mode = 0;
// uint8_t digit0 = 0;
// uint8_t digit1 = 0;
// uint8_t digit2 = 0;
// uint16_t digit3 = 0;
double digits = 00.00;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(ENCODER_CLK, INPUT_PULLUP);
pinMode(ENCODER_DT, INPUT_PULLUP);
pinMode(SW, INPUT_PULLUP);
}
//////////////////////////////////////////////// Count Up ////////////////////////////////////////////////////
void countup() {
switch (select_digit) {
case 0:
digits = digits + 0.01;
break;
case 1:
digits = digits + 0.1;
break;
case 2:
digits = digits + 1.0;
break;
case 3:
digits = digits + 10.0;
break;
}
if(digits > 50.0) digits = 50.0;
}
/////////////////////////////////////////////// Count Down ///////////////////////////////////////////////////
void countdown() {
switch (select_digit) {
case 0:
digits = digits - 0.01;
if(digits )
break;
case 1:
digits = digits - 0.1;
if(digits )
break;
case 2:
digits = digits - 1.0;
if(digits )
break;
case 3:
digits = digits - 10.0;
if(digits )
break;
}
if(digits < 0.0) digits = 0.0;
}
/////////////////////////////////////////////// Press Encoder ////////////////////////////////////////////////
void press_Encoder() {
switch (mode) {
case 0:
select_digit++;
if(select_digit > 4) select_digit = 0;
break;
}
}
///////////////////////////////////////////// ROTARY ENCODER /////////////////////////////////////////////////
// SW built in
bool swIsPress = false;
uint32_t swTime;
// Rotary Encoder
bool rotaryIsRotage = false;
uint32_t rotageTime;
uint32_t checkTime;
void rotaryEncode() {
checkTime = millis(); // = millis();
rotageTime = checkTime;
swTime = checkTime;
while((millis() - checkTime) < 50) {
if(digitalRead(SW) == 0) {
if((millis() - swTime) >= 1) { // debouncing
if(swIsPress == false) {
swIsPress = true;
press_Encoder();
}
}
} else {
swTime = millis();
swIsPress = false;
}
if(digitalRead(ENCODER_DT) == 0) {
if((millis() - rotageTime) > 1) { // debouncing
if(rotaryIsRotage == false) {
rotaryIsRotage = true;
if(digitalRead(ENCODER_CLK) == 0) {
countup();
}
else {
countdown();
}
}
}
}
else {
rotageTime = millis();
rotaryIsRotage = false;
}
}
}
void loop() {
// put your main code here, to run repeatedly:
rotaryEncode();
Serial.println(digits);
delay(10); // this speeds up the simulation
}