#include <LiquidCrystal.h>
#include <Button.h>
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
// Define pins
int EventCounter = 1;
Button LongRotaReset(16); // Connect rst button between pin 16 and GND
Button EventPos(17); // Connect ++ button between pin 17 and GND
Button EventNeg(18); // Connect -- button between pin 18 and GND
// Encoder Setup
// Taiss E38S6-600-24G
// Modified from https://electricdiylab.com/how-to-connect-optical-rotary-encoder-with-arduino/
volatile long LongTemp, LongCounter=0;
volatile long RotaTemp, RotaCounter=0;
int LongOutputA = 2; // Longitudinal Encoder Output A (white wire)
int LongOutputB = 3; // Longitudinal Encoder Output B (green wire)
int RotaOutputA = 14; // Rotational Encoder Output A (white wire)
int RotaOutputB = 15; // Rotational Encoder Output B (green wire)
float LongConversion = 5.55; // How many steps for each encoder step
float LongPos = 5.5; // initialize longitudinal position
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("C206 Data Logger");
lcd.setCursor(6,1);
lcd.print("V1.1");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ARE FLIGHT CTRLS");
lcd.setCursor(0,1);
lcd.print("FREE? YES");
delay(1000);
lcd.clear();
pinMode (LongOutputA, INPUT_PULLUP); // internal pullup input pin for LongOutputA
pinMode (LongOutputB, INPUT_PULLUP); // internal pullup input pin for LongOutputB
pinMode (RotaOutputA, INPUT_PULLUP); // internal pullup input pin for RotaOutputA
pinMode (RotaOutputB, INPUT_PULLUP); // internal pullup input pin for RotaOutputB
//Setting up interrupt for Longitudinal Encoder
//Setting up interrupt for Longitudinal Encoder
//A rising pulse from the encoder output, triggers the function (LongA, LongB, RotaA, or RotaB);
attachInterrupt(digitalPinToInterrupt(LongOutputA), LongA, RISING);
attachInterrupt(digitalPinToInterrupt(LongOutputB), LongB, RISING);
attachInterrupt(digitalPinToInterrupt(RotaOutputA), RotaA, RISING);
attachInterrupt(digitalPinToInterrupt(RotaOutputB), RotaB, RISING);
LongRotaReset.begin();
EventPos.begin();
EventNeg.begin();
lcd.print("EVENT: ");
lcd.setCursor(6,0);
lcd.print(EventCounter);
lcd.setCursor(11,0);
lcd.print("13:00");
lcd.setCursor(0,1);
lcd.print("D:");
lcd.setCursor(1,1);
lcd.print(0.,1);
lcd.setCursor(6,1);
lcd.print("in");
lcd.setCursor(11,1);
lcd.print("R");
lcd.setCursor(12,1);
lcd.print("180");
lcd.print((char) 223);
}
void loop() {
if (EventPos.pressed()){
EventCounter ++;
lcd.setCursor(6,0);
lcd.print(EventCounter);
}
if (EventNeg.pressed()){
EventCounter --;
lcd.setCursor(6,0);
lcd.print(EventCounter);
}
if (LongRotaReset.pressed()){
LongCounter = 0;
//RotaCounter = 0;
}
if( LongCounter != LongTemp ){
Serial.print (millis());
Serial.print ("/t");
Serial.println (LongCounter);
Serial.println (LongPos);
LongTemp = LongCounter;
}
LongPos = LongCounter*LongConversion;
lcd.setCursor(1,1);
//lcd.print(LongPos);
lcd.sprintf();
}
void LongA() {
// LongA is activated if LongOutputA pin is going from LOW to HIGH
// Check pin LongOutputB to determine the direction
if(digitalRead(LongOutputB)==LOW) {
LongCounter++;
}else{
LongCounter--;
}
}
void LongB() {
// LongB is activated if LongOutB pin is going from LOW to HIGH
// Check with LongOutputA to determine the direction
if(digitalRead(LongOutputA)==LOW) {
LongCounter--;
}else{
LongCounter++;
}
}
void RotaA() {
// RotaA is activated if RotaOutputA pin is going from LOW to HIGH
// Check pin RotaOutputB to determine the direction
if(digitalRead(RotaOutputB)==LOW) {
RotaCounter++;
}else{
RotaCounter--;
}
}
void RotaB() {
// RotaB is activated if RotaOutB pin is going from LOW to HIGH
// Check with RotaOutputA to determine the direction
if(digitalRead(LongOutputA)==LOW) {
RotaCounter--;
}else{
RotaCounter++;
}
}