Help In declaring Input and Output.

hello all , I’m Ash from Malaysia. I have a Mega 2560 that i bought just a few month. Anyway i am wondering if i can do something like the program below so that the input and the output can be done so that even number can be use as an input and odd number pin be use as an output.

 void setup(){
  for (int i =22 ; i<=53 ; i++)
  {
    int result = i %2;
    if (result == 0)
    { pinMode ( i,INPUT);}
    else {pinMode (i,OUTPUT);}}}
    void loop(){}

could this work? though the compiler give no error i have no way of testing if it work or not.

ash901226:
could this work? though the compiler give no error i have no way of testing if it work or not.

I think it will work. You can hook up an LED with a current-limiting resistor and use it to check all those pins 22 thru 53, and see if odd pins will light up the LED while even pins won’t.

Like this:

 void setup(){
  for (int i =22 ; i<=53 ; i++)
  {
    int result = i %2;
    if (result == 0)
    { pinMode ( i,INPUT);}
    else {
            pinMode (i,OUTPUT);
            digitalWrite(i,HIGH);}}}
    void loop(){}

i have no way of testing if it work or not

Do you have an LED? Do you have a current limiting resistor? If yes, then you can test.

Add this to the bottom of setup

  for (int i =22 ; i<=53 ; i++)
  {
    digitalWrite( i, HIGH );
  }

On the outputs, the LEDs will be very bright. On the inputs, the LEDs will be very dim.

thank you both for your quick respond i will try it now.
i will post my result in a couple of min

well maybe i have to delay the post the result awhile. i need to go out a while but will be back soon

i am please to say that the program work and it works great

You really should look into direct port manipulation. http://www.arduino.cc/en/Reference/PortManipulation

Don

That's good. What is your program for? Why the even/odd input/output?

actually liudr i was just wondering and would like to put the input in one side of the mega I/O pin and the output on the other