Hi. I am struggling with this code. I wrote a code that reads two rpm(s) from two input pulses, and converts them to a ratio.
The program runs fine until I tried to incorporate it into code to use a LCD keypad shield. The menu displays like intended but the RPMs will not run. I have tried moving the code out of the Menu A to inside the void loop, I have tried moving the code to the MainMenuDisplay, but havent had any success.
Any help would be appreciated.
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);
int keypad_pin = A0;
int keypad_value = 0;
int keypad_value_old = 0;
char btn_push;
byte mainMenuPage = 1;
byte mainMenuPageOld = 1;
byte mainMenuTotal = 3;
const int dataINA= 2; //pulse sensor INPUT
const int dataINB= 3; //pulse sensor INPUT
const int actuatorA= 13; //actuator power when out of sync
const int actuatorB= 12; //actuator power when out of sync
unsigned long prevmillis; //to store time
unsigned long duration; //to store time difference
unsigned long lcdrefresh; //to store time for lcd to refresh
int rpmA; //RPM value
int rpmB; //RPM value
float ratioA= rpmA/rpmB;
float ratioB= rpmB/rpmA;
boolean currentstate; //current stste of pulse scan
boolean prevstate; //state of pulse in previous scan
void setup()
{
lcd.begin(16,2); //Initialize a 2x16 type LCD
MainMenuDisplay();
delay(1000);
pinMode(dataINA,INPUT);
pinMode(dataINB,INPUT);
pinMode(actuatorA,OUTPUT);
pinMode(actuatorB,OUTPUT);
prevmillis=0;
prevstate=LOW;
ratioA= rpmA/rpmB;
ratioB= rpmB/rpmA;
}
void loop()
{
btn_push = ReadKeypad();
MainMenuBtn();
if(btn_push == 'S')//enter selected menu
{
WaitBtnRelease();
switch (mainMenuPage)
{
case 1:
MenuA();
break;
case 2:
MenuB();
break;
case 3:
MenuC();
break;
}
MainMenuDisplay();
WaitBtnRelease();
}
delay(10);
}//--------------- End of loop() loop ---------------------
void MenuA()
{
// RPM Measurement
currentstate = digitalRead(dataINA); // Read pulse
if( prevstate != currentstate) // If there is change in input
{
if( currentstate == HIGH ) // If input only changes from LOW to HIGH
{
duration = ( micros() - prevmillis ); // Time difference between revolution in microsecond
rpmA = (60000000/duration); // rpm = (1/ time millis)10001000*60;
prevmillis = micros(); // store time for nect revolution calculation
ratioA= (rpmA/rpmB);
}
}
prevstate = currentstate; // store this scan (prev scan) data for next scan
// LCD Display
if( ( millis()-lcdrefresh ) >= 100)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("A=");
lcd.print(rpmA);
lcd.print(" R/");
lcd.print(ratioA);
if (ratioA>ratioB){
digitalWrite(13,HIGH);
}else{
if (ratioA<ratioB);
digitalWrite(13,LOW);
}
{
// RPM Measurement
currentstate = digitalRead(dataINB); // Read pulse
if( prevstate != currentstate) // If there is change in input
{
if( currentstate == HIGH ) // If input only changes from LOW to HIGH
{
duration = ( micros() - prevmillis ); // Time difference between revolution in microsecond
rpmB = (60000000/duration); // rpm = (1/ time millis)10001000*60;
prevmillis = micros(); // store time for nect revolution calculation
ratioB= (rpmA/rpmB);
}
}
prevstate = currentstate; // store this scan (prev scan) data for next scan
}
// LCD Display
if( ( millis()-lcdrefresh ) >= 100)
{
lcd.setCursor(0,1);
lcd.print("B=");
lcd.print(rpmB);
lcd.print(" R/");
lcd.print(ratioB);
lcdrefresh = millis();
while(ReadKeypad()!= 'L')
if (ratioA>ratioB){
digitalWrite(13,HIGH);
}else{
if (ratioA<ratioB);
digitalWrite(13,LOW);
}
}
}
}
void MenuB()
{
lcd.clear();
lcd.setCursor(4,0);
lcd.print("RIGHT +");
lcd.setCursor(5,1);
lcd.print(ratioA);
while(ReadKeypad()!= 'L')
{
//Insert Task for Menu B here
}
}
void MenuC()
{
lcd.clear();
lcd.setCursor(5,0);
lcd.print("LEFT +");
lcd.setCursor(5,1);
lcd.print(ratioB);
while(ReadKeypad()!= 'L')
{
//Insert Task for Menu C here
}
}
void MainMenuDisplay()
{
lcd.clear();
lcd.setCursor(3,0);
switch (mainMenuPage)
{
case 1:
lcd.print("Main Menu");
lcd.setCursor(2,1);
lcd.print("press select");
break;
case 2:
lcd.print("RIGHT +");
lcd.setCursor(2,1);
lcd.print("press select");
break;
case 3:
lcd.print("LEFT +");
lcd.setCursor(2,1);
lcd.print("press select");
break;
}
}
void MainMenuBtn()
{
WaitBtnRelease();
if(btn_push == 'U')
{
mainMenuPage++;
if(mainMenuPage > mainMenuTotal)
mainMenuPage = 1;
}
else if(btn_push == 'D')
{
mainMenuPage--;
if(mainMenuPage == 0)
mainMenuPage = mainMenuTotal;
}
if(mainMenuPage != mainMenuPageOld) //only update display when page change
{
MainMenuDisplay();
mainMenuPageOld = mainMenuPage;
}
}
char ReadKeypad()
{
/* Keypad button analog Value
no button pressed 1023
select 741
left 503
down 326
up 142
right 0
*/
keypad_value = analogRead(keypad_pin);
if(keypad_value < 100)
return 'R';
else if(keypad_value < 200)
return 'U';
else if(keypad_value < 400)
return 'D';
else if(keypad_value < 600)
return 'L';
else if(keypad_value < 800)
return 'S';
else
return 'N';
}
void WaitBtnRelease()
{
while( analogRead(keypad_pin) < 800){}
}