Multiple inputs using keypad


hi, i'm new arduino , i'm trying use keypad user input, user can enter time , temperature. i'm having difficulties in getting temperature input, can time input cannot figure out temperature part.
any highly appreciated. i'm posting code below.
thanks

void setup()
{
  serial.begin(9600);
  sensors.begin();
 
  //setup , turn off both leds
  pinmode(redled, output);
  pinmode(greenled, output);
  digitalwrite(redled, low);
  digitalwrite(greenled, low);
 
  // initialize button pin input:
  pinmode(buttonpin, input);
  pinmode(temppin, input);
  pinmode(countpin, input);
 
 
  servoleft.attach(46);  // set left servo digital pin 10
  servoright.attach(44);  // set right servo digital pin 9
 
  lcd.begin(16, 2);
  selection();
  //setup default time 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 temperature reading
void gettemp(){ //function getting 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); // 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 , state 1
 if (int(key1) != 0 , 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 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 , state 1
 if (int(key2) != 0 , 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 zero
 //the valve closes since time reached
 digitalwrite(greenled, low);
}
void selection(){

 // read pushbutton input pin:
  buttonstate = digitalread(buttonpin);
  tempstate = digitalread(temppin);
 
  // compare buttonstate previous state
  if (buttonstate != lastbuttonstate) {
    // if state has changed, increment counter
    if (buttonstate == high) {
      lcd.clear();
      timemenu();
    }
  }
 
  if (tempstate != lasttempstate) {
    // if state has changed, increment counter
    if (tempstate == high) {
      lcd.clear();
      tempmenu();
    }
  }
 
 
  // save current state last state,
  //for next time through loop
  lastbuttonstate = buttonstate;
  lasttempstate = tempstate;
 
}



void loop(){
  selection();
  timer();
  temp();
//  sensors.requesttemperatures();
//  float tempc = sensors.gettempcbyindex(0);
//  float tempf = dallastemperature::tofahrenheit(tempc); // temp in fahrenheit
}

your indenting atrocious. there no excuse that, when tools + auto format fix you.

your code not posted. there no excuse that, either.

thereisnoexcusefornotuseingwhitespacearoundthecurlybraces,either.


code: [select]
  selection();
  timer();
  temp();

these crappy names. functions should have noun , verb.

when press key on keypad, how arduino supposed know whether set time or temporary? or temperature?


Arduino Forum > Using Arduino > Programming Questions > Multiple inputs using keypad


arduino

Comments