How to add 3 digits from keypad to array

here is the jumper in question.

not sure what they are jumpered to. but for this to work, they must be jumpered to a HIGH level signal.

fine, for now. the pins should be labeled as IN1 / IN2

sorry for the digression on the motor pins. guessing you've run the code since you said the motor works, but is it handling the commands as you expected?

It's handling them exactly as expected and with a hell of a lot less code and confusion. :smile:
Now my next thing on the agenda is getting keypad input from the user and also using the LCD to prompt their inputs. I already had that working with my code so shouldn't be to big a hill to climb.

Can I ask you, do you do this for a living or just a hobby that you're 
 very good at ? 

Thank you for all your help I really learned a lot, but if I run into troubles with menu is it OK if I message you?

thanks again
Scott

here's a modified version of your code that formats all the prints into a string which is then passed to a new function, disp(), for output on both the serial monitor as well as the LCD, i added what i believe on the lcd commands in comments.

// a programmable robot controller
//     program sequence of commands thru keypad and
//     execute sequence

const byte PinLeftDir      = 10;          //motor 1 pin 1
const byte PinLeftSpd      = 11;          //motor 2 pin 1

const byte PinRightDir     = 13;          //motor 1 pin 2
const byte PinRightSpd     = 12;          //motor 2 pin 2

const int Ncmd = 20;
int       vals [Ncmd] = {};
char      cmds [Ncmd];
int       cmdIdx;

enum { Forward, Reverse, Left, Right, Stop };

const int SpdDefault = 200;

char s [80];
char t [80];

// -----------------------------------------------------------------------------
void setup (void)
{
    Serial.begin(9600);

    pinMode (PinLeftDir,  OUTPUT);
    pinMode (PinLeftSpd,  OUTPUT);

    pinMode (PinRightDir, OUTPUT);
    pinMode (PinRightSpd, OUTPUT);
}
// -----------------------------------------------------------------------------
void loop (void)
{
    processKey (waitForKey ());                 //program starts here calls processKey and i assume waitForKey function
}

// -----------------------------------------------------------------------------


void processKey (char key)                          //gets key entered from char waitForKey function
{
    if ('0' <= key && key <= '9')  {                //ensure button pressed is a number
        vals [cmdIdx] = 10 * vals[cmdIdx] + key - '0';  //gets value for time to run motor and what direction
    }
    else if ('*' == key) {                            //if * key is pressed  
        if (0 < cmdIdx)                               // and cmfIdx is less than zero
            cmdIdx--;                                 // subtract 1 from cmdIdx (i assume to keep 0 from being first number)
    }
    else if ('#' == key) {                            // if # key pressed call execCmds funciont
        execCmds ();

    }
    else  {
        if (Ncmd > cmdIdx)  {                         // if cmdIdx is less than 20
            cmds [cmdIdx  ] = key;                    // put key entered into cmdIdx position of cmds

            sprintf (s, "processKey: %6d %c", vals [cmdIdx], cmds [cmdIdx]);
            disp (s);
            cmdIdx++;                                 // increment cmdIdx by 1
        }
        else
            Serial.println ("processKey: cmds size exceeded");  // error catcher
    }
}

