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)