SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By rgsparber
#200777
I am using a Pro Micro to record analog voltages on 8 channels. It works fine except for one cosmetic issue. Unterminated inputs float and random values are recorded. I decided to turn on the pull-up resistors to get a value near 5V. Two of my ports sit at or very near 5V but the rest still float. Using a DVM, I confirmed that the pull-ups are not there. Then I changed out the Pro Micro and the same two ports are pulled to near 5V while the rest continue to float. Yet I can apply known voltages to all ports and they are recorded properly.

typical code:
pinMode(Port0, INPUT);
pinMode(Port0, INPUT_PULLUP);

Has anyone else seen this behavior?

Thanks in advance,

Rick
You do not have the required permissions to view the files attached to this post.
By rgsparber
#200795
Brilliant! That was my problem.

Is there a way to make, for example, "A8" a variable that could be used in a GPIO statement? Would I define it as a string?

I found in the Arduino Reference section one place where they say

int analogPin = 3;

and then use it to define a GPIO as an input. However, since GPIO default to inputs, they do not use this variable to set up the port. Later they say

val = analogRead(analogPin); // read the input pin

and I can see that this function should assume the "A" in front of the number.

In the second example, they do set up the port and do not use a variable to define the port number. Too bad they don't have a note explaining this trap.

Thanks for your help,

Rick
By paulvha
#200796
Good.. Now I would NOT hard code the PIN instead of the Ax... let the IDE handle that complexity and translation both during compile and execution. Using a string does not help, as part of the translation is happening during compile. Here is how I would do it ( I have only included the relevant parts of the code and NOT tested, but it did compile)

regards,
Paul

Code: Select all
#define Port0 8
#define Port1 7
#define Port2 10
#define Port3 0
#define Port4 1
#define Port5 2
#define Port6 3
#define Port7 9


void setup() {
  // put your setup code here, to run once:
  
  //comment out these lines if you do not want unterminated ports reading 5V
#ifndef SoftwireActive //if Softwire not enabled  
  pullupmode(Port0);
  pullupmode(Port1);
#endif  
  pullupmode(Port2);
  pullupmode(Port3);
  pullupmode(Port4);
  pullupmode(Port5);
  pullupmode(Port6);
  pullupmode(Port7);
  //
}

void loop() {
  // put your main code here, to run repeatedly:
}

void pullupmode(int port)
{
  switch(port)
  {
    case Port0:
      pinMode(A8, INPUT_PULLUP);
      break;
    case Port1:
      pinMode(A7, INPUT_PULLUP);
      break;
    case Port2:
      pinMode(A10, INPUT_PULLUP);
      break;
    case Port3:
      pinMode(A0, INPUT_PULLUP);
      break;
    case Port4:
      pinMode(A1, INPUT_PULLUP);
      break;
    case Port5:
      pinMode(A2, INPUT_PULLUP);
      break;
    case Port6:
      pinMode(A3, INPUT_PULLUP);
      break; 
    case Port7:
      pinMode(A9, INPUT_PULLUP);
      break;  
  }
}