I'm doing a project and I would like to set values in my project from the setup() loop. I want to input a value from the keypad then press # to exit the loop. the number i input will be put into a switch case which will set values in global variables and these global variables will be used for various things in the program. what these values do are not important at the moment. Below is my code
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
char key = 0;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'}, // Keypad 1 map
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'},
};
byte rowPins[ROWS] = {2,3,4,5}; //connect to row pinouts
byte colPins[COLS] = {6,7,8,9}; //connect to column pinouts
int f=0;
static float SV_magnitude = 0; //0.8f
static float inverterFrequency = 0; //60
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.init();
// Print a message to the LCD.
lcd.backlight();
//setFrequency();
setFrequency();
}
void loop(){
//setFrequency();
//Serial.print("Inverter Frequency = ");Serial.print(inverterFrequency);Serial.print("\n");
//Serial.print("IModulation Index = ");Serial.print(SV_magnitude);Serial.print("\n");
//key = keypad.getKey();
/*if (key != NO_KEY){
lcd.setCursor(3,0);
lcd.print(key);
Serial.println(key);
}*/
}
void setFrequency()
{
key = keypad.getKey();
if(f==0){
lcd.setCursor(0,0);
lcd.print("Enter Mode: ");
f=1;
}
while (key != '#') //if (key != NO_KEY) Replacewhile statement and uncomment setFrequency() in loop and comment out in setup to see desired functionality
{
lcd.setCursor(12,0);
lcd.print(key);
}
Serial.print(key);
switch(key)
{
case '1':
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Vs = 23 VRMS");
lcd.setCursor(0,1);
lcd.print("fs = 6Hz");
inverterFrequency = 6;
SV_magnitude = 0.0907f;
break;
case '2':
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Vs = 46 VRMS");
lcd.setCursor(0,1);
lcd.print("fs = 12Hz");
inverterFrequency = 12;
SV_magnitude = 0.1814f;
break;
case '3':
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Vs = 69 VRMS");
lcd.setCursor(0,1);
lcd.print("fs = 18Hz");
inverterFrequency = 18;
SV_magnitude = 0.2721f;
break;
case '4':
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Vs = 92 VRMS");
lcd.setCursor(0,1);
lcd.print("fs = 24Hz");
inverterFrequency = 24;
SV_magnitude = 0.3628f;
break;
case '5':
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Vs = 115 VRMS");
lcd.setCursor(0,1);
lcd.print("fs = 30Hz");
inverterFrequency = 30;
SV_magnitude = 0.4535f;
break;
case '6':
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Vs = 138 VRMS");
lcd.setCursor(0,1);
lcd.print("fs = 36Hz");
inverterFrequency = 36;
SV_magnitude = 0.5442f;
break;
case '7':
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Vs = 161 VRMS");
lcd.setCursor(0,1);
lcd.print("fs = 42Hz");
inverterFrequency = 42;
SV_magnitude = 0.6349f;
break;
case '8':
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Vs = 184 VRMS");
lcd.setCursor(0,1);
lcd.print("fs = 48Hz");
inverterFrequency = 48;
SV_magnitude = 0.7256f;
break;
case '9':
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Vs = 207 VRMS");
lcd.setCursor(0,1);
lcd.print("fs = 54Hz");
inverterFrequency = 54;
SV_magnitude = 0.8163f;
break;
case '0':
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Vs = 230 VRMS");
lcd.setCursor(0,1);
lcd.print("fs = 60Hz");
inverterFrequency = 60;
SV_magnitude = 0.907f;
break;
default:
break;
}
}
As the code is right now its not working, i want to be able to input values from the setup().