Unexpected results from Analog Inputs

Hi All,
I'm a new member and also new to Arduino, so apologies well in advance of my ignorance.

I'm currently in the middle of a new project and had run out of digital pins, so after reading about digitalRead from analog pins, I went ahead and used them.
In my project, I have five pushbuttons (wired as pullups with 10k resistor).
Despite using a loop to read all the inputs, pin 15 (A1) always returns low state, whilst pin 14 returns a low state on startup, then settles to a high state.
All but pin 15 were returning valid states when buttons were pushed (low).
So, just in case I had a breadboard (or brain) failure, I reconfigured these pins to use input pullups and did not connect any externals.
Same problem with pin 15, in that it remains in a low state.

As my code works perfectly well with every other pin, could this be a hardware fault and if so, is there any way to confirm this?

We need to see a schematic of your project.

We need to see a good image of the actual wiring.

We need to see the complete sketch, use the </> button in the posting menu to attach the sketch.
Use CTRL T or CMD T to format your sketch before uploading to the thread.

Ok, as requested, here is the sketch. For the purposes of testing this sketch, no external wiring is needed.

//#include <LiquidCrystal.h>
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int intTData[8];//Holds state of all input triggers
int intECount[3];// Holds number of pump events
int intAlarm[5];// Holds alarm state
unsigned long ulTimes[5];// Holds duration of pump events. (1)P1 Duration, (2)P2 Duration, (3)P1 Elapsed, (4)P2 Elapsed

