I am trying to use a rotary encoder to display a password on my LCD. I have gotten the lcd to read the encoder when it is turning to raise or lower the number. I also use a long press on the encoder to delete the inputted password. The trouble that I am running into is using a single click. I want it when a click occurs, the previously inputted number is saved and then the next character on the Arduino is activated to input a second, third, fourth number. I'm also having trouble actually making it so the password means anything to the Arduino. I'll post the code below.
Thanks.
#include <Arduino.h>
#include <Wire.h>
#include <OneButton.h>
#include <LiquidCrystal_I2C.h>
// Define the IO Pins Used
#define CLK 2 // Used for generating interrupts using CLK signal
#define DT 3 // Used for reading DT signal
#define SW 4 // Used for the Rotary push button switch
#define V 5 // Set to HIGH to be the 5V pin for the Rotary Encoder
#define GND 6 // Set to LOW to be the GND pin for the Rotary Encoder
// OneButton class handles Debounce and detects button press
OneButton btnRot(SW, HIGH); // Rotary Select button
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Used for the Rotary Encoder interrupt routines PinA() and PinB()
volatile int rotaryCount = 0;
// Disables the Rotary Encoder interrupts while the LCD is being updated
byte rotaryDisabled;
volatile byte aFlag = 0; // lets us know when we're expecting a rising edge on pinA
// to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // lets us know when we're expecting a rising edge on pinB
// to signal that the encoder has arrived at a detent
// (opposite direction to when aFlag is set)
volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt
// pins before checking to see if we have moved a whole detent
int btnState;
unsigned long lastButtonPress = 0;
//Array Setup
byte count = 0;
int correctcode[] = {2,3,4,5};
int code[4]
// PinA() - Called by the Interrupt pin when the Rotary Encoder Turned
void PinA() {
if (rotaryDisabled) return;
cli(); //stop interrupts happening before we read pin values
// read all eight pin values then strip away all but pinA and pinB's values
reading = PIND & 0xC;
//check that both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
if (reading == B00001100 && aFlag) {
rotaryRight();
bFlag = 0; //reset flags for the next turn
aFlag = 0; //reset flags for the next turn
}
//signal that we're expecting pinB to signal the transition to detent from free rotation
else if (reading == B00000100) bFlag = 1;
sei(); //restart interrupts
}
// PinB() - Called by the Interrupt pin when the Rotary Encoder Turned
void PinB() {
if (rotaryDisabled) return;
cli(); //stop interrupts happening before we read pin values
//read all eight pin values then strip away all but pinA and pinB's values
reading = PIND & 0xC;
//check that both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
if (reading == B00001100 && bFlag) {
rotaryLeft();
bFlag = 0; //reset flags for the next turn
aFlag = 0; //reset flags for the next turn
}
//signal that we're expecting pinA to signal the transition to detent from free rotation
else if (reading == B00001000) aFlag = 1;
sei(); //restart interrupts
}
// rotaryRight() - Rotary Encoder is turned 1 detent to the Right (clockwise)
void rotaryRight()
{
rotaryCount++;
}
// rotaryLeft() - Rotary Encoder is turned 1 detent to the Left (counter-clockwise)
void rotaryLeft()
{
rotaryCount--;
}
// rotaryClick() - Rotary Encoder Select Switch is pressed
void rotaryClick()
{
}
// rotaryLongPress() - Rotary Encoder Select Switch is Held Down (Long Press)
void rotaryLongPress()
{
rotaryCount = 0;
}
// initializeRotaryEncoder() - Initialize the pins and interrupt functions for the Rotary Encdoer
void initializeRotaryEncoder()
{
// Set the Directions of the I/O Pins
pinMode(CLK, INPUT_PULLUP);
pinMode(DT, INPUT_PULLUP);
pinMode(SW, INPUT_PULLUP);
pinMode(GND, OUTPUT);
pinMode(V, OUTPUT);
// Set the 5V and GND pins for the Rotary Encoder
digitalWrite(GND, LOW);
digitalWrite(V, HIGH);
digitalWrite(SW, HIGH);
// set an interrupt on PinA and PinB, looking for a rising edge signal and
// executing the "PinA" and "PinB" Interrupt Service Routines
attachInterrupt(0, PinA, RISING);
attachInterrupt(1, PinB, RISING);
// Define the functions for Rotary Encoder Click and Long Press
btnRot.attachClick(&rotaryClick);
btnRot.attachLongPressStart(&rotaryLongPress);
btnRot.setPressTicks(2000);
rotaryDisabled = 0;
Serial.begin(9600);
}
// initializeLcd() - Initialize the LCD
void initializeLcd()
{
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
}
// updateLcd() - Update the LCD with current Rotary Encoder detent count
void updateLcd()
{
rotaryDisabled = 1;
lcd.setCursor(0, 0);
lcd.print(F("Password = "));
lcd.print(rotaryCount);
lcd.print(F(" "));
Serial.print(rotaryCount);
rotaryDisabled = 0;
}
// setup() - Initialization Function
void setup()
{
initializeRotaryEncoder();
initializeLcd();
}
// loop() - Main Program Loop Function
void loop()
{
updateLcd();
btnRot.tick();
delay(50);
}