Digital Input producing non 0/1

PCB has set of 7 switches connected to IO
Printing them out serially for debugging
One of them has a IO show up that is a 3 now and then
The others, show up as 1 as switch depressed

ITs got an ADC on that pin, how to i shut off?

// E21-003 Collimator Recognition
//Microcontroller being used is Leonardo variant
//polulu astar 3101       https://www.pololu.com/product/3101
//It is reading the configuration of switches off the PCB E33-031 via the E11-033 - Collimator Recognition Cable
//There are 7 switches on the collimator recognition PCB
//The mapping of hte Collimator recogntion Pins, through cable, to the Leo PCB is as follows
// Coll_PCB_SW1->  Cable#1 ->  Leo#0
// Coll_PCB_SW2 -> Cable#3 ->  Leo#1
// Coll_PCB_SW3 -> Cable#5 ->  Leo#4
// Coll_PCB_SW4 -> Cable#7 ->  Leo#7
// Coll_PCB_SW5 -> Cable#9 ->  Leo#8
// Coll_PCB_SW6 -> Cable#10 -> Leo#18  (or 19)
// Coll_PCB_SW7 -> Cable#8 ->  Leo#19 (or 18)

/***Definitions***/
#define Pin0 0
#define Pin1 1
#define Pin4 4
#define Pin7 7
#define Pin8 8
#define Pin18 18   
#define Pin19 19

//***Constants***//
byte SwitchArray[7] = {0, 0, 0, 0, 0, 0, 0};
 int arraySize = sizeof(SwitchArray) / sizeof(SwitchArray[0]);

void setup()
{
  Serial.begin(9600);
  pinMode(Pin0,INPUT);
  pinMode(Pin1,INPUT);
  pinMode(Pin4,INPUT);
  pinMode(Pin7,INPUT);
  pinMode(Pin8,INPUT);
  pinMode(Pin18,INPUT);
  pinMode(Pin19,INPUT);
}

void loop()
{
  delay(400);
  SwitchArray[1] = digitalRead(Pin0);
  SwitchArray[2] = digitalRead(Pin1);
  SwitchArray[3] = digitalRead(Pin4);
  SwitchArray[4] = digitalRead(Pin7);
  SwitchArray[5] = digitalRead(Pin8);
  SwitchArray[6] = digitalRead(Pin19);
  SwitchArray[7] = digitalRead(Pin18);

//print the array out, has to be done each element at a time
//then do a println to start a new line to repeat it
  for (int i = 1; i < arraySize+1; i++)
   {
    Serial.print(SwitchArray[i]); // Print the variable and move to the next
   }
    Serial.println(); // create a new line
}

type or paste code here

The bounds of your array are [0..6], not [1..7].

byte SwitchArray[7] = {0, 0, 0, 0, 0, 0, 0};
SwitchArray[7] = digitalRead(Pin18);

SwitchArray[7] is outside of the array. The index to the array is from 0 to 6

A little simpler:

// E21-003 Collimator Recognition
//Microcontroller being used is Leonardo variant
//polulu astar 3101       https://www.pololu.com/product/3101
//It is reading the configuration of switches off the PCB E33-031 via the E11-033 - Collimator Recognition Cable
//There are 7 switches on the collimator recognition PCB
//The mapping of hte Collimator recogntion Pins, through cable, to the Leo PCB is as follows
// Coll_PCB_SW1->  Cable#1 ->  Leo#0
// Coll_PCB_SW2 -> Cable#3 ->  Leo#1
// Coll_PCB_SW3 -> Cable#5 ->  Leo#4
// Coll_PCB_SW4 -> Cable#7 ->  Leo#7
// Coll_PCB_SW5 -> Cable#9 ->  Leo#8
// Coll_PCB_SW6 -> Cable#10 -> Leo#18  (or 19)
// Coll_PCB_SW7 -> Cable#8 ->  Leo#19 (or 18)

//***Constants***//
const byte arraySize = 7;
const byte SwitchArray[arraySize] = {0, 1, 4, 7, 8, 18, 19};

void setup()
{
  Serial.begin(9600);
  //Not necessary to set pins to INPUT because that is the default mode
}

void loop()
{
  delay(400);

  //print the array out, has to be done each element at a time
  //then do a println to start a new line to repeat it
  for (int i = 0; i < arraySize; i++)
  {
    Serial.print(digitalRead(SwitchArray[i])); // Print the variable and move to the next
  }
  Serial.println(); // create a new line
}

:smiley: :smiley: A million years ago I had a boss that had a saying: "Stranger than truth". :smiley: :smiley:

thanks all

yes, was out of bounds on the array
very strange indeed, producing numbers that were non 0/1when reading outside the array

Not strange at all because you don't know what the memory location is used for in the sketch

For the benefit of anyone finding this topic in the future please post your revised, working sketch in full

/***Definitions***/
#define Pin0 0
#define Pin1 1
#define Pin4 4
#define Pin7 7
#define Pin8 8
#define Pin18 18   
#define Pin19 19

//***Constants***//
int collimator = 0;   //initialize to nonvalid number
int valid_filter = 0; //initialize
byte SwitchArray[7] = {0, 0, 0, 0, 0, 0, 0};  //bounds starts at 0
 int arraySize = sizeof(SwitchArray) / sizeof(SwitchArray[0]);

void setup()
{
  Serial.begin(9600);
  pinMode(Pin0,INPUT);
  pinMode(Pin1,INPUT);
  pinMode(Pin4,INPUT);
  pinMode(Pin7,INPUT);
  pinMode(Pin8,INPUT);
  pinMode(Pin18,INPUT);
  pinMode(Pin19,INPUT);
}

void loop()
{
  delay(400);
  SwitchArray[0] = digitalRead(Pin0);
  SwitchArray[1] = digitalRead(Pin1);
  SwitchArray[2] = digitalRead(Pin4);
  SwitchArray[3] = digitalRead(Pin7);
  SwitchArray[4] = digitalRead(Pin8);
  SwitchArray[5] = digitalRead(Pin19);
  SwitchArray[6] = digitalRead(Pin18);

//print the array out, has to be done each element at a time
//then do a println to start a new line to repeat it
  for (int i = 0; i < arraySize; i++)
   {
    Serial.print(SwitchArray[i]); // Print the variable and move to the next
   }
    Serial.println(); // create a new line
 }

Let's see what's in memory:

> avr-nm -CSn sketch.ino.elf
...
00800180 00000007 b SwitchArray
00800187 00000001 b timer0_fract
00800188 00000004 b timer0_millis
0080018c 00000004 b timer0_overflow_count

So SwitchArray[7] is timer0_fract; writing to it messes slightly with millis() and reading it gives varying results.