Combining Code

I have different two programs: one gets a number from a keypad, the other counts up when a sensor is interrupted.

I want to combine these two codes.

The code shown is both, with the sensor code that needs placement at the very bottom. I was hoping that after pressing the * key, then the program would take over sensor counting. I've tried quite a few placements of the code and got nothing. Any suggestions?

#include <Keypad.h>
int pirPin = 10; //Sensor code
int counter = 0;//Sensor code
int laststate = HIGH; //Sensor code

int v1 = 0;
int v2 = 0;
int v3 = 0;
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);
  pinMode(pirPin,INPUT_PULLUP); //Sensor code
}

void loop()
{
   v1 = GetNumber();
   v2 = GetNumber();
   v3 = GetNumber();

}

int GetNumber()
{
   int num = 0;
   char key = kpd.getKey();
   while(key != '#')
   {
      switch (key)
      {
         case NO_KEY:
            break;

         case '0': case '1': case '2': case '3': case '4':
         case '5': case '6': case '7': case '8': case '9':
            Serial.print(key);
            num = num * 10 + (key - '0');
            break;

         case '*':
            Serial.println(num);
            break;
      }

      key = kpd.getKey();
   }

   return num;
}

//Sensor Code ->Code below needs placed somewhere
int state = digitalRead(pirPin);
  if (laststate == LOW && state == HIGH) // only count on a LOW-> HIGH transition
  {
    counter++;
    Serial.println(counter);
  }
  laststate = state;  // remember last state

This code needs to go after the last GetNumber in loop. Right now it is not part of loop, nor is it part of a function.

//Sensor Code ->Code below needs placed somewhere
int state = digitalRead(pirPin);
if (laststate == LOW && state == HIGH) // only count on a LOW-> HIGH transition
{
int num = Serial.read();
num++;
Serial.println(num);
}
laststate = state; // remember last state

CrossRoads:
This code needs to go after the last GetNumber in loop. Right now it is not part of loop, nor is it part of a function.

