How to call the Ax states in a loop?

I have the folllowing piece of code:

 digitalWrite(A1, HIGH); //for debug info and learning process
 digitalWrite(A2, LOW); //for debug info and learning process
  digitalWrite(A3, LOW); //for debug info and learning process
  for (int i=0 ; i<3 ; i++ ) {
    lcd.setCursor ((4+3*i),2);
 
    lcd.print(digitalRead((15+i)));
    //lcd.print(HIGH);
    
  }

I would need to print the state of A1 then A2 then A3. I try to call them as input 15 16 17 (I believe it works that way).
I read 1 1 1 instead of 1 0 0
I tried lcd.print(HIGH); returns 1 and lcd.print(LOW); returns 0 as expected.

I obviously miss something here.
Any clue?

You're writing to a pin, then reading from that pin a little while later. What does this achieve?

Please use code tags.

Ax are set on another part of the program, I am currently trying to understand how to print properly these value. As I am currently failing there is no point in removing the first three lines. I need to be sure of my inputs.

Post your code, I have no idea what you're going on about.

I am afraid it will be more confusing:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

#define BACKLIGHT_PIN     13

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//LiquidCrystal_I2C lcd(0x38, BACKLIGHT_PIN, POSITIVE);  // Set the LCD I2C address

String stringOne, stringTwo, stringThree;

// Creat a set of new characters
const uint8_t charBitmap[][8] = {
  { 
    0xc, 0x12, 0x12, 0xc, 0, 0, 0, 0   }
  ,
  { 
    0x6, 0x9, 0x9, 0x6, 0, 0, 0, 0   }
  ,
  { 
    0x0, 0x6, 0x9, 0x9, 0x6, 0, 0, 0x0   }
  ,
  { 
    0x0, 0xc, 0x12, 0x12, 0xc, 0, 0, 0x0   }
  ,
  { 
    0x0, 0x0, 0xc, 0x12, 0x12, 0xc, 0, 0x0   }
  ,
  { 
    0x0, 0x0, 0x6, 0x9, 0x9, 0x6, 0, 0x0   }
  ,
  { 
    0x0, 0x0, 0x0, 0x6, 0x9, 0x9, 0x6, 0x0   }
  ,
  { 
    0x0, 0x0, 0x0, 0xc, 0x12, 0x12, 0xc, 0x0   }

};

#include "SevSeg.h"

//Create an instance of the object.
SevSeg sevseg;

//Create global variables
unsigned long timer;
int CentSec=0;

int digit=1;
  
  unsigned long timeupstart=0;
  unsigned long timeup=0;
  unsigned long burstup=0;
  float leakrate=100;
  int j=1;
  
void setup() {
  
    int charBitmapSize = (sizeof(charBitmap ) / sizeof (charBitmap[0]));

  // Switch on the backlight
  pinMode ( BACKLIGHT_PIN, OUTPUT );
  digitalWrite ( BACKLIGHT_PIN, HIGH );

stringOne = String("Hello ");
  stringTwo = String("world!");
 // stringThree = String ();
 stringThree = stringOne + stringTwo;
  lcd.begin(16,2);               // initialize the lcd 

  for ( int i = 0; i < charBitmapSize; i++ )
  {
    lcd.createChar ( i, (uint8_t *)charBitmap[i] );
  }

  lcd.home ();                   // go home
  lcd.print(stringThree);  
  lcd.setCursor ( 0, 1 );        // go to the next line
  lcd.print ("Wow what a rockt");
  delay ( 200 );
  
  
  
  pinMode(A4, OUTPUT);
  digitalWrite(A4, LOW);
  pinMode(A5, OUTPUT);
  digitalWrite(A5, LOW);
//I am using a common cathode display 0, with the digit pins connected
//from 2-5 and the segment pins connected from 6-13
  sevseg.Begin(0,4,3,2,1,12,11,10,9,8,7,6,5); //11+10 6 5ok 7 9 8
  timer=millis();
 
  
  
}

