Serial Print All Values

Is there any way to print to serial all values of all variables (inputs and outputs) with one function? I'm using a Mega2560 for a school project to control an automated guided vehicle. The program is done and working fine, but part of the assignment is to have a troubleshoot option where all outputs and inputs can be seen on the computer screen through the serial print. Is there any way to do this without having to go through each line doing serial.print on each line? Also when I try and do serial.print for an output, it displays the pin number instead of the output. How can I change this?

UCDAGV: Also when I try and do serial.print for an output, it displays the pin number instead of the output. How can I change this?

As far as this goes, you have to do a read of the pin, then print it using the variable name you assign to the read. Or print the variable name you're using in the program. If you're trying to print the name of the pin itself, then all you're doing is showing the value you assigned to it at the beginning of your program; i.e., the pin number.

troubleshoot option where all outputs and inputs can be seen on the computer screen through the serial print.

To print a pin value you would have to do something like

Serial.println (digitalRead(pin), BYTE);

and do that for every pin you use.

A neater way to do it for a lot of pins is

byte pins_used [] = {2, 4, 5, 6, 9, 12};

for (int i = 0; i < sizeof (pins_used); i++) {
   Serial.println (digitalRead(pins_used[i]), BYTE);
}

If you need to print variables as well then there’s really nothing for it bit to do a 100 print()s, although you could set up an array of structs with variable names and addresses.


Rob

An even neater way:

for (int i = 0; i < sizeof (pins_used); i++)
{
   Serial.print("Pin #:");
   Serial.print(pins_used[i], DEC);
   Serial.print(" = ");
   Serial.println (digitalRead(pins_used[i]), BYTE);
}

Nothing worse than a bunch of numbers scrolling by with no clue what they mean.