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

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

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

        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);

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

        case '-':
            sign = -1;

        case 'B':

        case 'b':

        case 'C':

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

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

        case 'd':

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

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

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

        case 'R':

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

        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;

        case 's':           // stop
            pos    = 0;
            maxPer = minPer = 0;

        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);

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


        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");



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.