// 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 firstRun = true;
int lastClk = HIGH;
////int throttleRangeMinPin = 2; // analog pin used to connect the potentiometer
////int throttleRangeMaxPin = 3; // analog pin used to connect the potentiometer
////int rpm;
////int rpm2;
int rpmRandom;
int randomNumber;
int randomNumberEngine;
int randomNumberVolume;
//int throttleLast = map(analogRead(throttlePin), 0, 1023, 0, 50);
////int throttleLast = 0;
double microphone;
double microphonePow;
double sound;
double soundDb;
double rpmPow;
//int rangeMin;
//int rangeMax;
int rounds;
int valve;
int throttlePin = 1;
////int throttleStart = 0;
int throttle = analogRead(throttlePin); // variable to read the value from the analog pin from rpm throttle
int throttleLast = 0;
////int throttleSlider;
////int throttleNew;
////int throttleNew2 = 0;
int rpmMin = 1000;
int rpmMax = 5600;
int rpm;
int rpmThrottle;
int rpmMinLimit = rpmMin;
int rpmMaxLimit = rpmMax;
int rpmStartPin = 2; // analog pin used to connect the potentiometer from set min rpm range
int rpmStopPin = 3; // analog pin used to connect the potentiometer from set max rpm range
int rpmStart = analogRead(rpmStartPin);
int rpmStop = analogRead(rpmStopPin);
////int rpmRange;
////int throttleRangeMin = analogRead(throttleRangeMinPin);
////int throttleRangeMinSet= 1000;
////int throttleRangeMax = analogRead(throttleRangeMaxPin);
////int throttleRangeMaxSet = 5600;
void engineSound () {
//microphone = analogRead(A0);
microphone = rpmRandom;
randomNumberEngine = random(-10, 10);
randomNumberVolume = random(-5, 5);
soundDb = 50 + randomNumberVolume + log10(pow(microphone / 10, 10)) * throttleLast / 15;
tone(7, (soundDb / 20) * (500 + randomNumberEngine), 5);
//Serial.println(soundDb, DEC);
//delay(5);
digitalWrite(13, abs (microphone - 512) > 50);
}
void readRPM() {
//throttleNew2 = 0;
//throttle = analogRead(throttlePin);
rpm = map(analogRead(throttlePin), 0, 1023, rpmMin, rpmMax);
rpmRandom = rpm + random(0, 100);
//randomNumber = random(0, 100);
//if ((rpm>throttleRangeMinSet)&&(rpm<throttleRangeMaxSet)) {
// throttleSlider = map(rpm, throttleRangeMinSet, throttleRangeMaxSet, 0, 1023);
// throttleSlider = map(throttle, 0, 1023, 0, (throttleRangeMaxSet-throttleRangeMinSet));
//rpmThrottle = map(rpm, rpmMin, rpmMax, rpmMinLimit, rpmMaxLimit);
//if () {
if (rpm>rpmMinLimit&&rpm<rpmMaxLimit){
throttle = map(rpm, rpmMinLimit, rpmMaxLimit, 0, 50);
myStepper.step(throttle-throttleLast);
throttleLast = throttle;
}
//}
//throttleLast = throttle;
//throttleLast = throttle;
//throttleNew2 = map(throttleNew, 0, (throttleRangeMaxSet-throttleRangeMinSet), 0, 50);
//throttleLast = throttleNew;
// throttleNew = throttleSlider;
//throttleNew = map(throttleSlider, 0,throttleRangeMaxSet, 0, 50);
//throttleNew = map(rpm, throttleRangeMinSet, throttleRangeMaxSet, 0, 50);
//Serial.println(throttleSlider,DEC);
//}
// rpm2 = map(throttle, 0, 1023, throttleRangeMinSet, throttleRangeMaxSet);
// rpmRandom = rpm + randomNumber;
// reads the value of the potentiometer (value between 0 and 1023)
//if (!(rpm>throttleRangeMinSet&&rpm<throttleRangeMaxSet)) {
//else {
//throttleNew = map(rpm, 900, 5600, 0, 50);}
// throttleNew = map(throttle, 0, 1023, 0, 50);
//rpm = map(throttleNew, 0, 50, throttleRangeMinSet, throttleRangeMaxSet) + randomNumber;
//}
}
void readRangeRPM () {
rpmStart = analogRead(rpmStartPin);
if (rpmMinLimit<rpmMaxLimit) rpmMinLimit= map(rpmStart, 0, 1023, rpmMin, rpmMax);
if (rpmMinLimit>=rpmMaxLimit) rpmMinLimit=rpmMaxLimit-1;
rpmStop = analogRead(rpmStopPin);
if (rpmMaxLimit>rpmMinLimit) rpmMaxLimit = map(rpmStop, 0, 1023, rpmMin, rpmMax);
if (rpmMaxLimit<=rpmMinLimit) rpmMaxLimit=rpmMinLimit+1;
/*throttleRangeMin = analogRead(throttleRangeMinPin);
if (throttleRangeMinSet<throttleRangeMaxSet) throttleRangeMinSet= map(throttleRangeMin, 0, 1023, 900, 5600);
if (throttleRangeMinSet>=throttleRangeMaxSet) throttleRangeMinSet=throttleRangeMaxSet-1;
throttleRangeMax = analogRead(throttleRangeMaxPin);
if (throttleRangeMaxSet>throttleRangeMinSet) throttleRangeMaxSet = map(throttleRangeMax, 0, 1023, 900, 5600);
if (throttleRangeMaxSet<=throttleRangeMinSet) throttleRangeMaxSet=throttleRangeMinSet+1; */
}
//Serial.println(throttleRangeMinNew,DEC);
/*
void readEncoder() {
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH) {
counter++; // Clockwise
myStepper.step(1);
}
if (dtValue == LOW) {
counter--; // Counterclockwise
myStepper.step(-1);
}
}
void resetCounter() {
if (valve != 0)
myStepper.step(-valve);
noInterrupts();
valve = 0;
interrupts();
}
*/
void lcdUpdate(int rounds, int valve, double sound, int rangeMin, int rangeMax) {
lcd.setCursor(0, 0);
lcd.print("Min:");
lcd.setCursor(5, 0);
lcd.println(rangeMin, DEC);
lcd.setCursor(10, 0);
lcd.print("Max:");
lcd.setCursor(15, 0);
lcd.println(rangeMax, DEC);
lcd.setCursor(0, 1);
lcd.print("RPM:");
lcd.setCursor(5, 1);
lcd.println(rounds, DEC);
lcd.setCursor(10, 1);
lcd.print("Valve:");
lcd.setCursor(17, 1);
lcd.println(valve, DEC);
lcd.setCursor(0, 3);
lcd.print("Decibels:");
lcd.setCursor(10, 3);
lcd.print(sound);
}
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(500);
// initialize the serial port:
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
tone(7, (900 / 1000) * 500, 5);
pinMode(BUTTON_PIN, INPUT_PULLUP);
int value = LOW;
//int value = HIGH;
while (value == HIGH) {
myStepper.step(-1);
value = digitalRead((BUTTON_PIN));
}
//throttle = 0;
//myStepper.step(0);
}
void loop() {
readRangeRPM();
readRPM();
engineSound();
//if (digitalRead(ENCODER_SW) == LOW)
// resetCounter();
lcdUpdate(rpm, throttle, soundDb, rpmMinLimit, rpmMaxLimit);
}