ARDUINO MEGA USERINTERFACE INPUT / OUTPUT ASSIGMENT

Hi people
I have a question.
I want to write a userinterface on windows where i can assigne buttons or leds to an ARDUINO MEGA. These Pins should be than controllable via serial communication.
So im setting the pins as INPUT OR OUTPUT.

Just as an example:

pin 40 Button
pin 41 LED
pin42 Button
.....

than i click on a button like "upload".

So it should be changeable via userinterface and not via the sketch that i have to write by myselfe.

Are there any code snippets that i can use? How do i have to programm the arduino to be able to set each pin individualy as input or output pin.

Hope its clear what i want :slight_smile:

my code usually starts with pcRead() that allows me to pass integers and execute single letter commands on the Arduino thru the serial interface/monitor. It changes for each program and evolved as I work on the code.

it is uncomplicated. it processes input character by character. It can capture a signed integer. It processes single letter commands which can use the preceding integer as an argument.

separate variables may be needed if multiple values are required, such as a pin # and mode or value to write. But something like pinMode could be invoked with separate command letters for INPUT, INPUT_PULLUP or OUTPUT. digitalWrite needs a pin and value. So a separate command could be to set a pin#.

this version came from code working with motors and pid

void
pcRead (void)
{
    static long  val  = 0;
    static int   sign = 1;

    if (Serial.available()) {
        int c = Serial.read ();

        switch (c)  {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            val = c - '0' + (10 * val);
            Serial.println (val);
            return;

        case ' ':
            val  = 0;
            sign = 1;
            return;

        case '-':
            sign = -1;
            return;

        case 'B':
            dumpBuf();
            break;

        case 'b':
            SetSpeed(0);
            setDir(D_SHORT);
            break;

        case 'C':
            clearBuf();
            break;

        case 'c':
            capture = val;
            val   = 0;
            break;

        case 'D':
            debug = val;
            val   = 0;
            break;

        case 'd':
            status();
            break;

        case 'K':
            pidKd = val / 100.0;
            val = 0;
            break;

        case 'M':
            maxPwm = val;
            val = 0;
            break;

        case 'm':
            mode = val;
            val = 0;
            break;

        case 'R':
            SetSpeed(H_MAX);
            break;

        case 'r':
            setDir(D_CW == hDir ? D_CCW : D_CW);
            break;

        case 'S':
            SetSpeed(H_MAX < val * sign ? H_MAX : val * sign);
            setDir(0 < val ? D_CCW : D_CW);
            mode = M_MAN;
            Serial.print   (" pcRead: hPwm ");
            Serial.println (hPwm);
            val  = 0;
            sign = 1;
            break;

        case 's':           // stop
            SetSpeed(0);
            status();
            pos    = 0;
            maxPer = minPer = 0;
            break;

        case 't':
            mode   = M_CTL;
            target = val * sign;
            val    = 0;
            sign   = 1;

            Serial.print   ("pcRead: ");
            Serial.print   (" target ");
            Serial.println (target);

            isrCnt = maxPer = minPer = 0;

       //   setDir   (target < pos ? D_CCW : D_CW);
       //   SetSpeed (200);
            break;

        case 'v':
            Serial.println (VERSION);
            break;

            break;

        case '?':
            Serial.println ("   [0-9] 10*val + digit");
            Serial.println ("   sp    val = 0");
            Serial.println ("   -     sign = -1");
            Serial.println ("   B     dumpBuf()");
            Serial.println ("   b     brake");
            Serial.println ("   C     clearBuf ()");
            Serial.println ("   c     set capture (1 ISR, 2 PID");
            Serial.println ("   D     set debug");
            Serial.println ("   d     status()");
            Serial.println ("   K     set pidKd (x100)");
            Serial.println ("   M     set maxPwm");
            Serial.println ("   m     set mode");
            Serial.println ("   P     set pos = spd = 0");
            Serial.println ("   R     run max speed");
            Serial.println ("   r     reverse");
            Serial.println ("   S     set speed/directio (+/- 0-255)");
            Serial.println ("   s     set pos = spd = 0 and status ()");
            Serial.println ("   t     set target pos and enable ctlPid");
            Serial.println ("   v     version");
            break;

        default:
            break;
        }
    }
}

b

Hi Thanks Greg for you code Part but i still do not realy see that this solves my problem...

Or do i just understand it wrong?

I mean if i switch PIN 1 in userinterface from Button to LED than it should do that on arduino also...

I can send Serial commands for example "PIN1 BUTTON"

So the arduino have to recieve that and set PIN1 as BUTTON
And if it recieves "PIN1 LED"
it should set it as OUTPUT ...

So the arduino have to recieve that and set PIN1 as BUTTON
And if it recieves "PIN1 LED"
it should set it as OUTPUT ...

doesn't it ultimately mean pinMode() needs to be executed for a specific pin to set it to INPUT, INPUT_PULLUP or OUTPUT regardless of what command you send across the serial interface?

i'm suggesting the command for configuring the pin be 'i', 'p', or 'o'. but there could be 'b' for button or 'l' for led. for example "13l" to configure pin 13 as an output (i.e. led).

but additional commands can be to read or write a specific pin. But you can have as many other commands as you would like.