#define encoder0PinA 2
#define encoder0PinB 3
#define encoder0Btn 4
#define RC PI / 180
int encoder0Pos = 0;
int rotcheck = 0;
int degree = 1;
double radian =0;
double sine =0;
double cosine = 0;
double tangent = 0;
#include <LiquidCrystal.h>;
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
Serial.begin(9600);
pinMode(encoder0PinA, INPUT_PULLUP);
pinMode(encoder0PinB, INPUT_PULLUP);
pinMode(encoder0Btn, INPUT_PULLUP);
attachInterrupt(0, doEncoder, CHANGE);
lcd.begin(16, 2);
}
int valRotary,lastValRotary;
void loop() {
//start encoder check function
int btn = digitalRead(encoder0Btn);
//define degreese radians sin cos and tan
degree = (valRotary * 22.5);
radian = (degree * RC);
sine = (sin(radian));
cosine = (cos(radian));
tangent = (sine / cosine);
//print degreese
lcd.setCursor(0, 0);
lcd.print("Deg:");
lcd.setCursor(4, 0);
lcd.print(degree);
//print Radians
lcd.setCursor(9, 0);
lcd.print("Rad:");
lcd.setCursor(13, 0);
lcd.print(radian,2);
//print sine
lcd.setCursor(0, 1);
lcd.print("S:");
lcd.setCursor(2, 1);
lcd.print(sine,3);
//print cos
lcd.setCursor(8, 1);
lcd.print("C:");
lcd.setCursor(10, 1);
lcd.print(cosine,3);
//print tan
if (rotcheck != valRotary) {
rotcheck = valRotary;
Serial.print(rotcheck);
lcd.clear();
}
}
//read encoder
void doEncoder()
{
if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB))
{
encoder0Pos++;
}
else
{
encoder0Pos--;
}
valRotary = abs(encoder0Pos/2.5);
}