Hello every body i'm new in the forum, i'm sorry about my english it could be not perfect and there might be numerous mistakes. I'm a begginer in Arduino i would to do a program which use a rotary encoder with push button and i don't know how to start so i ask you guys for your help please. I would like encode my arduino that whenever I press the push button I can switch from one setting to another and that whenever i turn the rotary encoder i can change the value of the setting ongoing. Thank you.
Here is a setup I made for testing a encoder. Originally it adjusted a real time clock but I altered it to serial print the results but the encoder is less responsive using serial.
//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 A 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 is set to input
pinMode (LED,OUTPUT); //LED set to input
}
void loop() {
if (checkButton(encButton)){
while (checkButton(encButton)){ //Wait for button to be released
};
delay(debounceDelay);
digitalWrite(LED, HIGH); // set the LED on
Serial.println(F("Enter Setup"));
eAdjust();
Serial.println(F("Exit Setup"));
digitalWrite(LED, LOW); // set the 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 from main loop when encoder button pressed
void eAdjust(){
char value = readEncoder(); //Init encoder readback value and prime encoder sub (result discarded)
byte eMode = eHours; //Set mode to eHours
doDisplay(eMode); //Display Hours
unsigned long eTime = millis(); //Get current CPU time for 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 for button to be released
};
delay(debounceDelay); //Slight delay to allow for button debounce
//Serial.println("Button Released");
eMode++; //Increment mode
if (eMode > eYear){ //End of modes?
//Serial.println("Mode Exit");
return; //Return
}
doDisplay(eMode); //Display the 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 was turned
//Serial.print("Encoder Turned");
switch (eMode){
case eHours: //Mode eHours
hours = hours + result; //Read hours and 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 to do 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 for 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 to read
byte checkButton(byte Button) {
if (digitalRead(Button) == HIGH) { // If button pressed then wait a bit to allow for debounce
delay(debounceDelay);
return digitalRead(Button); // Read button again and return it
}
else { //Button not pressed so no need to wait for 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;
}
For processing the Gray switch
http://www.circuitsathome.com/mcu/reading-rotary-encoder-on-arduino
Post a vendor link or datasheet for the encoder.
Thank you guys for your reponses.
What do you mean by vendor link ??
Can you explain please for what does it help thits part of the program Riva
if (checkButton(encButton)){
while (checkButton(encButton)){ //Wait for button to be released
};
delay(debounceDelay);
digitalWrite(LED, HIGH); // set the LED on
Serial.println(F("Enter Setup"));
eAdjust();
Serial.println(F("Exit Setup"));
digitalWrite(LED, LOW); // set the 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);
}
}