Is There a Code To Read the State of GPIO Pins?

Hi,

I am looking for a way to read GPIO pins, similar to M119 but allowing to specify the pin I wish to read.

I am using an Arduino Mega2560.

Thank you.

Comments

  • There is only M42 to set a pin. If you need reading you can add a M code easily in commands.cpp that writes the read result.
  • Thank you for the quick reply. That sounds like it would require me to understand your C++ code and write new code, is that correct? It looks very complex.
  • It's not really complex for a programmer. If you aren't one it is in deed not as easy.
    It is more or less just adding a "case 456:"..."break;" to the M code list in commands.cpp and add what you want to do. You find lots of samples, e.g. READ(77); reads pin 77 state. Of cours eit must be f´defined as input before.

    What do you want to achieve with this?
  • Repetier said:
    It's not really complex for a programmer. If you aren't one it is in deed not as easy.
    It is more or less just adding a "case 456:"..."break;" to the M code list in commands.cpp and add what you want to do. You find lots of samples, e.g. READ(77); reads pin 77 state. Of cours eit must be f´defined as input before.

    What do you want to achieve with this?
    I am executing different GCODE programs based on four external switch inputs. I am serving the GCODE with a simple custom python server. I want to read the state of the switches using the Arduino Mega2560 as opposed to adding additional hardware to the system to read the switches.
  • And I thank you very much for your previous comment, I have studied the code in commands.cpp and I believe I understand it enough to modify it.
  • Great. Just remember to set the 4 pins as inputs in setup function.
    SET_INPUT(pinNumber);

    then you can read them with READ(pinNumber) and buid a number 0-15 from the 4 bits.
  • In order to keep the required code modifications limited to one location in the code, and since this was strictly for a temporary setup, I wrote the new case this way. A bit clumsy and redundant, but straightforward. Also, my way has the drawback of being Arduino-specific.

        case 130: // M130
            pinMode(28, INPUT_PULLUP);
            pinMode(29, INPUT_PULLUP);
            pinMode(30, INPUT_PULLUP);
            pinMode(31, INPUT_PULLUP);
            Com::writeToAll = false;
            Commands::waitUntilEndOfAllMoves();
            Com::printF(PSTR("~M130~"));
            if (READ(28) == 0) { Com::printF(PSTR("~28~")); }
            if (READ(29) == 0) { Com::printF(PSTR("~29~")); }
            if (READ(30) == 0) { Com::printF(PSTR("~30~")); }
            if (READ(31) == 0) { Com::printF(PSTR("~31~")); }        
            break;
  • Add before break a
    Com::println();

    or you get problems with host software because you are not finishing the line and that can lead to issues.

    Also Commands::waitUntilEndOfAllMoves(); means it waits for moves to stop, so you should not use it during a print.

            pinMode(28, INPUT_PULLUP);
            pinMode(29, INPUT_PULLUP);
            pinMode(30, INPUT_PULLUP);
            pinMode(31, INPUT_PULLUP);
    should be inside Printer::setup where all hardware gets initialized.
    Apart from this it would work.
  • Thank you for the additional advice, I will make those modifications.
Sign In or Register to comment.