Reset Function is not working in my code

I am using a reset function in my code but for some reason it’s not resetting my Arduino.
I tested the function with other code and it worked but in this one it didn’t. I tried calling the function in different spot in the code but no results.
This project is using an Arduino Uno, a keypad, shift registers, LEDs, resistors and wires.
I am working on breadboard to test before I put the project together.
Basically the project is like this: when the person pushes a key a message start scrolling if the PIR detected motion if not the Arduino should reset itself through the reset function (which I tested and worked) and the Arduino waits to detect another person or wait for another key to be pushed and display another message (of course if motion is detected)
I hope someone looks at my code and help me find out why the resetFunc() is not working.
Note: I only have one case in the switch. I am trying first to get work with one. The rest should be the same.
Currently, when I push the Key “1” from the keypad nothing happen until i wave my hand near the PIR which is what I want but it keeps scrolling for ever even if I am not near the PIR.
Thank you.

#include <Keypad.h>
const byte ROWS = 2;
const byte COLS = 2;
char keys[ROWS][COLS] =
{
  {
    '1','3'    }
  ,
  {
    '7','9'    }
  ,
};

byte rowPins [ROWS] = {
  5,6};
byte colPins [COLS] = {
  7,8};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


int pirPin = 9;
int dataPin1 = 13;     //Name Pin. Define which pins will be used for the Shift Register control
int dataPin2 = 10;     //Off Pin
int dataPin3 = 11;    //CVN Pin
int dataPin4 = 12;    //Vacation Pin
int dataPin5 = 2;    //Here Pin
int latchPin = 3;
int clockPin = 4; 
int val = 0;
int pirState = LOW;
int PirState = PirFun();
int seq[14] = {
  1,2,4,8,16,32,64,128,64,32,16,8,4,2};       //The byte sequence



void setup()
{
  Serial.begin (9600); 
  pinMode(dataPin1, OUTPUT);
  pinMode(dataPin2, OUTPUT);
  pinMode(dataPin3, OUTPUT);
  pinMode(dataPin4, OUTPUT);
  pinMode(dataPin5, OUTPUT);     
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(pirPin, INPUT);
}



void handleKeypress (const char key)
{
  Serial.println (key);
}


void(* resetFunc)(void)=0;



boolean PirFun(){
  val = digitalRead(pirPin);
}




void loop()

{

  for (int n = 0; n < 14; n++)
  {
    digitalWrite(latchPin, LOW);             //Pull latch LOW to start sending data
    shiftOut(dataPin1, clockPin, MSBFIRST, seq[n]);          //Send the data
    digitalWrite(latchPin, HIGH);            //Pull latch HIGH to stop sending data
    delay(30);
  }



  byte key = kpd.getKey();
  if (key)
    handleKeypress (key);

  switch (key) {
  case '1':

   
    while(PirState == HIGH){




      for (int n = 0; n < 14; n++)
      {
       
        digitalWrite(latchPin, LOW);             //Pull latch LOW to start sending data
        shiftOut(dataPin1, clockPin, MSBFIRST, seq[n]);          //Send the data
        digitalWrite(latchPin, HIGH);            //Pull latch HIGH to stop sending data
        delay(10);


      }
      for (int n = 0; n < 14; n++)
      {


        digitalWrite(latchPin, LOW);             //Pull latch LOW to start sending data
        shiftOut(dataPin2, clockPin, MSBFIRST, seq[n]);          //Send the data
        digitalWrite(latchPin, HIGH);            //Pull latch HIGH to stop sending data
        delay(10);


      }
 

    }

    break;

  }

if(PirState == LOW){

        resetFunc();
      }
}

I am using a reset function in my code

Why? You should fix the problem that leads you to think you need a reset function.

when the person pushes a key a message start scrolling if the PIR detected motion if not the Arduino should reset itself through the reset function

When one can't even punctuate a sentence, I have to wonder just how good their code is.

int dataPin1 = 13;     //Name Pin. Define which pins will be used for the Shift Register control
int dataPin2 = 10;     //Off Pin
int dataPin3 = 11;    //CVN Pin
int dataPin4 = 12;    //Vacation Pin
int dataPin5 = 2;    //Here Pin

If you used meaningful names the comments wouldn't be needed. Vacation pin?

int PirState = PirFun();

You should not be calling this function here. Is this executed before init() has gotten the hardware ready, or after?

void handleKeypress (const char key)
{
  Serial.println (key);
}

How is that "handling" the key press?

Why do you think it is necessary to reset the Arduino when there is no motion? It is NOT!

Answer to the last question. I thought of resetting because when I do it manually on the arduino it allows me to push another button to display another message, if I didn't, pushing another button will not get me out of the loop. If I fixed that I wouldn't need a reset function but after multiple tries, I thought using this software reset will help even though I didn't like it.

