problem combining two sketch

hello i’m trying to combine two sketch , the first one is a basic keypad sketch

#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'#','0','*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 13

void setup()
{
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin, HIGH);
  Serial.begin(9600);
}

void loop()
{
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  {
    switch (key)
    {
      case '9':
        digitalWrite(ledpin, LOW);
        break;
      case '6':
        digitalWrite(ledpin, HIGH);
        break;
      default:
        Serial.println(key);
    }
  }
}

the second one is a basic motor sketch

const int AIA = 9;  // (pwm) pin 9 connected to pin A-IA 
const int AIB = 5;  // (pwm) pin 5 connected to pin A-IB 
const int BIA = 10; // (pwm) pin 10 connected to pin B-IA  
const int BIB = 6;  // (pwm) pin 6 connected to pin B-IB 

byte speed = 255;  // change this (0-255) to control the speed of the motors 

void setup() {
  pinMode(AIA, OUTPUT); // set pins to output
  pinMode(AIB, OUTPUT);
  pinMode(BIA, OUTPUT);
  pinMode(BIB, OUTPUT);
}

void loop() {
  forward(); 
  delay(1000);
  backward();
  delay(1000);
  left();
  delay(1000);
  right();
  delay(1000);
}

void backward()
{
  analogWrite(AIA, 0);
  analogWrite(AIB, speed);
  analogWrite(BIA, 0);
  analogWrite(BIB, speed);
}

void forward()
{
  analogWrite(AIA, speed);
  analogWrite(AIB, 0);
  analogWrite(BIA, speed);
  analogWrite(BIB, 0);
}

void left()
{
  analogWrite(AIA, speed);
  analogWrite(AIB, 0);
  analogWrite(BIA, 0);
  analogWrite(BIB, speed);
}

void right()
{
  analogWrite(AIA, 0);
  analogWrite(AIB, speed);
  analogWrite(BIA, speed);
  analogWrite(BIB, 0);
}

so what i want to do is when i press a button it turn on a led , one button for led off and one button for make a motor going forward here is the sketch i end up with

#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'#','0','*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 }; 
const int AIA = 2;  // (pwm) pin 9 connected to pin A-IA 
const int AIB = 3;  // (pwm) pin 5 connected to pin A-IB 
const int BIA = 4; // (pwm) pin 10 connected to pin B-IA  
const int BIB = 5;  // (pwm) pin 6 connected to pin B-IB

byte speed = 180;


// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 13

void setup()
{
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin, HIGH);
  pinMode(AIA, OUTPUT); // set pins to output
  pinMode(AIB, OUTPUT);
  pinMode(BIA, OUTPUT);
  pinMode(BIB, OUTPUT);
  Serial.begin(9600);
}

void loop()
{

  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  {
    switch (key)
    {
      case '9':
        digitalWrite(ledpin, LOW);
        break;
      case '6':
        digitalWrite(ledpin, HIGH);
        break;
      default:
        Serial.println(key);
    }
  }
  
  {forward(); // The error on compiling starts here
  }
  
  void forward()
  {
    if(key)  // Check for a valid key.
   {
    switch (key)
     {
      case '2':
    analogWrite(AIA, speed);
    analogWrite(AIB, 0);
    analogWrite(BIA, speed);
    analogWrite(BIB, 0);


     break; 
     }
   }
}

can someone help me on it please? i just want to understand a bit better (because i’m new to coding ), y goal is not to make the motor run i know i can just put the case ‘2’ under the first switch and evrything should work , i want to learn how to make several “loop” (i don’t know if it’s the apropriate name)

This compiles but will not work because some of the motor control pins are not PWM enabled. Make sure that all 4 motor control pins are PWM enabled as in the comments and move some of the keypad pins to non PWM pins

#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'#', '0', '*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 };
const int AIA = 2;  // (pwm) pin 9 connected to pin A-IA
const int AIB = 3;  // (pwm) pin 5 connected to pin A-IB
const int BIA = 4; // (pwm) pin 10 connected to pin B-IA
const int BIB = 5;  // (pwm) pin 6 connected to pin B-IB

byte speed = 180;

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 13

void setup()
{
  pinMode(ledpin, OUTPUT);
  digitalWrite(ledpin, HIGH);
  pinMode(AIA, OUTPUT); // set pins to output
  pinMode(AIB, OUTPUT);
  pinMode(BIA, OUTPUT);
  pinMode(BIB, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  char key = kpd.getKey();
  if (key) // Check for a valid key.
  {
    switch (key)
    {
      case '9':
        digitalWrite(ledpin, LOW);
        break;
      case '6':
        digitalWrite(ledpin, HIGH);
        break;
      case '2':
        forward();
        break;
      default:
        Serial.println(key);
    }
  }
}

void forward()
{
  analogWrite(AIA, speed);
  analogWrite(AIB, 0);
  analogWrite(BIA, speed);
  analogWrite(BIB, 0);
}

This Simple Merge Demo may give you some ideas.

...R

hello helibob thanks for your reply , i gonna try this code and change the pin to pwm ones.

and robin thanks for your link i gonna check this out.