// Stepper motor on Wokwi!
#include <Stepper.h>
#include <LiquidCrystal_I2C.h>
#include "math.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; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
// encoder init
int lastClk = HIGH;
int counter = 0;
int steps = 0;
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int rpm = 900;
int newPosition;
int lastPosition = analogRead(potpin);
double microphone;
double sound;
double soundDb;
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);
myStepper.step(val);
}
void readEncoder() {
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH) {
counter++; // Clockwise
myStepper.step(1);
}
if (dtValue == LOW) {
counter--; // Counterclockwise
myStepper.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)
myStepper.step(-counter);
noInterrupts();
counter = 0;
interrupts();
}
void lcdUpdate(int steps, int counter, double sound) {
lcd.setCursor(0, 0);
lcd.print("RPM:");
lcd.setCursor(9, 0);
//lcd.print(getCounter());
lcd.print(steps);
lcd.setCursor(0, 1);
lcd.print("Position:");
lcd.setCursor(9, 1);
lcd.print(counter);
//lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("Sound:");
lcd.setCursor(9, 2);
lcd.print(sound);
}
void setup() {
// set the speed at 60 rpm:
myStepper.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);
pinMode(13, OUTPUT); // sound led indicator
pinMode(7, OUTPUT); // buzzer
//readRPM();
/*
pinMode(BUTTON_PIN, INPUT_PULLUP);
int value = HIGH;
while (value==HIGH){
myStepper.step(-1);
value = digitalRead((BUTTON_PIN));
}*/
}
void loop() {
newPosition = analogRead(potpin);
newPosition = map(newPosition, 0, 1023, 0, 50);
rpm = map(newPosition, 0, 50, 0, 5600);
// sound measurmemt
microphone = analogRead(A1);
//soundDb = log10(microphone);
soundDb = log10(pow(microphone,10));
//soundDb = (rpm/1000)*log10(pow(microphone,10));
Serial.println(soundDb);
delay(10);
digitalWrite(13, abs (microphone - 512) > 50);
tone(7,(rpm/1000)*500,1);
//lcdUpdate(rpm, newPosition, soundDb);
//if (newPosition != lastPosition) {
// There was a change on the CLK pin
if (newPosition > lastPosition) {
//newPosition = map(val, 0, 1023, 0, 50);
//steps = newPosition;
myStepper.step(newPosition - lastPosition);
}
if (newPosition < lastPosition) {
//newPosition = map(val, 0, 1023, 0, 50);
//steps = newPosition;
myStepper.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();
}
lcdUpdate(rpm, newPosition, soundDb);
/*
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 50);
myStepper.step(val);
val = 0;
Serial.println(analogRead(potpin));
*/
// step one revolution in one direction:
//Serial.println("clockwise");
//myStepper.step(1);
//delay(500);
/*
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);*/
/*
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH) {
counter++; // Clockwise
myStepper.step(1);
}
if (dtValue == LOW) {
counter--; // Counterclockwise
myStepper.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) {
myStepper.step(10);
//counter++; // Clockwise
Serial.println("Rotated clockwise ⏩");
}
if (newClk == LOW && dtValue == LOW) {
myStepper.step(20);
//counter--; // Clockwise
Serial.println("Rotated counterclockwise ⏪");
}
}*/
}