Pages: [1]   Go Down
Author Topic: Keypad is not operated in sub-function.  (Read 348 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have made Sensor-valve-LCD-Keypad system.

if I input an weight of water of a height of water using keypad, arduino control the amount of water using sensor.

but code is not operated well, if i compile the code, there is no error.

Code:

#include <LiquidCrystal.h>//LCD Header file load
const int sensorIR=0;//IR sensor
const int sensorUS=1;//Ultrasonic sensor
const int sensorRC=2;
const int numRows = 4;//the number of Rows
const int numCols = 4;// The number of Col
const int debounceTime = 30;//pushing time
const int valve=13;
const int threshold = 1;
LiquidCrystal lcd(22,26,30,34,38,42); //lcd number 4, 6, 11, 12, 13, 14

const char keymap[numRows][numCols] = {
  { '1', '2', '3', 'q' },
  { '4', '5', '6', 'w' },
  { '7', '8', '9', 'e' },
  { '*', '0', '#', 'r' }
};

const int rowPins[numRows] = {6, 7, 8, 9}; //keypad sw4,5,6,7
const int colPins[numCols] = {2, 3, 4, 5}; //keypad sw0,1,2,3


//===================set up=====================================//
void setup() {
  pinMode(valve,OUTPUT);// valve
    
  //lcd.begin(8,2);//lcd setup
  Serial.begin(9600);
  for (int row = 0; row < numRows; row++)//keypad setup
  {
    pinMode(rowPins[row], INPUT);
    digitalWrite(rowPins[row], HIGH);
  }
  for(int column = 0; column < numCols; column++)
  {
    pinMode(colPins[column], OUTPUT);
    digitalWrite(colPins[column], HIGH);
  }
  Serial.println("MODE");//print on LCD
}

// ===========grobal constant==========



char attempt[4]; // char of inputs
int ans[4],data; // int of inputs
int z=0;

//=====================getKey====================



char getKey()// getting key from key pad
{
  char key = 0;
  
  for (int column = 0; column < numCols; column++)
   {
    digitalWrite(colPins[column], LOW);
    for(int row = 0; row < numRows; row++)
    {
      if(digitalRead(rowPins[row]) == LOW)
      {
        delay(debounceTime);
        while(digitalRead(rowPins[row])==LOW)
        ;
        key = keymap[row][column];
      }
    }
    digitalWrite(colPins[column], HIGH);
  }
  return key;
}

//============correctpin==================================

void correctPIN() // do this if correct PIN entered
{
  Serial.println("* ok *");
  data=1000*ans[0]+100*ans[1]+10*ans[2]+ans[3];
  //lcd.setCursor(0,1); // the lcd cursor moves to the second line
  Serial.print(data);
  delay(1000);
  for(int zzz;zzz<z;zzz++)
  {
    ans[zzz]=0;
  }
  
}



//===============incorrect pin   ================


void incorrectPIN() // do this if incorrect PIN entered
{
  Serial.print("TryAgain");
  delay(1000);

  Serial.print("MODE");
}



//================ check pin====================



void checkPIN()
{
   for (int zz=0; zz<z; zz++) // wipe attempt
  {
    ans[zz]=int(attempt[zz])-48;
    attempt[zz]=0;
  }
  correctPIN();
}


//===================readKeypad===========================


void readKeypad()// Keypad read
{
 
   char key1 = getKey();
  Serial.print("press *");
 
  if (key1 != 0)                     [b]  //[color=red]========> not operate[/color]
  {[/b]
    switch(key1)
    {
    case '*'://turn on getting keypad system
      z=0;
      //lcd.setCursor(0,1);
      Serial.print(key1);
      break;
    case '#':// turn off getting keypad system
      delay(100); // for extra debounce
      if (z!=4)
      {
        incorrectPIN();//check 4 digits number
      }
      else
      {checkPIN();}//move to checkPIN function
      
      break;
    default:
      attempt[z]=key1;//displace component of array from keypad
      //lcd.setCursor(0,1);
      Serial.print(attempt[z]);// print key on lcd
      z++;
    }
  }
}


//=======================ultrasonic====================

float ultrasonic()//Ultrasonic sensor code
{
   int valueUS=analogRead(sensorUS);//receive data from sensor
   float rangeUS=valueUS/3.8+25;//change data to mm unit
   delay(10);//10ms delay
   return rangeUS;// return data to height function
}  


//====================IRsensor===========================

float IRsensor()// IR sensor
{
  
  float valueIR = analogRead(sensorIR);//receive data from sensor
  float rangeIR;  
  if (valueIR>threshold)
  {
  rangeIR=((2914/(valueIR+5))-1)*10;//chagne to data to mm unit
    
  }
  return rangeIR;//return data to height function
}

//=============================Height function==================//

void height()//height control system
{
  readKeypad();// get data from keypad
  digitalWrite(valve,HIGH);// open valve
  while(1)// infinite loop to close valve
  {
  float IRdata =IRsensor();//get data from IRsensor
  float USdata = ultrasonic();//get data from US sensor
  if((data-2+USdata<=IRdata)&&(IRdata<=USdata+data+2))
  {
   digitalWrite(valve,LOW);//close valve
   break;//break while(1)
  }
  }
}

/*void weight()
{
  readKeypad();
  digitalWrite(valve,HIGH);
  while(1)
  {
  flaot rangeRC =loadcell();
  if(data-2<rangeRC<data-2)
  {
    digitalWrite(valve,LOW)
    break;
  }
}
}

float loadcell()
{
  float valRC=analogRead(2);
  return valRC;
}
*/  








//======================= main function======================//

void loop() {
  char key = getKey();// get key from keypad
  if (key != 0)
  {
    switch(key)
    {
    case 'q':// calling height function
      height();
      break;
    case 'r':
//      weight();
      break;
  }
  }
}

====

 my code is not perfect because i didn't program for loadcell. but I made a code for height mode.
the algorithm


1.select mode in void loop(using get key function)
2. height fuction
3. readkeypad ( problem occurs in this fuction. the screen shows "press *" after that there is no response. no progression. if I push q button , "press *" show on screen again.
4. check key or incollect key
5. collect key
6. get data from collect key function.
7. compare 'data' with sensors data
8. close valve.

Pleas help me. I spent so much time to solve this problem. but i couldn't find out the solution.
« Last Edit: December 06, 2012, 06:14:25 am by jsy1903 » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop() {
  char key = getKey();// get key from keypad
  if (key != 0)
  {
    switch(key)
    {
    case 'q':// calling height function
      height();
      break;
    case 'r':
      //      weight();
      break;
    }
  }
}


So pressing 'q' calls height.

Code:
void height()//height control system
{
  readKeypad();// get data from keypad
  digitalWrite(valve,HIGH);// open valve
  while(1)// infinite loop to close valve
  {
    float IRdata =IRsensor();//get data from IRsensor
    float USdata = ultrasonic();//get data from US sensor
    if((data-2+USdata<=IRdata)&&(IRdata<=USdata+data+2))
    {
      digitalWrite(valve,LOW);//close valve
      break;//break while(1)
    }
  }
}

That calls read readKeypad.

Code:
void readKeypad()// Keypad read
{

  char key1 = getKey();
  Serial.print("press *");

  if (key1 != 0)                     
  {
   
      switch(key1)
    {
    case '*'://turn on getting keypad system

Unless you have super-human responses you won't be able to type that fast. The keypad library is non-blocking. It won't wait for you to press the key. You need to restructure.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

how do i change the structure to operate well? to make wating time, i insert delay but, it shows same response... too difficult,.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49443
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i insert delay but, it shows same response... too difficult,.
You need to make readKeypad() blocking function. Add a while(key1 == NO_KEY) statement that reads the key in the body.

Code:
char key1 = getKey();
while(key1 == NO_KEY)
{
   key1 = getKey();
}

This will end when you press some key.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

how do i change the structure to operate well? to make wating time, i insert delay but, it shows same response... too difficult,.

I never said to insert a delay. Only have one call to getKey. Keep some "states". For example:

Code:
void loop() {
  char key = getKey();// get key from keypad
  if (key != 0)
  {
    switch(key)
    {
    case 'q':// calling height function
      gettingHeight = true;
      break;

    case 'r':
      gettingWeight = true;
      break;

    case '*':
     // do something
     break;

    case '0' ... '9':
    // collect digits
    break;
    }
  }
}
Logged


Pages: [1]   Go Up
Jump to: