Using a keyboard intead of jumpers

Hi all

I am having problem to solve a tread in my sketch.
These sketch drive a stepper motor in some positions according to jumper grd to pins.
The first version I use a selector to do this. Now I want to include a keyboard 4x4 to control the stepper
through numbers.

Could you please give a direction on how to do this. Attached the sketch
Sorry for my english…

Turntable_with_buttonS_V2_22.ino (5.81 KB)

Why do you set all pins of the keypad to INPUT_PULLUP in your setup? You're overwriting the pinModes that the keypad constructor sets on line 33.

Once you get the right result from getKey() (line 159-162), you can just use a large switch to decide how much the motor should move, based on the key you pressed.

Remove pinmode for pin 4-11, and uncomment kpd.begin(). Then try this.
I don't have the libraries needed to compile so it might be some errors in it.

void loop()
{
  int num = 0;
  char key = kdp.getKey();
  if (key) {
    num = atoi(key);
    serial.println(num);
  }

  switch (num) {

    case 1:    // PARA 3200 STEPS DIVIDIDO POR 360 GRAUS É IGUAL A 8,888 STEPS POR GRAU O QUE EQUIVALE A 133,33 STEPS
      //  PARA CADA 15 GRAUS DA ATLAS E  177,77  STEPS PARA CADA 20 GRAUS DA FRATESCHI
      Astepper1.moveTo(0);
      Astepper1.run();
      Astepper1.runToPosition();
      break;

    case 2:
      Astepper1.moveTo(200);
      Astepper1.run();
      Astepper1.runToPosition();
      break;

    case 3:
      Astepper1.moveTo(400);
      Astepper1.run();
      Astepper1.runToPosition();
      break;

    case 4:
      Astepper1.moveTo(600);
      Astepper1.run();
      Astepper1.runToPosition();
      break;

    case 5:
      Astepper1.moveTo(800);
      Astepper1.run();
      Astepper1.runToPosition();
      break;

    case 6:
      Astepper1.moveTo(1000);
      Astepper1.run();
      Astepper1.runToPosition();
      break;

    case 7:
      Astepper1.moveTo(1200);
      Astepper1.run();
      Astepper1.runToPosition();
      break;

    case 8:
      Astepper1.moveTo(1400);
      Astepper1.run();
      Astepper1.runToPosition();
      break;

  }
}

I don't have the libraries needed to compile so it might be some errors in it.

It does. Right here:

  char key = kdp.getKey();
  if (key) {
    num = atoi(key);

atoi() takes a NULL terminated array of chars, not a single char. It is trivial to convert '3' to 3. Just subtract '0' from '3'.

  char key = kdp.getKey();
  if (key) {
    num = key - '0';

Handling the non-digit keys is left to the switch statement or the OP.

Hi all

Although I have not tested so far no option described here,
only as information and posssivel corrections, I use the input-pullup on all pins because the first version
was to jumper grd with each pin through selector and thus make rotation of the stepper motor for each option.
Only as information, this sketch will be used on a turntable of a railway model

EDIT
I made the corrctions according Gabriel and PaulIs but the stepper dont stop on sensor and turn
clockwise and unticlockwise randomly. Any directions ?

Hi PaulS

Solved the problem with randomly rotation, and the stop on sensor.

Now another tread: the numbers pressed on keyboard does not corresponde to the cases: .number 1 to number 5 nothing happens. numbers 6 and 8 are correct., but letters B,C, 0 and # corresponde to case 3,2,1,7 and 4 respectively

Something is wrong with setup, but I dont know how to solve this issue.
Any help ?

Turntable_with_buttonS_V2_22.ino (5.22 KB)

From the information you give, I think this is how your codes interpret your keypad?
It is a diagonal flip of the keypad. . (dot), 8, 6 and A is correct. (Pins for rows and columns included)

11  D C B A
10  # 9 6 3
 9  0 8 5 2
 8  . 7 4 1

    7 6 5 4

Code says row pins is 11, 10, 9, 8 and column pins is 7, 6, 5, 4.
Button 1, 2 and 3 should be row 11 column 7, 6 and 5, but is actually row 8, 9 and 10, column 4.

You have the option to reconnect your wires or change the code. If code is changed to this, it is identical to the comment. Is that a coincident?

byte rowPINS[ROWS] = {4,5,6,7};    //PINAGEM NO KEYPAD  PINO 1 A 8 > PINO ARDUINO =   4,5,6,7 - 8,9,10,11
byte colPINS[COLS] = {8,9,10,11};

4  1 2 3 A
5  4 5 6 B
6  7 8 9 C
7  . 0 # D

  8 9 10 11

Button 4 is pin 5 and 8, looking at pin 5 and 8 at first table, it gave a 4 from the button #

Hi Gabriel

Thank you so much for the explanations. This "open" my mind about keypads.

The code was correct, I only inverted the positions of the pins. After that buttons 1,2 and 3
was not operate, and this makes me to think about the keypad.

My keypad is a "shing Ling" keypad ( from China !!!! ) and pin 1 was broken
I change to another keypad and voila..!!!. everything is running. Next step is inclusion of a LCD.

Thank you