void setup() {

  //lcd.begin(16, 2);// set up the LCD's number of columns and rows:
  for (byte L = 6; L <= 10; L++) {
    pinMode(L, OUTPUT); // set up output pins.(6)P1 Trig,(7)P2 Trig,(8)Buzzer,(9)Backlight,(10)Contrast
  //the following input pullup settings are for testing only.
  // hardware project will use 10k pullup resistors
  pinMode(A0, INPUT_PULLUP);
  pinMode(A1, INPUT_PULLUP);
  pinMode(A2, INPUT_PULLUP);
  pinMode(A3, INPUT_PULLUP);
  pinMode(A4, INPUT_PULLUP);
  pinMode(A5, INPUT_PULLUP);   
  analogWrite(9, 255); // Backlight 0 to 255
  analogWrite(10, 10); //Contrast 10 to 70

void loop() {
  // Reads analog inputs (16 to 21) into intTData[L]
  // 14 = Pump 1 Float, 15 = Pump 2 Float
  // 16 = Pump 1 Manual, 17 = Pump 2 Manual
  // 18 = Mode, 19 = Instrument Trigger
  for (byte L = 14; L <= 19; L++) {
    intTData[L - 12] = digitalRead(L);
  // End of input read
  // Run Pump Activity calculations

}// end void loop
void PumpEvents() { // Checks for pump activity and records data.
  if (intTData[2] == 1 && intTData[0] == 1) {
    return; // No P1 Actions
  if (intTData[3] == 1 && intTData[1] == 1) {
    return; // No P2 Actions
  for (byte L = 2; L <= 3; ++L) {
    if (intTData[L] == 0 && intTData[L + 2] == 1) { //Pump automatic run (not triggerd by manual button)
      if (intTData[L - 2] != intTData[L]) { //NEW activity
        intECount[L - 1] = ++intECount[L - 1]; // Increments event counter
        intTData[L - 2] = intTData[L]; // sets new 'last' state
        ulTimes[L - 1] = millis(); // starts timer
        intAlarm[L - 1] = 1;
    if (intTData[L] != intTData[L - 2]) { //End of automatic run
      ulTimes[L + 1] = ulTimes[L + 1] + (millis() - ulTimes[L - 1]); // Updates total run time
      intTData[L - 2] = intTData[L]; // resets last state
      intAlarm[L - 1] = 0;
void Alarm() {
  unsigned long ulATime[4];
  if (intAlarm[1] == 1 && intAlarm[3] == 0) { //New P1 alarm
    ulATime[0] = 100;// beep duration
    ulATime[1] = 300;// beep spacing
    ulATime[2] = 1000;// silence at end
    for (byte L = 0; L <= 3; ++L) {
      ulATime[3] = millis();// start timer
      while (millis() < (ulATime[3] + ulATime[0])) {
        digitalWrite(8, HIGH);
      ulATime[3] = millis();// start timer
      while (millis() < (ulATime[3] + ulATime[1])) {
        digitalWrite(8, LOW);
    ulATime[3] = millis();// start timer
    while (millis() < (ulATime[3] + ulATime[2])) {
      digitalWrite(8, LOW);
void SerialPrint(int D) {
  if (D = 1) {
    Serial.println("Input Data");
    for (byte L = 0; L <= 8; L++) {
      Serial.print(intTData[L]);Serial.print(", ");
    Serial.println("End of Input Data");


This is a mockup i've done in Tinkercad.
To avoid any head scratching, the project is a two channel bilge pump monitor and alarm system for my yacht. In this image, the bulbs represent the bilge pumps, the LED represents the audio alarm and the two slide switches represent the 12v trigger from the float switches.
At this stage, coding only covers the analog input pins.

This is the way you should be wiring the 4N35 to the Arduino:

If you use the internal pullup resistor in the Arduino, the external 10k collector resistor is not needed.

please post a real schematic, at least hand drawn.

Yes, understood. As I posted initially, the original used external resistors, but when that did not work, I used pullups and NO external wiring, no switches......nothing. This (if i'm correct), should have led to all the inputs being high. As per my original, A1 stayed low and A0 was low for a second, then went and stayed high.

Sorry, but don't have one at this stage.

I appreciate your thoughts, but in this instance, the results are anomalous with NO external connections to any pin.

It sounds like a hardware issue, but I suggest that you check it with a simpler sketch that just reads the six pins and prints them.

 if (D = 1) {


If you had only the pull ups turn on and no external wiring, the Arduino is bad.

The opto wiring is still incorrect as you show in the diagram.

The compiler has some warnings for you:

In function 'void PumpEvents()':
69:26: warning: operation on 'intECount[(((int)L) - 1)]' may be undefined [-Wsequence-point]
         intECount[L - 1] = ++intECount[L - 1]; // Increments event counter

In function 'void SerialPrint(int)':
118:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if (D = 1)

123:19: warning: iteration 8 invokes undefined behavior [-Waggressive-loop-optimizations]
       Serial.print(intTData[L]); Serial.print(", ");
121:24: note: within this loop
     for (byte L = 0; L <= 8; L++)

In function 'main':
123:19: warning: iteration 8 invokes undefined behavior [-Waggressive-loop-optimizations]
       Serial.print(intTData[L]); Serial.print(", ");
121:24: note: within this loop
     for (byte L = 0; L <= 8; L++)

Those last two mean you are going off the end of an array! An 8-element array has elements 0 through 7. You are trying to use element 8.

That first one is a grave mistake:
intECount[L - 1] = ++intECount[L - 1]; //
++intECount[L - 1]; //
intECount[L - 1] += 1; //

The results of "x = x++;" are not defined and could result in any behavior. At one point the Arduino UNO compiler changed from doing the increment to NOT doing the increment and a few old sketches with that mistake suddenly stopped working.

Thanks for that John.
This might be a rather silly question, but how do I do what you've done here. Is this a capability of the arduino IDE?

Thanks for the reply. I've tried a few other things, but it does seem to be a hardware fault.
As for the opto wiring, I'll get that changed before I commit to hardware.

Not sure what you're referring to. If it's the warnings, the functionality is in the IDE.

File -> Preferences, set compiler warnings to ALL.

Update............that has me head scratching even more.
In order to confirm the hardware fault, I tried changing to analogRead and all pins returned 1023, as they should.
I then tried duplicating the read loop, using digitalRead again (so that I have the analog results, then the digital results..............and all pins now return HIGH.
Only other thing I did was to tidy up the code as suggested by Johnwasser (thanks again).
So, unless overrunning the array was causing the problem, I'm still very confused.