Concatenate Array Elements into a Single Variable

User avatar
Graeme
Cosmic Lounger
Posts: 1220
Joined: 11 Feb 2010, 12:23
Location: Medway, Kent, UK

Concatenate Array Elements into a Single Variable

Post by Graeme »

Morning all.

I put an Arduino (C++) programme together that reads the high or low status of the pins on a Dip Switch and stores the 1s or 0s in an array.

The plan is to concatenate the array elements into a single binary variable.

After searching I tried:


void loop() {

for (int i = 0; i < PinCount; i++) {
DipSwitchValues = digitalRead(ArduinoPins);
}

DipSwitchRead = 0;

for (int i = 0; i < PinCount; i++){
DipSwitchRead = DipSwitchRead + DipSwitchValues;
}
}


This just adds up the numbers of array elements that are set to 1.

Is it possible to read all 8 array elements 0s and 1s and store them, in order, in a variable?

Thanks

Regards

Graeme

Code: Select all

//  --------------------------------------------
//  Avery Way Observatory
//  Rain Sensor Safety Monitor V7.0
//  Dip Switch Sensitivity Code Testing - sketch_apr14b
//  15/04/23
//  Graeme Durden FRAS
//  --------------------------------------------

int ArduinoPins[] = {                                 //  Array of Arduino Pin Numbers 
  4, 5, 6, 7, 8, 9, 10, 11
};   
int DipSwitchPins[] = {                               //  Array of Dip Switch Pin Numbers 
  1, 2, 3, 4, 5, 6, 7, 8
};   
int DipSwitchValues[] = {                             //  Array of Dip Switch Pin Values 
  0, 0, 0, 0, 0, 0, 0, 0
};    
int PinCount = 8;                                     //  Array Length
int Sensitivity = 0;                                  //  Sensitivity Value based on Dip Switch Settings
int DipSwitchRead = 0;                                //  Dip Switch Values Concatenated
int DelayTimer = 3000;                                //  Void Loop Delay  


void setup() {

  Serial.begin(9600);                                 //  Initialize the Serial Port

  for (int i = 0; i < PinCount; i++) {                //  Pin Mode for Loop
    pinMode(ArduinoPins[i], INPUT_PULLUP);            //  Set Arduino Pins to Input Pullup
  }
}


void loop() {

  for (int i = 0; i < PinCount; i++) {
    DipSwitchValues[i] = digitalRead(ArduinoPins[i]); //  Read Pin Values
  }
  DipSwitchRead = 0;
  //  Sensitivity = ???
  for (int i = 0; i < PinCount; i++){
    DipSwitchRead = DipSwitchRead + DipSwitchValues[i];
  }
  
  for (int i = 0; i < PinCount; i++) {                //  Print DipSwitch Values to the Serial Port
    Serial.print("DipSwitchValue ");
    Serial.print(DipSwitchPins[i]);
    Serial.print(" = ");
    Serial.println(DipSwitchValues[i]);
    Serial.print(" DipSwitchRead ");
    Serial.println(DipSwitchRead);
  }
  
  Serial.println("");

  delay(DelayTimer);                                  //  Void Loop Delay  
}

_______________________________________

http://www.averywayobservatory.co.uk/

User avatar
Graeme
Cosmic Lounger
Posts: 1220
Joined: 11 Feb 2010, 12:23
Location: Medway, Kent, UK

Re: Concatenate Array Elements into a Single Variable

Post by Graeme »

I found an answer!

x = (digitalRead (4))+(digitalRead (5)*2)+(digitalRead (6)*4)+(digitalRead (7)*8)+etc.

It reads the least significant bit and then adds the next bit * 2, then adds the next bit * 4, then adds the next bit * 8, then adds the next bit * 16, etc. So I end up with a decimal figure for my binary output. Didn't need the array after all. I'm sure there's a less cumbersome way of doing it but it does the job, so I'm happy!

Graeme
_______________________________________

http://www.averywayobservatory.co.uk/

User avatar
HansV
Administrator
Posts: 78419
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Concatenate Array Elements into a Single Variable

Post by HansV »

Glad you found a solution yourself!
Best wishes,
Hans