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
}