#include <LiquidCrystal.
h>
#include <Key.h>
#include <Keypad.h>
//#include <LiquidCrystal_I2C.h>
const int comparatorPositivePin = A5;
/*#include <Keypad.h>
#include <LiquidCrystal_I2C.h>*/
const int rs = 2, en = 3, d4 = 10, d5 = 11, d6 = 12, d7 = 13;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//Motor A
const int motorDirectionPin = A3;
const int motorSpeedPin = A2;
//LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x3F for a 16 chars
and 2 line display
#define codeLength 5
//Declaration of Global Variables
char Code[codeLength];
byte keycount = 0;
char customKey;
String instr = "";
int enterBtn = 0, changeFreqBtn = 0, cancelBtn = 0, signalState = 0;
long int rpm = 0;
long int Hz = 0;
long int lastRpm = 0; // Variable to store the last entered RPM
const byte ROWS = 4;
const byte COLS = 3;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'-', '0', '+'}
};
byte rowPins[ROWS] = {5, 6, 7, 8};
byte colPins[COLS] = {0, 1, 4};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
[Link](9600);
[Link](16, 2);
[Link]();
//[Link](); // Make sure backlight is on
[Link](4,0);
[Link]("Welcome!");
//[Link](0,1);
//[Link]("Dr. Prajit Nandi");
delay(3000);
[Link]();
delay(1000);
[Link](1,0);
[Link]("Stroboscope is");
[Link](5,1);
[Link]("ready!");
delay(3000);
[Link]();
deftxt();
pinMode(9, OUTPUT);
// initialize the pushbutton pin 11,10,12 as an input:
pinMode(A0, INPUT);
pinMode(A1, INPUT);
// pinMode(A2, INPUT);
pinMode(motorDirectionPin, OUTPUT);
pinMode(motorSpeedPin, OUTPUT);
}
void loop(){
//Stroboscope LED blinking
if (signalState == 1){
float y = 60000.0 / rpm;
digitalWrite(9, HIGH);
delay(y);
digitalWrite(9, LOW);
delay(y);
}
// Action when the ENTER pushbutton is pressed.
int enterBtn = analogRead(A0);
int threshold = 500; // Adjust this threshold as needed
// Check if the analog reading exceeds the threshold
if (enterBtn > threshold) {
// Button pressed, execute the following actions
digitalWrite(9, HIGH); // Turn on the LED
long int b = [Link]();
if (b == 0){
[Link]();
deftxt();
return;
}
int z = keycount;
z = 11 - z;
[Link]();
delay(100);
deftxt();
[Link](z, 0);
[Link](b);
rpm = b;
Hz = b / 60;
int a = 0, y = Hz;
while (y != 0){
y = y / 10;
a++;
}
a = 11 - a;
[Link](a, 1);
[Link](Hz);
delay(50);
[Link](rpm);
[Link](",");
[Link](b);
[Link](",");
[Link](Hz);
signalState = 1;
}
// Action when the CANCEL pushbutton is pressed.
int changeFreqBtn = analogRead(A1);
if (changeFreqBtn > threshold) {
// Button pressed, execute the following actions
signalState = 0;
[Link](0, 0);
[Link](" rpm");
clearMem();
deftxt();
}
// Action when the Change frequency is pressed.
// int cancelBtn = analogRead(A2);
// if (cancelBtn > threshold) {
// // Button pressed, execute the following actions
// [Link]();
// [Link](0, 0);
// [Link]("Last RPM: ");
// [Link](lastRpm); // Display the last entered RPM
// instr = String(lastRpm); // Restore the last entered frequency
// keycount = [Link](); // Set the keycount to the length of the last
entered frequency
// deftxt();
// return;
// }
// Keypad press detect
customKey = [Link]();
// Action on - button detect
if (customKey == '-'){
int a = [Link]();
if (a <= 0){
error();
delay(1000);
[Link]();
deftxt();
return;
}
a--;
rpm++;
instr = String(a);
keycount--;
rpmValue();
}
// Action on + button detect
if (customKey == '+'){
int a = [Link]();
a++;
rpm++;
instr = String(a);
keycount--;
rpmValue();
}
// Map the RPM to the PWM range (0-255 corresponds to 0-5V)
int pwmValue = map(rpm, 0, 60000, 0, 255);
// Write the PWM value to the comparator's positive terminal
analogWrite(comparatorPositivePin, pwmValue);
//Action on number key detect
if(customKey){
digitalWrite(9, LOW);
signalState = 0;
numKey();
}
// Read the comparator output
int comparatorOutput = analogRead(A4);
// Map the comparator output to the RPM range
long rpm2 = map(comparatorOutput, 0, 1023, 0, 60000);
// Map the RPM to the PWM range
int pwmValue2 = map(rpm2, 0, 60000, 0, 255);
// Control the motor speed
analogWrite(motorSpeedPin, pwmValue2);
// Control the motor direction
digitalWrite(motorDirectionPin, HIGH);
//Action on more than 5 digit input
if(keycount>codeLength){
error();
memset(Code, 0, sizeof(Code));
keycount = 0;
instr = "";
delay(2000);
[Link]();
rpm = 0;
deftxt();
}
}
void deftxt(){ //Default text to show in LCD
[Link]();
[Link](10,0);
[Link]("0 rpm");
[Link](10,1);
[Link]("0 Hz");
return;
}
bool numKey(){
Code[keycount]=customKey;
instr += customKey;
rpmValue();
[Link](instr);
//[Link](Code[keycount]);
//[Link](Code[keycount]);
//[Link]([Link]()); //To check in case of 0 and - btn
keycount++;
delay(60);
}
void rpmValue(){ //char conversion to int
[Link](0,0);
[Link](" rpm");
long int a,z;
a = ([Link]());
z = 10-keycount;
[Link](z,0);
[Link](a);
}
void error(){ //Action on error detect
[Link]();
[Link]("ERR");
[Link](5,0);
[Link]("Error!");
[Link](1,1);
[Link]("Limit Exceeded");
}
void clearMem(){
//[Link](0,0);
//[Link](" rpm");
memset(Code, 0, sizeof(Code));
keycount = 0;
instr = "";
}