#include <EncoderButton.h>
#include<LiquidCrystal_I2C.h>
#include<Wire.h>
/**
Instatiate an EncoderButton.
For Arduino Uno, the hardware interrupts are pins 2 & 3
For Teensy, you can use any digital pin.
*/
EncoderButton firstEncoderButton(3, 2, 4);
EncoderButton button1(4);
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int motorPin = 5;
int pwmValue = 0;
int increment ;
const int led = 13;
int pwm = 0;
int click = 0;
int pwmPer = 0;
int count = 0;
unsigned long currentTime, previousTime;
float estimatedAngularVelocity = 0;
float K = 0.1; // experimentally determined constant
float B = 0.1; // experimentally determined constant
int RPM = 3600;
float speed = 0;
float lastSpeed = 0;
/**
A function to handle the first encoder 'encoder' event
*/
void onFirstEncoder(EncoderButton& eb) {
Serial.print("first encoder incremented by: ");
Serial.println(eb.increment());
Serial.print("first encoder position is: ");
Serial.println(eb.position());
increment = eb.position();
Serial.println(increment);
if (pwm == 1) {
pwmValue = map((increment * 2), 0, 100, 0, 255);
pwmValue = constrain(pwmValue, 0, 255);
analogWrite(motorPin, pwmValue);
Serial.println(pwmValue);
} else {
analogWrite(motorPin, 0);
}
}
/**
A function to handle the first encoder 'clicked' event
*/
void onFirstButtonClicked(EncoderButton& eb) {
Serial.print("First button clickCount: ");
Serial.println(eb.clickCount());
click = eb.clickCount();
if (click == 2) {
digitalWrite(led, HIGH);
pwm = 1;
} else {
digitalWrite(led, LOW);
pwm = 0;
}
}
void onButton1LongPress(EncoderButton& eb) {
Serial.print("button1 longPressCount: ");
Serial.println(eb.longPressCount());
count = 0;
}
void setup() {
// put your setup code here, to run once:
currentTime = millis();
Serial.begin(9600);
delay(500);
Serial.println("EncoderButton multiple encoder/button example");
//Link the event(s) to your function
firstEncoderButton.setEncoderHandler(onFirstEncoder);
firstEncoderButton.setClickHandler(onFirstButtonClicked);
button1.setLongPressHandler(onButton1LongPress, true); //Second arg means repeat the long click
// set up the LCD
lcd.init();
lcd.backlight();
// display initial message on the LCD
lcd.clear();
lcd.print("Encoder with PWM");
lcd.setCursor(0, 1);
lcd.print("Press HOLD CLR CNT");
lcd.setCursor(0, 2);
lcd.print("2click to ON");
lcd.setCursor(0, 3);
lcd.print("1click to OFF");
delay(4000);
lcd.clear();
}
void loop() {
// put your main code here, to run repeatedly:
// You must call update() for every defined EncoderButton.
firstEncoderButton.update();
button1.update();
//this line of code is to hold rotary encoder value into the spacific range
if (pwmValue <= 0) {
firstEncoderButton.resetPosition(0);
} else if (pwmValue >= 255) {
firstEncoderButton.resetPosition(50);
}
lcd.setCursor(0, 0);
lcd.print("Switch: ");
lcd.print(pwm ? "ON " : "OFF");
lcd.setCursor(0, 1);
lcd.print("PWM:");
pwmPer = pwmValue * 0.393;
lcd.println(pwmPer);lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("CNT:"); lcd.println(count);lcd.print(" ");
if (pwm == 1) {
speed = 0.01 * pwmPer;
if (millis() - currentTime >= 1000) { // check if one second has passed
Serial.print("timeP: ");
Serial.println(millis() - currentTime);
speed = 0.01 * pwmPer;
speed = (millis() - currentTime)*speed;
speed = speed/1000;
lastSpeed = speed + lastSpeed;
Serial.print("Lspeed: ");
Serial.println(lastSpeed);
if(lastSpeed >= 0.99 ){
count++;
lastSpeed = 0;
}
Serial.print("speed: ");
Serial.println(speed);
currentTime = millis();
previousTime = millis();
}
}
}