void loop() {
  
  //Produce an output on the display
  sevseg.PrintOutput();

  //Check if 10ms has elapsed
  unsigned long mils=millis();

float refreshrate=1000;
  if (mils-timer>=refreshrate) { //was 10
    timer=mils;
  //  CentSec++; //was 0.01s counter
    
    // read the input on analog pin 0:
  int sensorValue = analogRead(A0);

  float voltage = sensorValue * 0.004888; //(5.0 / 1023.0);
  float milivolt = voltage * 1000;

  
//  VOUT  = VS (P x 0.009 + 0.04)-0.4 
    float pressure = (voltage * 24 )+ 10+ (4.066 * (-1.78)); //typical 100kpa -1.78=22.22-24

  float negative = (pressure * 10)-1000;
  
  //negative = 81.;
  sevseg.NewNum((negative),(byte) 2);
  
  //negative = 81;
    leakrate = ((1000 * timeup) / timer);
    sevseg.NewNum((negative),(byte) 4);
      
  lcd.clear ();
  lcd.home ();                   // go home
  //negative=-99.5; //test value
  if (negative >= 99.5) {
  lcd.setCursor ( 1, 0 );
  lcd.print(negative,0); 
  }
  
  else {
  if (negative>= 9.5) {
    
    lcd.setCursor (2, 0);
    lcd.print(negative,0); 
  }
 
  else {
    if (negative>=0) {
 
    lcd.setCursor (3, 0 );
  lcd.print(negative,0);
}
else {
  if( negative>-9.5){
 lcd.setCursor (2, 0);
    lcd.print(negative,0);
  }
  else {
   if( negative>-99.5){
 lcd.setCursor (1, 0);
    lcd.print(negative,0);
   }
   else {
 lcd.setCursor (0, 0);
    lcd.print(negative,0);     
   }
  }
}
  } 
  }
  digitalWrite(A1, HIGH); //debug only
  digitalWrite(A2, LOW); //debug only
  digitalWrite(A3, LOW); //debug only
  for (int i=0 ; i<4 ; i++ ) {
    lcd.setCursor ((4+3*i),2);
 
    lcd.print(digitalRead((15+i)));
    //lcd.print(HIGH);
    
  }
  
  lcd.setCursor ( 8,1);
  //lcd.print(digit,1);  
  lcd.setCursor ( 8, 0 ); 
 lcd.print(milivolt,0); 
 
  lcd.setCursor ( 0, 1 );        // go to the next line
  lcd.print (leakrate,0);
  
  
 // sevseg.NewNum((negative),(byte) 4);
 
        if (pressure>52) { //30kpa depression -0.3bar
        digitalWrite(A1, HIGH);
        digitalWrite(A2, HIGH);
        digitalWrite(A3, HIGH);
        timeup=timeup+refreshrate;
        burstup=burstup+refreshrate;
        }
        else {
            if (pressure>=47) { //hysterisis
              if (j>0) {
                timeup=0;
                
                j=0;
                  }
                  digitalWrite(A1, LOW);
                  digitalWrite(A2, LOW);
                  digitalWrite(A3, LOW);
                  burstup=0;
                  
                }
        }
  }
  
 
}

I am trying to show on a lcd a 1 for a working motor, switched by A1 A2 or A3 or a 0 if it is not working.

The main stuff consist of a vacuum sensor, 3 fridges motor units as pumps(activated through switchtail (ssr in a case)) a 2x16 lcd reading and a 4digit reading that will soon disappear (I started with that, now moving on to i2c). Currently I did not code all the features. I plan to switch between pumps and ramp up if needed or if one pump is failing. These fridge motors are not made to work 24h in a row, so alternating, and ventilating, will keep them cooler.

Have you set A1, A2 and A3 as OUTPUT with pinMode() ?

...R

loopingz:
I have the folllowing piece of code:

 digitalWrite(A1, HIGH); //for debug info and learning process

digitalWrite(A2, LOW); //for debug info and learning process
 digitalWrite(A3, LOW); //for debug info and learning process
 for (int i=0 ; i<3 ; i++ ) {
   lcd.setCursor ((4+3*i),2);

lcd.print(digitalRead((15+i)));
   //lcd.print(HIGH);
   
 }




I would need to print the state of A1 then A2 then A3. I try to call them as input 15 16 17 (I believe it works that way).
I read 1 1 1 instead of 1 0 0
I tried lcd.print(HIGH); returns 1 and lcd.print(LOW); returns 0 as expected.


I obviously miss something here.
Any clue?

Another thing to be aware of: (A1 == 15 && A2 == 16 && A3 == 17) == TRUE only for UNOs and clones (and probably leonardo, any other Arduino that only has 13 digital I/Os, or 328 based Arduino). The Mega and DUE (for example) both have physical locations for 15, 16, and 17 (as well as 14 (would be A0 on the UNO) and 18 through 53). Might want to set up an array like: byte digitalInputs[] = {A1, A2, A3}; and then cycle through the array with something like for (i=0...etc) {digitalRead(digitalInputs[ i ])}. This way the IDE takes care of assigning the correct hardware pin based on your target board.

Or… keep track of what state you want the pin levels to be. If your script is the one setting the states then it should already know.

Thanks Robin,

Adding these declaration solved the issue.

Thanks for the feedback Sembarazu, I am currently programming a UNO, but it would be cleaner doing an A array (and again going out of my comfort zone). I might also want to change to set print whenever I change a A state without doing a test. It might be smaller memory usage for the program but I kinda like the idea of testing/verifying, once in the main loop.