Printing an "Array" of input value's

Hello!

I am currently working on a project, that translates 32 input switches (that represent a 32bit binary signal) to the hexadecimal octal and decimal system.

I am stuck on the following subject;

I can currently display the Binary signal, by just digitalread the value, which is either 1 or 0 , and then print them on the display.
But to translate the signal, i have to put all the 32 bits in an array, and then print them on the display, in HEX DEC or OCT.

My question is;

How do i put all the read value's in an array
How do i print that array on the LC-Display

NOTE: I am a newb at programming, i have done a lot of research, but could not manage to figure this one out myself. A clear newb-friendly explanation would be much appreciated

Here my current code;

// Hierbij betrek ik de LiquidCrystal library
#include <LiquidCrystal.h>

// Definïeer welke pinnen worden gebruikt door het LC-display
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// Hier definiëer ik de pin nummers als BITs (Bit1, Bit2 etc.) 

int Bit1 = 52;
int Bit2 = 50;
int Bit3 = 48;
int Bit4 = 46;
int Bit5 = 44;
int Bit6 = 42;
int Bit7 = 40;
int Bit8 = 38;
int Bit9 = 36;
int Bit10 = 34;
int Bit11 = 32;
int Bit12 = 30;
int Bit13 = 28;
int Bit14 = 26;
int Bit15 = 24;
int Bit16 = 22;
int Bit17 = 53;
int Bit18 = 51;
int Bit19 = 49;
int Bit20 = 47;
int Bit21 = 45;
int Bit22 = 43;
int Bit23 = 41;
int Bit24 = 39;
int Bit25 = 37;
int Bit26 = 35;
int Bit27 = 33;
int Bit28 = 31;
int Bit29 = 29;
int Bit30 = 27;
int Bit31 = 25;
int Bit32 = 23;

// Hier begint de SETUP routine, deze is start alle benodigde dingen op
void setup() {
  
  //Start het LC-display met de dimensies
  lcd.begin(20, 4);
  
  //INTRO
  lcd.print("Jonnos ARINC Project");
  delay(1000);
  lcd.clear();
  
  delay(500);
  lcd.print("Starting up");
 delay(250);
  lcd.print(".");
  delay(250);
  lcd.print(".");
  delay(250);
  lcd.print(".");
  delay(250);
  lcd.print(".");
  delay(250);
  lcd.print(".");
  delay(250);
  lcd.print(".");
  
  lcd.clear();
  lcd.print("Starting up") ;
  
  delay(250);
  lcd.print(".");
  delay(250);
  lcd.print(".");
  delay(250);
  lcd.print(".");
  delay(250);
  lcd.print(".");
  delay(250);
  lcd.print(".");
  delay(250);
  lcd.print(".");
  
  delay(1000);
  lcd.clear(); 
  delay(1000);
  lcd.setCursor(7,0);
  lcd.print("BINARY");  
  delay(1000);
  
  // Hier definïeer ik alle BIT nummers als INPUTS
  pinMode(Bit1, INPUT);
  pinMode(Bit2, INPUT);
  pinMode(Bit3, INPUT);
  pinMode(Bit4, INPUT);
  pinMode(Bit5, INPUT);
  pinMode(Bit6, INPUT);
  pinMode(Bit7, INPUT);
  pinMode(Bit8, INPUT);
  pinMode(Bit9, INPUT);
  pinMode(Bit10, INPUT);
  pinMode(Bit11, INPUT);
  pinMode(Bit12, INPUT);
  pinMode(Bit13, INPUT);
  pinMode(Bit14, INPUT);
  pinMode(Bit15, INPUT);
  pinMode(Bit16, INPUT);
  pinMode(Bit17, INPUT);
  pinMode(Bit18, INPUT);
  pinMode(Bit19, INPUT);
  pinMode(Bit20, INPUT);
  pinMode(Bit21, INPUT);
  pinMode(Bit22, INPUT);
  pinMode(Bit23, INPUT);
  pinMode(Bit24, INPUT);
  pinMode(Bit25, INPUT);
  pinMode(Bit26, INPUT);
  pinMode(Bit27, INPUT);
  pinMode(Bit28, INPUT);
  pinMode(Bit29, INPUT);
  pinMode(Bit30, INPUT);
  pinMode(Bit31, INPUT);
  pinMode(Bit32, INPUT);
}

