0% found this document useful (0 votes)
22 views5 pages

Isd Code4

The document describes code for a stroboscope device that uses a microcontroller, LCD display, and keypad to control the RPM and frequency. It defines pins and variables, includes libraries, and contains functions for the LCD display, number input, and RPM calculation.

Uploaded by

Khushi Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
22 views5 pages

Isd Code4

The document describes code for a stroboscope device that uses a microcontroller, LCD display, and keypad to control the RPM and frequency. It defines pins and variables, includes libraries, and contains functions for the LCD display, number input, and RPM calculation.

Uploaded by

Khushi Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 5

#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(){
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
//lcd.backlight(); // Make sure backlight is on
lcd.setCursor(4,0);
lcd.print("Welcome!");
//lcd.setCursor(0,1);
//lcd.print("Dr. Prajit Nandi");
delay(3000);
lcd.clear();
delay(1000);
lcd.setCursor(1,0);
lcd.print("Stroboscope is");
lcd.setCursor(5,1);
lcd.print("ready!");
delay(3000);
lcd.clear();
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 = instr.toInt();


if (b == 0){
lcd.clear();
deftxt();
return;
}
int z = keycount;
z = 11 - z;
lcd.clear();
delay(100);
deftxt();
lcd.setCursor(z, 0);
lcd.print(b);
rpm = b;
Hz = b / 60;
int a = 0, y = Hz;
while (y != 0){
y = y / 10;
a++;
}
a = 11 - a;
lcd.setCursor(a, 1);
lcd.print(Hz);
delay(50);
Serial.print(rpm);
Serial.print(",");
Serial.print(b);
Serial.print(",");
Serial.println(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;
lcd.setCursor(0, 0);
lcd.print(" rpm");
clearMem();
deftxt();
}

// Action when the Change frequency is pressed.


// int cancelBtn = analogRead(A2);
// if (cancelBtn > threshold) {
// // Button pressed, execute the following actions
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Last RPM: ");
// lcd.print(lastRpm); // Display the last entered RPM
// instr = String(lastRpm); // Restore the last entered frequency
// keycount = instr.length(); // Set the keycount to the length of the last
entered frequency
// deftxt();
// return;
// }

// Keypad press detect


customKey = customKeypad.getKey();

// Action on - button detect


if (customKey == '-'){
int a = instr.toInt();
if (a <= 0){
error();
delay(1000);
lcd.clear();
deftxt();
return;
}
a--;
rpm++;
instr = String(a);
keycount--;
rpmValue();
}

// Action on + button detect


if (customKey == '+'){
int a = instr.toInt();
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);
lcd.clear();
rpm = 0;
deftxt();
}
}

void deftxt(){ //Default text to show in LCD


lcd.clear();
lcd.setCursor(10,0);
lcd.print("0 rpm");
lcd.setCursor(10,1);
lcd.print("0 Hz");
return;
}
bool numKey(){
Code[keycount]=customKey;
instr += customKey;
rpmValue();
Serial.println(instr);
//Serial.println(Code[keycount]);
//lcd.print(Code[keycount]);
//Serial.println(instr.length()); //To check in case of 0 and - btn
keycount++;
delay(60);
}
void rpmValue(){ //char conversion to int
lcd.setCursor(0,0);
lcd.print(" rpm");
long int a,z;
a = (instr.toInt());
z = 10-keycount;
lcd.setCursor(z,0);
lcd.print(a);
}
void error(){ //Action on error detect
lcd.clear();
Serial.println("ERR");
lcd.setCursor(5,0);
lcd.print("Error!");
lcd.setCursor(1,1);
lcd.print("Limit Exceeded");
}
void clearMem(){
//lcd.setCursor(0,0);
//lcd.print(" rpm");
memset(Code, 0, sizeof(Code));
keycount = 0;
instr = "";
}

You might also like