Hi, I'm new to Arduino and I'm trying to use a keypad for user input, where the user can enter the time and temperature. I'm having difficulties in getting the temperature input, I can get the time input but cannot figure out the temperature part.
Any help will be highly appreciated. I'm posting my code below.
Thanks
void setup()
{
Serial.begin(9600);
sensors.begin();
//setup and turn off both LEDs
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
pinMode(tempPin, INPUT);
pinMode(countPin, INPUT);
servoLeft.attach(46); // Set left servo to digital pin 10
servoRight.attach(44); // Set right servo to digital pin 9
lcd.begin(16, 2);
selection();
//setup default time to 00:00
showtime[0]='0';
showtime[1]='0';
showtime[2]='0';
showtime[3]='0';
currentTempValue[0]='0';
currentTempValue[1]='0';
}
lcd.clear();
lcd.print("Options");
delay(2000);
lcd.setCursor(0,0);
lcd.print("1. Set Timer");
lcd.setCursor(0,1);
lcd.print("2. Set Temp");
delay(5000);
lcd.clear();
}
void tempMenu(){
digitalWrite(redLED, HIGH);
lcd.print("Temp: ");
temp();
}
//Function should show the temperature reading
void GetTemp(){ //Function for Getting the Temperature
int time;
int secs;
time = millis();
if (time >= secs){ // 1 second loop
secs = time + 1000;
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
float tempF = DallasTemperature::toFahrenheit(tempC); // The Temp in Fahrenheit
lcd.print("Temp: F");
lcd.setCursor(6, 0);
lcd.print(tempF);
delay(500);
}
}
void timeMenu(){
digitalWrite(redLED, HIGH);
lcd.print("Time:");
timer();
}
int timer(){
digitalWrite(redLED, LOW);
char tempVal[3];
char key1 = keypad.getKey();
//key pressed and state is 1
if (int(key1) != 0 and currentState == 1) {
switch (key1) {
case '*':
Status(false);
showtime[0]='0';
showtime[1]='0';
showtime[2]='0';
showtime[3]='0';
showEnteredTime();
currentState = 1;
num = 0;
timerSeconds = 0;
break;
case '#':
tempVal[0] = showtime[0];
tempVal[1] = showtime[1];
tempVal[2] = 0;
timerSeconds = atol(tempVal) * 60;
tempVal[0] = showtime[2];
tempVal[1] = showtime[3];
tempVal[2] = 0;
timerSeconds = timerSeconds + atol(tempVal);
currentState = 2;
break;
case 'A':
ProjectDisplay();
break;
default:
showtime[0] = showtime[1];
showtime[1] = showtime[2];
showtime[2] = showtime[3];
showtime[3] = key1;
showEnteredTime();
break;
}
}
if (currentState == 2) {
if (int(key1) != 0) {
if (key1 == '*') {
Status(false);
selection();
showtime[0]='0';
showtime[1]='0';
showtime[2]='0';
showtime[3]='0';
showEnteredTime();
currentState = 1;
num = 0;
timerSeconds = 0;
}
}
else {
if (num > 9) {
num = 0;
--timerSeconds;
showtimer();
if (timerSeconds == 0) {
currentState = 1;
Status(false); //servo should be closed
servoLeft.write(15); //Valve Closed
servoRight.write(75); //Valve Closed
lcd.clear();
ProjectDisplay();
}
else {
Status(true);
}
}
++num;
delay(100);
}
}
}
//temp
int temp(){
char tempVal1[2];
char key2 = keypad.getKey();
//key pressed and state is 1
if (int(key2) != 0 and currentState == 1) {
switch (key2) {
case '*':
Status(false);
currentTempValue[0]='0';
currentTempValue[1]='0';
showEnteredTemp();
currentState = 1;
num = 0;
tempset = 0;
break;
case '#':
tempVal1[0] = currentTempValue[0];
tempVal1[1] = currentTempValue[1];
tempVal1[2] = 0;
tempset = atol(tempVal1);
currentState = 2;
break;
case 'A':
ProjectDisplay();
break;
default:
currentTempValue[0] = currentTempValue[1];
showtime[1] = currentTempValue[2];
showtime[2] = key2;
showEnteredTemp();
break;
}
}
if (currentState == 2) {
if (int(key2) != 0) {
if (key2 == '*') {
Status(false);
selection();
currentTempValue[0]='0';
currentTempValue[1]='0';
showEnteredTemp();
currentState = 1;
num = 0;
tempset = 0;
}
}
else {
if (num > 9) {
num = 0;
//showtimer();
}
else {
Status(true);
}
++num;
delay(100);
}
}
}
void showEnteredTime()
{
lcd.setCursor(10,0);
lcd.print(showtime[0]);
lcd.print(showtime[1]);
lcd.print(":");
lcd.print(showtime[2]);
lcd.print(showtime[3]);
}
void showEnteredTemp()
{
lcd.setCursor(10,1);
lcd.print(currentTempValue[0]);
lcd.print(currentTempValue[1]);
}
void showtimer()
{
lcd.clear();
char timest[6];
lcd.setCursor(0,0);
lcd.print("TIME: ");
sprintf(timest, "%d:%.2d", (timerSeconds/60), (timerSeconds - ((timerSeconds/60)*60)));
lcd.print(timest);
}
void Status(bool state)
{
if (state)
digitalWrite(greenLED, HIGH);
else
//servo set angel to zero
//The Valve Closes since time is reached
digitalWrite(greenLED, LOW);
}
void selection(){
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
tempState = digitalRead(tempPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
lcd.clear();
timeMenu();
}
}
if (tempState != lasttempState) {
// if the state has changed, increment the counter
if (tempState == HIGH) {
lcd.clear();
tempMenu();
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
lasttempState = tempState;
}
void loop(){
selection();
timer();
temp();
// sensors.requestTemperatures();
// float tempC = sensors.getTempCByIndex(0);
// float tempF = DallasTemperature::toFahrenheit(tempC); // The Temp in Fahrenheit
}