/*
Simple "Hello World" for ILI9341 LCD
https://wokwi.com/arduino/projects/308024602434470466
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
const int encoder_a = 2; // Pin
const int encoder_b = 3; // Pin
const int encoder_Z = 4; // Pin
long encoder_pulse_counter = 0;
long direction = 1;
void encoderPinChangeA()
{
encoder_pulse_counter += 1;
direction = digitalRead(encoder_a) == digitalRead(encoder_b) ? -1 : 1;
}
void encoderPinChangeB()
{
encoder_pulse_counter += 1;
direction = digitalRead(encoder_a) != digitalRead(encoder_b) ? -1 : 1;
}
void encoderPinChangeZ()
{
encoder_pulse_counter += 1;
// direction = digitalRead(encoder_a) == digitalRead(encoder_b) ? -1 : 1;
}
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void setup() {
tft.begin();
// tft.setCursor(26, 120);
// tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
// tft.println("Hello, TFT!");
// tft.setCursor(20, 160);
// tft.setTextColor(ILI9341_GREEN);
//tft.setTextSize(2);
// tft.println("I can has colors?");
Serial.begin(9600);
pinMode(encoder_a, INPUT_PULLUP);
pinMode(encoder_b, INPUT_PULLUP);
pinMode(encoder_Z, INPUT_PULLUP);
attachInterrupt(0, encoderPinChangeA, CHANGE);
attachInterrupt(1, encoderPinChangeB, CHANGE);
//counting Z pin interruptions
attachInterrupt(digitalPinToInterrupt(encoder_Z), encoderPinChangeA, HIGH);
attachInterrupt(digitalPinToInterrupt(encoder_a), speedCalculate, HIGH);
// Meme reference: https://english.stackexchange.com/questions/20356/origin-of-i-can-haz
}
void loop()
{
}
void speedCalculate()
{
long speed = encoder_pulse_counter/1024.00*60;
// For encoder plate with 1024 Pulses per Revolution
//Serial.print("RPM: ");
tft.println("RPM:");
if(direction == 1)
{
//Serial.println("Rotated clockwise ⏩");
tft.println("clockwise");
}
else
{
tft.println("counterclockwise");
// Serial.println("Rotated counterclockwise ⏪");
}
//Printing speed with direction
//Serial.println(direction*speed);
encoder_pulse_counter = 0; // Clear variable just before counting again
delay(1000);
}