Yeah, I've tried that, and just checked it again, to no result. I know it's not part of the function, that's strictly the code for the sensor counting that needs to be integrated into the keypad code (the rest of the code, minus a few spots noted by comments)

   while(key != '#')
   {
      switch (key)
      {
         case NO_KEY:
            break;

So, 99.999% of the time, when the function is called, it will break out of the while loop and return 0.

That doesn't sound all that useful.

void loop()
{
   v1 = GetNumber();
   v2 = GetNumber();
   v3 = GetNumber();
}

How does this actually accomplish anything?

PaulS:

   while(key != '#')

{
     switch (key)
     {
        case NO_KEY:
           break;



So, 99.999% of the time, when the function is called, it will break out of the while loop and return 0.

That doesn't sound all that useful.



void loop()
{
  v1 = GetNumber();
  v2 = GetNumber();
  v3 = GetNumber();
}



How does this actually accomplish anything?

resets the number to 0 when pressed, in case the wrong number is typed in.

I think what you're getting at with the loop code is for this application it can just be GetNumber(); which is correct and changed to rid of unnecessary code.

But these things aside, the point of the topic is inserting the sensor code into the keypad code. The idea behind the project is to input a number, press * to allow the sensor to start counting, and count from the number every time the sensor beam is broken. Both the individual codes work well for their jobs, I just need to put them together now.

Any other suggestions on my problem?

resets the number to 0 when pressed, in case the wrong number is typed in.

I understand that. However, getKey() is not a blocking function. It tells you what key is pressed, if any, when you ask it what key is pressed. If no key is being pressed when you ask, the return value will be NO_KEY, so you break out of the while loop, and return.

So, for many, many passed through loop(), v1, v2, and v3 are going to get 0 stored in them. And, for what? You never use v1, v2, and v3 anyway.

PaulS:

resets the number to 0 when pressed, in case the wrong number is typed in.

I understand that. However, getKey() is not a blocking function. It tells you what key is pressed, if any, when you ask it what key is pressed. If no key is being pressed when you ask, the return value will be NO_KEY, so you break out of the while loop, and return.

So, for many, many passed through loop(), v1, v2, and v3 are going to get 0 stored in them. And, for what? You never use v1, v2, and v3 anyway.

I apologize in my error of communication. Yes, the NO_KEY is more pointless code, as are the v's (both are removed from the code). I then tried running the sensor code in different places to see if that helped and I am still getting nothing.

So your code looks like this?

#include <Keypad.h>
int pirPin = 10; //Sensor code
int counter = 0;//Sensor code
int laststate = HIGH; //Sensor code


const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);
  pinMode(pirPin,INPUT_PULLUP); //Sensor code
}

void loop()
{
     GetNumber();

}

int GetNumber()
{
   int num = 0;
   char key = kpd.getKey();
   while(key != '#')
   {
      switch (key)
      {

         case '0': case '1': case '2': case '3': case '4':
         case '5': case '6': case '7': case '8': case '9':
            Serial.print(key);
            num = num * 10 + (key - '0');
            break;

         case '*':
            Serial.println(num);
            break;
      }

      key = kpd.getKey();
   }

   return num;
}

//Sensor Code ->Code below needs placed somewhere
int state = digitalRead(pirPin);
  if (laststate == LOW && state == HIGH) // only count on a LOW-> HIGH transition
  {
    counter++;
    Serial.println(counter);
  }
  laststate = state;  // remember last state

I think you are trying to print a 3 digit no. What exactly does

num=num*10+(key-0)

do when num is always 0?I get what you are trying to do.
make num a global variable.
and remove
int num=0
as for combining the code, you can do the following

case '*':
Serial.println(num);
int x=0;

and put this in loop

while(x==0&&/condition to stop counting is false/)
{
int state = digitalRead(pirPin);
if (laststate == LOW && state == HIGH) // only count on a LOW-> HIGH transition
{
counter++;
Serial.println(counter);
}
}

And if there is no condition to stop counting,remove
&&/condition to stop counting is false/

suvratroxx:
I think you are trying to print a 3 digit no. What exactly does

num=num*10+(key-0)

do when num is always 0?I get what you are trying to do.
make num a global variable.
and remove
int num=0
as for combining the code, you can do the following

case '*':
Serial.println(num);
int x=0;

and put this in loop

while(x==0&&/condition to stop counting is false/)
{
int state = digitalRead(pirPin);
if (laststate == LOW && state == HIGH) // only count on a LOW-> HIGH transition
{
counter++;
Serial.println(counter);
}
}

Your suggestions make the code this:

#include <Keypad.h>

int pirPin = 10; //Sensor code
int counter = 0;//Sensor code
int laststate = HIGH; //Sensor code
int num = 0;

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);
  pinMode(pirPin,INPUT_PULLUP); //Sensor code
}

void loop()
{
   GetNumber();
   while(x==0&&counter!=0)
{
int state = digitalRead(pirPin);
  if (laststate == LOW && state == HIGH) // only count on a LOW-> HIGH transition
  {
    counter++;
    Serial.println(counter);
  }
}
}

int GetNumber()
{
   char key = kpd.getKey();
   while(key != '#')
   {
      switch (key)
      {            
         case '0': case '1': case '2': case '3': case '4':
         case '5': case '6': case '7': case '8': case '9':
            Serial.print(key);
            num = num * 10 + (key - '0');
            break;

         case '*':
            Serial.println(num);
            int x = 0;
            break;
      }

      key = kpd.getKey();
   }
   return num;
}

I'm sorry if it seems like I'm digging for every small answer but I have put effort into trying to work my errors to no success.
x won't work in the loop because it's undefined, and what should increment in the sensor code? I have it set as counter but should it be num? Do I need something like a Serial.read to make it work?