Help please about a rotary encoder
hello every body i'm new in forum, i'm sorry english not perfect , there might numerous mistakes. i'm begginer in arduino program use rotary encoder push button , don't know how start ask guys please. encode arduino whenever press push button can switch 1 setting , whenever turn rotary encoder can change value of setting ongoing. thank you.
here setup made testing encoder. adjusted real time clock altered serial print results encoder less responsive using serial.
code: [select]
//enumerations of settings
const byte ehours = 0;
const byte eminutes = 1;
const byte eday = 2;
const byte emonth = 3;
const byte eyear = 4;
//constants
const byte encbutton = 4; //rotary encoder button pin
const byte encoder0pina = 7; //rotary encoder pin
const byte encoder0pinb = 8; //rotary encoder b pin
const byte led = 13;
const unsigned long debouncedelay = 50; //button debounce time
const unsigned long adjustdelay = 10000; //10 second adjust timeout
//globals
byte hours = 0;
byte minutes = 0;
byte day = 0;
byte month = 0;
byte year = 0;
void setup() {
serial.begin(115200);
pinmode (encoder0pina,input_pullup); //enable pullup resistor
pinmode (encoder0pinb,input_pullup);
pinmode (encbutton,input); //encoder button set input
pinmode (led,output); //led set input
}
void loop() {
if (checkbutton(encbutton)){
while (checkbutton(encbutton)){ //wait button released
};
delay(debouncedelay);
digitalwrite(led, high); // set led on
serial.println(f("enter setup"));
eadjust();
serial.println(f("exit setup"));
digitalwrite(led, low); // set led off
if (hours < 10)
serial.print("0");
serial.print(hours);
serial.print(":");
if (minutes < 10)
serial.print("0");
serial.print(minutes);
serial.print(" - ");
if (day < 10)
serial.print("0");
serial.print(day);
serial.print("/");
if (month < 10)
serial.print("0");
serial.print(month);
serial.print("/20");
if (year < 10)
serial.print("0");
serial.print(year);
}
}
// called main loop when encoder button pressed
void eadjust(){
char value = readencoder(); //init encoder readback value , prime encoder sub (result discarded)
byte emode = ehours; //set mode ehours
dodisplay(emode); //display hours
unsigned long etime = millis(); //get current cpu time adjust loop timeout
while ((millis() - etime) < adjustdelay) {
if (checkbutton(encbutton)){ //is encoder button pressed?
// mode button pressed
//serial.println("button pressed");
while (checkbutton(encbutton)){ //wait button released
};
delay(debouncedelay); //slight delay allow button debounce
//serial.println("button released");
emode++; //increment mode
if (emode > eyear){ //end of modes?
//serial.println("mode exit");
return; //return
}
dodisplay(emode); //display new mode
etime = millis(); //reset adjust loop timout
}
if (millis() % 5 == 0){ //only read every 10ms
char result = readencoder(); //read encoder. returns -1,0,1
//serial.print("encoder: ");
if (!result == 0){ //encoder <> 0
//encoder wheel turned
//serial.print("encoder turned");
switch (emode){
case ehours: //mode ehours
hours = hours + result; //read hours , add encoder direction
hours = (hours % 24); //legalizing
break; //no further processing
case eminutes:
minutes = minutes + result;
minutes = (minutes % 60);
break;
case eday:
day = day + result;
day = (day % 31);
break;
case emonth:
month = month + result;
month = (month % 12);
break;
case eyear:
year = year + result;
year = (year % 99);
break;
default: //what if mode unknown
break; //nothing
}
dodisplay(emode); //display new mode
etime = millis(); //reset adjust loop timout
}
}
}
//timed out
serial.println("timeout");
}
void dodisplay(byte mode){ //display relevent data given mode
serial.print("mode ");
serial.print(mode);
serial.print(": ");
switch (mode){
case ehours:
serial.println(hours);
break;
case eminutes:
serial.println(minutes);
break;
case eday:
serial.println(day);
break;
case emonth:
serial.println(month);
break;
case eyear:
serial.println(year);
break;
default:
break;
}
}
// button = pin number read
byte checkbutton(byte button) {
if (digitalread(button) == high) { // if button pressed wait bit allow debounce
delay(debouncedelay);
return digitalread(button); // read button again , return it
}
else { //button not pressed no need wait bebounce
return low;
}
}
char readencoder() {
static byte encoder0pinalast = low;
char edir = 0;
byte n = digitalread(encoder0pina);
if ((encoder0pinalast == low) && (n == high)) {
if (digitalread(encoder0pinb) == low) {
edir = -1;
//serial.print("-1");
}
else {
edir = 1;
//serial.print("1");
}
}
encoder0pinalast = n;
//serial.println(edir);
return edir;
}
Arduino Forum > Using Arduino > Programming Questions > Help please about a rotary encoder
arduino
Comments
Post a Comment