I don't know how that function (i.e. handlekeypress) works. If I removed it, somehow, the keypad doesn't input anything when I press a button. Thank you for your feedback

ferkheusa: I don't know how that function (i.e. handlekeypress) works. If I removed it, somehow, the keypad doesn't input anything when I press a button. Thank you for your feedback

I think @PaulS was commenting on the name of the function rather than its content. A name like causeKeyPress() would seem more appropriate.

Using reset in code is a bit like crashing your car into a wall in preference to using the brakes.

...R

@OP: Your requirements can probably be handled by a state machine:

http://gammon.com.au/statemachine

boolean PirFun(){
  val = digitalRead(pirPin);
}

Where is the return statement that actually returns a boolean? Fail!

I think @PaulS was commenting on the name of the function rather than its content. A name like causeKeyPress() would seem more appropriate.

It doesn't cause a key press. It prints a key that is pressed. Since what it does is in no way handling the key that was pressed, the function name should not claim that it does.

I can't see anything in the Keypad library that necessitates the need for handleKeyPress(), and I can't see that the way you use the function accomplishes anything.

Currently, when I push the Key "1" from the keypad nothing happen until i wave my hand near the PIR which is what I want but it keeps scrolling for ever even if I am not near the PIR.

You need to identify the part of the code that causes scrolling. You need to put that part of the code in a function, with a useful name, like scrollText(), NOT some useless name like handleText().

