Help needed regarding adruino uno coding -switch loop with servo motor

I want to control servo motor with keypad 4x4.
Want to start continous loop on pressing particular button till another button is pressed for another continous loop.

I m new to programming and managed to cut copy and edit code for the same.

Problem is my code is verified on IDE and uploaded on adruino uno but not working.

#include <Servo.h>
Servo leftRightservo;

#include <Keyboard.h>

#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {
  {'4', '5', '6', 'B'},
  {'1', '2', '3', 'A'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
boolean backHandRun = false;  // NEW
boolean foreHandRun = false;  // NEW

void setup() {
  leftRightservo.attach(10);
  int position = 0;
  Serial.begin(9600);
}

void loop() {
  char key = customKeypad.getKey();
  Serial.println(key);

   if (key)  // Check for a valid key.
  {
    switch(key) {

    case '1': // Back Hand
      Serial.println("Back Hand Active");
      backHandRun = true; // NEW
      foreHandRun = false; // NEW
      break;

    case '2': // forehand
      Serial.println("Fore hand active");
      backHandRun = false;  // NEW
      foreHandRun = true;  // NEW
      break;
  }
  delay(10);


}
}
void foreHand() { // I want this to loop until i press a button on the remote

  if (foreHandRun == true) { // new

    leftRightservo.write(10);
    delay(100);
    leftRightservo.write(50);
    delay(100);
    leftRightservo.write(100);
    delay(100);
  }
}
void backHand() { // I want this to loop until i press a button on the remote

  if (backHandRun == true) { // new
    leftRightservo.write(90);
    delay(100);
    leftRightservo.write(180);
    delay(100);
  }
}

only servo motor initialize and doesnt move on pressing button.
Help me :frowning:

May you, please, take a pic of the hardware or create a fritzing?

I don’t want be pedantic, but I will...
I don’t see any reference to a ‘servo motor’ in your code..

Only the R/C servo - leftrightServo.

Good job using code tags with your first post.

The issue that I see with your code is that you have written your functions, and have boolean control variables for them, but you never call them in loop().

You need the understand the difference between declaring a function and calling it.
https://www.arduino.cc/en/Reference/FunctionDeclaration

In the loop() add the calls to your functions.

Your keypad layout looks peculiar. Is that indeed the arrangement of the keys? Typically 123A would be the top row.

#include <Servo.h>
Servo leftRightservo;

#include <Keyboard.h>

#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {
  {'4', '5', '6', 'B'},
  {'1', '2', '3', 'A'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
boolean backHandRun = false;  // NEW
boolean foreHandRun = false;  // NEW

void setup() {
  leftRightservo.attach(10);
  int position = 0;
  Serial.begin(9600);
}

void loop() {
  char key = customKeypad.getKey();
  Serial.println(key);

  if (key)  // Check for a valid key.
  {
    switch (key) {

      case '1': // Back Hand
        Serial.println("Back Hand Active");
        backHandRun = true; // NEW
        foreHandRun = false; // NEW
        break;

      case '2': // forehand
        Serial.println("Fore hand active");
        backHandRun = false;  // NEW
        foreHandRun = true;  // NEW
        break;
    }
    //delay(10);
    forehand();
    backhand();
  }
}
void foreHand() { // I want this to loop until i press a button on the remote

  if (foreHandRun == true) { // new

    leftRightservo.write(10);
    delay(100);
    leftRightservo.write(50);
    delay(100);
    leftRightservo.write(100);
    delay(100);
  }
}
void backHand() { // I want this to loop until i press a button on the remote

  if (backHandRun == true) { // new
    leftRightservo.write(90);
    delay(100);
    leftRightservo.write(180);
    delay(100);
  }
}

cattledog:
Good job using code tags with your first post.

The issue that I see with your code is that you have written your functions, and have boolean control variables for them, but you never call them in loop().

You need the understand the difference between declaring a function and calling it.
Arduino - FunctionDeclaration

In the loop() add the calls to your functions.

Your keypad layout looks peculiar. Is that indeed the arrangement of the keys? Typically 123A would be the top row.

#include <Servo.h>

Servo leftRightservo;

#include <Keyboard.h>

#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {
  {‘4’, ‘5’, ‘6’, ‘B’},
  {‘1’, ‘2’, ‘3’, ‘A’},
  {‘7’, ‘8’, ‘9’, ‘C’},
  {’*’, ‘0’, ‘#’, ‘D’}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
boolean backHandRun = false;  // NEW
boolean foreHandRun = false;  // NEW

void setup() {
  leftRightservo.attach(10);
  int position = 0;
  Serial.begin(9600);
}

void loop() {
  char key = customKeypad.getKey();
  Serial.println(key);

if (key)  // Check for a valid key.
  {
    switch (key) {

case ‘1’: // Back Hand
        Serial.println(“Back Hand Active”);
        backHandRun = true; // NEW
        foreHandRun = false; // NEW
        break;

case ‘2’: // forehand
        Serial.println(“Fore hand active”);
        backHandRun = false;  // NEW
        foreHandRun = true;  // NEW
        break;
    }
    //delay(10);
    forehand();
    backhand();
  }
}
void foreHand() { // I want this to loop until i press a button on the remote

if (foreHandRun == true) { // new

leftRightservo.write(10);
    delay(100);
    leftRightservo.write(50);
    delay(100);
    leftRightservo.write(100);
    delay(100);
  }
}
void backHand() { // I want this to loop until i press a button on the remote

if (backHandRun == true) { // new
    leftRightservo.write(90);
    delay(100);
    leftRightservo.write(180);
    delay(100);
  }
}

as i copied keypad codes and edited rows and coloumn according to my keypad wiring . thats why you can see them abnormal but keypad is fine when pressing key as it shows same value which i pressed.

could you please correct in code that how should function be declared in loop which i declared in last in code.

thanks in advance

could you please correct in code that how should function be declared in loop which i declared in last in code.

I actually tried to do that in the post above, but I didn't look carefully, and placed the calls at the wrong level in the code. :frowning:

Place the two lines which call the functions, right before the closing bracket of loop().

}
    delay(10);
  }//closing bracket for if(key)
forehand();
backhand();
}//closing bracket for loop

cattledog:
I actually tried to do that in the post above, but I didn't look carefully, and placed the calls at the wrong level in the code. :frowning:

Place the two lines which call the functions, right before the closing bracket of loop().

}

delay(10);
  }//closing bracket for if(key)
forehand();
backhand();
}//closing bracket for loop

adding these lines as you said give error when i verify on IDE = forehand was not declared :frowning:

help

dont know where is error in coding

dont know where is error in coding

My eyes were closed, but yours should be open. Spelling error. C++ is capitalization sensitive. Your use of "camel case" convention was good. My missing it was bad.

//forehand();
//backhand();
foreHand();
backHand();