// Gesloten 'loop'. Serie met acties die ondeindig doorgaat
void loop() {
 
  // Leest de input waarde, definiëert het als valBIT
  int valBit1 = digitalRead(Bit1);
  int valBit2 = digitalRead(Bit2);
  int valBit3 = digitalRead(Bit3);
  int valBit4 = digitalRead(Bit4);
  int valBit5 = digitalRead(Bit5);
  int valBit6 = digitalRead(Bit6);
  int valBit7 = digitalRead(Bit7);
  int valBit8 = digitalRead(Bit8);
  int valBit9 = digitalRead(Bit9);
  int valBit10 = digitalRead(Bit10);
  int valBit11 = digitalRead(Bit11);
  int valBit12 = digitalRead(Bit12);
  int valBit13 = digitalRead(Bit13);
  int valBit14 = digitalRead(Bit14);
  int valBit15 = digitalRead(Bit15);
  int valBit16 = digitalRead(Bit16);
  int valBit17 = digitalRead(Bit17);
  int valBit18 = digitalRead(Bit18);
  int valBit19 = digitalRead(Bit19);
  int valBit20 = digitalRead(Bit20);
  int valBit21 = digitalRead(Bit21);
  int valBit22 = digitalRead(Bit22);
  int valBit23 = digitalRead(Bit23);
  int valBit24 = digitalRead(Bit24);
  int valBit25 = digitalRead(Bit25);
  int valBit26 = digitalRead(Bit26);
  int valBit27 = digitalRead(Bit27);
  int valBit28 = digitalRead(Bit28);
  int valBit29 = digitalRead(Bit29);
  int valBit30 = digitalRead(Bit30);
  int valBit31 = digitalRead(Bit31);
  int valBit32 = digitalRead(Bit32);
  
  lcd.setCursor(2,2);     // Verplaats cursor naar 0, 2
  
  lcd.print(valBit1);    // Print alle Bit waardes op het LCD 
  lcd.print(valBit2);
  lcd.print(valBit3);
  lcd.print(valBit4);
  lcd.print(valBit5);
  lcd.print(valBit6);
  lcd.print(valBit7);
  lcd.print(valBit8);
  lcd.print(valBit9);
  lcd.print(valBit10);
  lcd.print(valBit11);
  lcd.print(valBit12);
  lcd.print(valBit13);
  lcd.print(valBit14);
  lcd.print(valBit15);
  lcd.print(valBit16);
  
  lcd.setCursor(2,3);
  
  lcd.print(valBit17);
  lcd.print(valBit18);
  lcd.print(valBit19);
  lcd.print(valBit20);
  lcd.print(valBit21);
  lcd.print(valBit22);
  lcd.print(valBit23);
  lcd.print(valBit24);
  lcd.print(valBit25);
  lcd.print(valBit26);
  lcd.print(valBit27);
  lcd.print(valBit28);
  lcd.print(valBit29);
  lcd.print(valBit30);
  lcd.print(valBit31);
  lcd.print(valBit32);
  
}

Boy is that code crying out for arrays to be used for all those variables with digits in their name.

But to translate the signal, i have to put all the 32 bits in an array, and then print them on the display, in HEX DEC or OCT.

So, you need an array to hold the 32 bits
byte anArray[32];That gives you 32 places in which to store a byte value. This is inefficient because you will only be storing 0 or 1 in each byte but it will get you started.

Now when you read the values from the pins you put them in the array in the correct position from 0 to 31. This would be so much easier if you used an array of pin numbers because you could use a for loop to iterate through them.

const byte bitPins[] = {52, 50, 48, 46, etc etc];

Then you could read them like this

for (int i = 0; i < 32; i++)
{
  anArray[i] = digitalRead(bitPins[i]);
}

The bitPins array could be used with a for loop to set the pinMode() of the pins in the same way in setup()

Once the values are in anArray you can output or manipulate them with a for loop.

I hope this helps

Also have a look at bitRead and bitWrite

UKHeliBob:
Boy is that code crying out for arrays to be used for all those variables with digits in their name.

Jup, it probably is. But since using arrays seems difficult, i decided to do it this way. Remember, i'm a total green when it comes to programming.

But thanks for the help so far, i will work a little with what you guys gave me, and keep the topic updated!

Instead of using an array of bytes to store the 32 bits I would use a single unsigned long int:

unsigned long int value = 0;
for (int i=0; i<32; i++)
   value |= (unsigned long)digitalRead(pins[i]) << i;

This assumes that the pin numbers are stored Least Significant Bit first.
Note: "value |= x;" is a shorthand way of saying "value = value | x;" and '|' is the bitwise OR function.

You can then print the value directly in decimal or hex:

Serial.print(value); // Decimal
Serial.print(value, HEX); // Hexadecimal

Instead of using an array of bytes to store the 32 bits I would use a single unsigned long int:

John, that is if course an excellent idea and is what I was alluding to when I said that an array of bytes was inefficient in reply #1, but as the OP is having trouble with arrays I am not sure that he is ready for bitwise operators and bit shifting just yet.