You need to put every { on a new line.

You need to use Tools + Auto Format.

You need to get rid of the useless blank lines (and useless functions) in that code.

Then, maybe you can see what the problem is. But, Robin2 is right. Using reset is like crashing your car to avoid the need to sterr or brake.

As PaulS said earlier about this:

int dataPin2 = 10;     //Off Pin
int dataPin3 = 11;    //CVN Pin
int dataPin4 = 12;    //Vacation Pin
int dataPin5 = 2;    //Here Pin

Your code is mumbo-jumbo without meaningful names, like:

int OffPin = 10;     
int CVNPin = 11;   
int VacationPin = 12;    
int HerePin = 2;

The comments are all well and good, but good names persist through all the code.

And if they never change, say so:

const int OffPin = 10;     
const int CVNPin = 11;   
const int VacationPin = 12;    
const int HerePin = 2;

Thank you so much for all these good feedbacks. Hahaha. I was desperate that's why I chose to crash my car instead of falling from a cliff :). I did change the names to something meaningful. You are right, the function handlekeypress is only for printing the numbers. It doesn't do anything else. I will clean my code and try to figure out another way to fix the problem. Thank you very much

I cleaned my code, however I am stuck in an infinite loop. I tried the if statement in my scrolltext function but the scroll happen only one time. When I use the while loop it get stuck there for ever and pressing another key doesn’t do anything. It just keep scrolling at the first one I pressed. Here is my new code.

#include <Keypad.h>
const byte ROWS = 2;
const byte COLS = 2;
char keys[ROWS][COLS] =
{
  {
    '1','3'      }
  ,{
    '7','9'      }
  ,};
byte rowPins [ROWS] = {
  5,6};
byte colPins [COLS] = {
  7,8};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
const int pirPin = 9;
const int namePin = 13;     
const int offPin = 10;     
const int cvnPin = 11;    
const int vacatPin = 12;    
const int herePin = 2;    
const int latchPin = 3;
const int clockPin = 4;
int val = 0;
int seq[14] = {
  1,2,4,8,16,32,64,128,64,32,16,8,4,2};       
void scrollText(int X, int Y ){
 val = digitalRead(pirPin);
 Serial.println('val');
  while(val == HIGH){
    for (int n = 0; n < 14; n++)
    {
      digitalWrite(latchPin, LOW);             
      shiftOut(X, clockPin, MSBFIRST, seq[n]);         
      digitalWrite(latchPin, HIGH);            
      delay(50);
    }
    for (int n = 0; n < 14; n++)
    {
      digitalWrite(latchPin, LOW);             
      shiftOut(Y, clockPin, MSBFIRST, seq[n]);          
      digitalWrite(latchPin, HIGH);            
      delay(50);
    }
  }
}  
void setup()
{
  Serial.begin (9600); 
  pinMode(namePin, OUTPUT);
  pinMode(offPin, OUTPUT);
  pinMode(cvnPin, OUTPUT);
  pinMode(vacatPin, OUTPUT);
  pinMode(herePin, OUTPUT);     
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(pirPin, INPUT);    
}
void loop()
{
 
  byte key = kpd.getKey();
  if (key)
    switch (key) {
    case '1':
      scrollText(namePin,offPin);
      break;
    case '3':
      scrollText(namePin,cvnPin);
      break;
    case '7':
      scrollText(namePin,vacatPin);
      break;
    case '9': 
      scrollText(namePin,herePin);
      break;
    }
}

I hope you give me some suggestions or solution to the problem. Thank you.

Well it would get stuck wouldn’t it?

void scrollText(int X, int Y ){
 val = digitalRead(pirPin);
 Serial.println('val');
 while(val == HIGH){
    for (int n = 0; n < 14; n++)
    {
      digitalWrite(latchPin, LOW);             
      shiftOut(X, clockPin, MSBFIRST, seq[n]);         
      digitalWrite(latchPin, HIGH);            
      delay(50);
    }
    for (int n = 0; n < 14; n++)
    {
      digitalWrite(latchPin, LOW);             
      shiftOut(Y, clockPin, MSBFIRST, seq[n]);          
      digitalWrite(latchPin, HIGH);            
      delay(50);
    }
  }
}

You never re-read val.

How about reading it each time through the loop?

void scrollText(int X, int Y ){
 while(digitalRead(pirPin) == HIGH){    // <---- re-read each time
    for (int n = 0; n < 14; n++)
    {
      digitalWrite(latchPin, LOW);             
      shiftOut(X, clockPin, MSBFIRST, seq[n]);         
      digitalWrite(latchPin, HIGH);            
      delay(50);
    }
    for (int n = 0; n < 14; n++)
    {
      digitalWrite(latchPin, LOW);             
      shiftOut(Y, clockPin, MSBFIRST, seq[n]);          
      digitalWrite(latchPin, HIGH);            
      delay(50);
    }
  }
}

You are an awesome person. Thank you very much and thanks for everyone who provided feedbacks. I cleaned the code and payed attention to everything you guys said.
My code now does what I want it to do. I can stop here if I want but there is one little thing that I wish I can fix: I get stuck in the while loop. It’s not a big issue because I can wait for the PIR to go LOW before I push (no problem) but it would be better if I don’t have to wait. Someone suggested to use a counter in the loop but I have to change my code and put everything in the loop which means I need to start over again.
I would like if there is another way around this. Thank you very much
here is my current code:

#include <Keypad.h>
const byte ROWS = 2;
const byte COLS = 2;
char keys[ROWS][COLS] =
{
  {
    '1','3'        }
  ,{
    '7','9'        }
  ,};
byte rowPins [ROWS] = {
  5,6};
byte colPins [COLS] = {
  7,8};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
const int pirPin = 9;
const int namePin = 13;     
const int offPin = 10;     
const int cvnPin = 11;    
const int vacatPin = 12;    
const int herePin = 2;    
const int latchPin = 3;
const int clockPin = 4;
byte storeKey;
byte key;
int seq[14] = {
  1,2,4,8,16,32,64,128,64,32,16,8,4,2};       
void scrollText(int X, int Y ){
  while(digitalRead(pirPin) == HIGH){
    for (int n = 0; n < 14; n++)
    {
      digitalWrite(latchPin, LOW);             
      shiftOut(X, clockPin, MSBFIRST, seq[n]);         
      digitalWrite(latchPin, HIGH);            
      delay(50);
    }
    for (int n = 0; n < 14; n++)
    {
      digitalWrite(latchPin, LOW);             
      shiftOut(Y, clockPin, MSBFIRST, seq[n]);          
      digitalWrite(latchPin, HIGH);            
      delay(50);
    }
  }
}  
void setup()
{
  Serial.begin (9600); 
  pinMode(namePin, OUTPUT);
  pinMode(offPin, OUTPUT);
  pinMode(cvnPin, OUTPUT);
  pinMode(vacatPin, OUTPUT);
  pinMode(herePin, OUTPUT);     
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(pirPin, INPUT);    
}
void loop()
{
  byte key = kpd.getKey();
  if (key)
  storeKey = key;
    switch (storeKey) {
    case '1':
      scrollText(namePin,offPin);
      break;
    case '3':
      scrollText(namePin,cvnPin);
      break;
    case '7':
      scrollText(namePin,vacatPin);
      break;
    case '9': 
      scrollText(namePin,herePin);
      break;
    }
}

You could get rid of the while loop in scrollText. Make it an if statement, instead.

Then, in loop, instead of calling the scrollText() function only when the key changes, call it every time, with whatever value corresponds to the last key pressed. Change the last key pressed in the switch statement.

I think I tried the if statement before and it scrolls only once. I want the scroll to stay until the PIR goes low. But I'll try it again. Thanks

I think I tried the if statement before and it scrolls only once. I want the scroll to stay until the PIR goes low. But I'll try it again.

Unless you change how often you call the function, that will continue to be true. You need to call it every pass through loop().

Thank you very much.