Hello.
I put a sketch together that reads the high or low status of the pins on a Dip Switch and stores the 1s or 0s in an array. It's a mini test sketch designed to be a proof of concept before I add the code to the main sketch.
The plan is to concatenate my 1s and 0s array elements into a single binary variable. I have looked to see if there is such a function but came up with nothing.
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 array elements , which on a second look makes sense.
This is the whole sketch:
// --------------------------------------------
// 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
}
Is it possible to read all 8 array elements 0s and 1s and store them, in order, in a variable so that I then have a binary number that represents the dip switch settings?
Thanks
Regards
Graeme