keypad

hi friends
new to arduino, this is my second code, just going from having stepper motors turn repeatedly ( a code i know works) to having them turn repeatedly after i press a number, in this case “1”.

edit** the error is that the motors dont turn - my guess is something in the getKey area. no error messages present.

also, why is “void setup” always empty/what is this for?

thanks
-F

#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;
char keys [ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPINS[ROWS] = {16,17,18,19};
byte colPINS[COLS] = {2,3,4,5};

Keypad keypad = Keypad( makeKeymap(keys), rowPINS, colPINS, ROWS, COLS );

void setup() {

}

void loop() 
  {
int ENA = 10; //enable stepper 1
int DIR = 11; //direction stepper 1
int PUL = 12; //pulse stepper 1

int ENA2 = 6; //enable stepper 2
int DIR2 = 7; //direction stepper 2
int PUL2 = 8; //pulse stepper 2


  
  // put your main code here, to run repeatedly:
    
    char key = keypad.getKey();

  
if (key == '1')
  {
int rotation = 3200;
int vel = 50;
int vel2 = 200;


  for (int i=0; i<rotation; i++)    //Forward 5000 steps
  {
    digitalWrite(DIR,LOW);       
    digitalWrite(ENA,HIGH);
    digitalWrite(PUL,HIGH);    
    delayMicroseconds(vel);
    digitalWrite(PUL,LOW);
    delayMicroseconds(vel);

    digitalWrite(DIR2,LOW);
    digitalWrite(ENA2,HIGH);
    digitalWrite(PUL2,HIGH);
    delayMicroseconds(vel2);
    digitalWrite(PUL2,LOW);
    delayMicroseconds(vel2);
  }
  
  }

  }

It is for all the things that a program has to do at the beginning, if there are

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we're trying to read your code.

The setup function is called once when your program is started. It's common for there to be some code that you only want to run once at the start so this was considered a useful feature for Arduino users. In some cases there will be no such code and so you might leave it empty. In this case you might find setup() useful for setting the pin modes of your output pins:

const byte ENA = 10; //enable stepper 1
const byte DIR = 11; //direction stepper 1
const byte PUL = 12; //pulse stepper 1

const byte ENA2 = 6; //enable stepper 2
const byte DIR2 = 7; //direction stepper 2
const byte PUL2 = 8; //pulse stepper 2

void setup() {
  pinMode(ENA, OUTPUT);
  pinMode(DIR, OUTPUT);
  pinMode(PUL, OUTPUT);
  pinMode(ENA2, OUTPUT);
  pinMode(DIR2, OUTPUT);
  pinMode(PUL2, OUTPUT);
}

The not setting the pin mode of the pins is likely why the motors didn't turn.

More information:

The setup() function is used for functionality that you only want to run once.

E.g. setting up serial communication using Serial.begin(), setting pins as input or output using pinMode(), telling the lcd library that you're using a 16x2 lcd, displaying a welcome message on that lcd etc.

The loop() function is used for functionality that you want to run repeatedly.

E.g. checking the status of a button using digitalRead(), reading data from a sensor (or keypad in your case) and take action based on that data.

Note that void setup us usually not empty: look at any of the examples that come with the IDE.

Also, pin numbers are usually not defined in a function but in the global section of the code before setup() and loop() so they are known everywhere and you can use pinMode() on them in setup() and digitalWrite() in loop() without to define them multiple times.