// -----------------------------------------------------------------------------
// simulate keypad function
char waitForKey (void)              // gets input from user and returns it to processKey
{
    while (! Serial.available())
        ;
    return Serial.read ();
}
// -----------------------------------------------------------------------------
void execCmds (void)                  //execute the commands
{
    for (int i = 0; i < cmdIdx; i++)  {       //set i to zero and ifi is less than cmdIdx index i by 1 
        switch (cmds [i]) {                   //check cmds index i for direction to travel
        case 'B':       // backwards
            setSpeed (Reverse, vals [i]);
            break;

        case 'F':       // forward
                    setSpeed (Forward, vals [i]);
            break;

        case 'L':       // left turn
            setSpeed (Left, vals [i]);
            break;

        case 'R':       // right turn
            setSpeed (Right, vals [i]);
            break;

        case 'S':       // stop
            setSpeed (Stop, 0);
            break;
        }

        vals [i] = 0;             //sets vals index i to zero
    }
    cmdIdx = 0;                   //sets cmdIdx to zero
}
// -----------------------------------------------------------------------------
// set left & right motor to specified speed, neg mean reverse
void setSpeed (int dir, int msec)
{

    switch (dir) {
    case Forward:                               //forward
        digitalWrite (PinLeftDir,  LOW);        // motor1pin1 = 10
        digitalWrite (PinRightDir, HIGH);        // motor1pin2 = 13
        digitalWrite (PinLeftSpd,  HIGH);       // motor2pin1 = 11
        digitalWrite (PinRightSpd, LOW);       // motor2pin2 = 12
        
       // analogWrite  (PinLeftSpd,  SpdDefault);
       // analogWrite  (PinRightSpd, SpdDefault);

        sprintf (s, "setSpeed: %6d forward", msec);
        disp (s);

        delay (msec);
        digitalWrite (PinLeftDir,  LOW);        
        digitalWrite (PinRightDir, LOW);
        digitalWrite (PinLeftSpd,  LOW);        
        digitalWrite (PinRightSpd, LOW); 
        
        break;

    case Reverse:
        digitalWrite (PinLeftDir,  HIGH);        // motor1pin1 = 10
        digitalWrite (PinRightDir, LOW);        // motor1pin2 = 13
        digitalWrite (PinLeftSpd,  LOW);       // motor2pin1 = 11
        digitalWrite (PinRightSpd, HIGH);       // motor2pin2 = 12

        sprintf (s, "setSpeed: %6d reverse", msec);
        disp (s);

        delay (msec);
        digitalWrite (PinLeftDir,  LOW);        
        digitalWrite (PinRightDir, LOW);
        digitalWrite (PinLeftSpd,  LOW);        
        digitalWrite (PinRightSpd, LOW);
        break;

    case Left:
        digitalWrite (PinLeftDir,  HIGH);        // motor1pin1 = 10
        digitalWrite (PinRightDir, LOW);        // motor1pin2 = 13
        digitalWrite (PinLeftSpd,  HIGH);       // motor2pin1 = 11
        digitalWrite (PinRightSpd, LOW);       // motor2pin2 = 12

        sprintf (s, "setSpeed: %6d left-turn", msec);
        disp (s);

        delay (msec);
        digitalWrite (PinLeftDir,  LOW);        
        digitalWrite (PinRightDir, LOW);
        digitalWrite (PinLeftSpd,  LOW);        
        digitalWrite (PinRightSpd, LOW);
        break;

    case Right:
        digitalWrite (PinLeftDir,  LOW);        // motor1pin1 = 10
        digitalWrite (PinRightDir, HIGH);        // motor1pin2 = 13
        digitalWrite (PinLeftSpd,  LOW);       // motor2pin1 = 11
        digitalWrite (PinRightSpd, HIGH);       // motor2pin2 = 12

        sprintf (s, "setSpeed: %6d right-turn", msec);
        disp (s);

        delay (msec);
        digitalWrite (PinLeftDir,  LOW);        
        digitalWrite (PinRightDir, LOW);
        digitalWrite (PinLeftSpd,  LOW);        
        digitalWrite (PinRightSpd, LOW);
        break;

    case Stop:
        digitalWrite (PinLeftDir,  LOW);        
        digitalWrite (PinRightDir, LOW);
        digitalWrite (PinLeftSpd,  LOW);        
        digitalWrite (PinRightSpd, LOW);

        sprintf (s, "setSpeed: stop");
        disp (s);

        break;
    }
}

void disp (char *s)
{
    Serial.println (s);
    // lcd.clear ();
    // lcd.print (s);
}

i'm a retired firmware developer.

you should post your questions on this thread. but feel free.
i help out with the high school robotics team and am curious how you make out.

How fun, especially if you get kids that are REALLY interested. I was working on merging my code into what we have but will look at yours first (probably way more streamline). I will post any questions i run into here and will update you when I've got more done.

thanks again
Scott

@gcjr I'm having a little trouble determining just where to put my LCD Menu. Should I put it in the void loop before the call to processKey or somewhere in the waitForKey function? I want to do it myself, but could you point me in the right direction?

So, why don't you?
Experiment.
It only costs your time.

i don't understand why you need what you call a "menu". guessing you have some other modes in mind.

at least for now, why not just enter keypresses as i've done it. there should only be one place where the code waits for a keypress. it's a sequence of cmds, one or more #s followed by a cmd letter

I guess I should have explained more. If I was the only one operating it then it would be fine, but I have grandsons that might like to play with it if I do get it finished and therefore I will need a user interface that is simplified.

Make it work first

Point taken. :smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.