#include <Stepper.h>
#include <LiquidCrystal_I2C.h>
#define ENCODER_CLK 2
#define ENCODER_DT 3
#define ENCODER_SW 4
#define BUTTON_PIN 5
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int stepsPerRevolution = 200;
Stepper stepperMotor(stepsPerRevolution, 8, 9, 10, 11);
int lastClk = HIGH;
int counter = 0;
int steps = 0;
int RPM = 0;
int maxRPM = 4800; // ??????????????
/*
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int newPosition;
int lastPosition = analogRead(potpin);
int throttleChange = false;
void readRPM(){
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 50);
stepperMotor.step(val);
}
*/
void readEncoder() {
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH) {
counter++; // Clockwise
stepperMotor.step(1);
}
if (dtValue == LOW) {
counter--; // Counterclockwise
stepperMotor.step(-1);
}
}
// Get the counter value, disabling interrupts.
// This make sure readEncoder() doesn't change the value
// while we're reading it.
int getCounter() {
int result;
noInterrupts();
result = counter;
interrupts();
return result;
}
int getPosition() {
int result;
noInterrupts();
result = counter;
interrupts();
return result;
}
void resetCounter() {
if (counter!=0)
stepperMotor.step(-counter);
noInterrupts();
counter = 0;
interrupts();
}
void lcdUpdate(int steps, int counter){
lcd.setCursor(0, 0);
lcd.print("RPM:");
lcd.setCursor(9, 0);
//lcd.print(getCounter());
lcd.print(steps,10);
lcd.setCursor(0, 1);
lcd.print("Position:");
lcd.setCursor(9, 1);
lcd.print(counter,10);
//lcd.print(" ");
}
void setup() {
// set the speed at 60 RPM:
stepperMotor.setSpeed(500);
// initialize the serial port:
//Serial.begin(9600);
// encoder part
Serial.begin(115200);
// Initialize LCD
lcd.init();
lcd.backlight();
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_SW, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING);
//readRPM();
pinMode(BUTTON_PIN, INPUT_PULLUP);
int value = HIGH;
while (value==HIGH){
stepperMotor.step(-1);
value = digitalRead((BUTTON_PIN));
}
}
void loop() {
if (digitalRead(ENCODER_SW) == LOW) {
resetCounter();
}
/*
newPosition = analogRead(potpin);
newPosition = map(newPosition, 0, 1023, 0, 50);
RPM = map(newPosition, 0, 50 , 0, 5600);
lcdUpdate(RPM,newPosition);
//if (newPosition != lastPosition) {
// There was a change on the CLK pin
if (newPosition > lastPosition) {
//newPosition = map(val, 0, 1023, 0, 50);
//steps = newPosition;
stepperMotor.step(newPosition-lastPosition);
}
if (newPosition < lastPosition) {
//newPosition = map(val, 0, 1023, 0, 50);
//steps = newPosition;
stepperMotor.step(-(lastPosition-newPosition));
}
lastPosition = newPosition;
throttleChange = true;
*/
//}
//Serial.println(analogRead(potpin),DEC);
//Serial.println(lastPosition,DEC);
//Serial.println(RPM,DEC);
//delay(100);
/*
if (digitalRead(ENCODER_SW) == LOW) {
resetCounter();
}
*/
/*
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 50);
stepperMotor.step(val);
val = 0;
Serial.println(analogRead(potpin));
*/
// step one revolution in one direction:
//Serial.println("clockwise");
//stepperMotor.step(1);
//delay(500);
/*
// step one revolution in the other direction:
Serial.println("counterclockwise");
stepperMotor.step(-stepsPerRevolution);
delay(500);*/
/*
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH) {
counter++; // Clockwise
stepperMotor.step(1);
}
if (dtValue == LOW) {
counter--; // Counterclockwise
stepperMotor.step(-1);
}*/
/*
// encoder part
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
// There was a change on the CLK pin
lastClk = newClk;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
stepperMotor.step(10);
//counter++; // Clockwise
Serial.println("Rotated clockwise ⏩");
}
if (newClk == LOW && dtValue == LOW) {
stepperMotor.step(20);
//counter--; // Clockwise
Serial.println("Rotated counterclockwise ⏪");
}
}*/
}