I have this DIP switch connected to a pro micro. The DIP switch has 8 switches connected to VCC and ports 2-10 except 3. The problem comes when I try to read these with this code:
int pins[] = {10, 2, 4, 5, 6, 7, 8, 9};
int pinCount = 8;
String DIPRead = "";
int port = A0;
void setup() {
//Serial.println(port);
Serial.begin(9600);
while (!Serial) ;
for (int index = 0; index < pinCount; index++) {
pinMode(pins[index], INPUT);
DIPRead += String(digitalRead(pins[index]));
}
}
I keep getting faulty results. I have soldered the dip switch to the pro micro and tested if the wires hit each other but thats not the case. Does someone know if this is a code problem or a skill issue on my soldering?
Try the above as follows and check that you have consistent readings. This is to avoid floating conditions of the pins that are not activated by VCC. You need to de-solder the VCC wire and connect/solder it to GND. A closed swicth will assert logic LOW and an open swicth will assert logic HIGH.
Enable the internal pull-ups and the inputs will read high when nothing is connected, or when the switch is off (open). Turning-on (closing) the switch "overpowers" the pull-up, pulling the input down and you'll read low. (You may have to reverse the logic in your code.)
Or, you can use external pull-down resistors and wire the switch to 5V, but it's usually easier to do it in software.
Everything in my code works fine, when the switch is on it returns a 1 (sometimes) I think it may me something with floating conditions because when I print the output in a loop it keeps changing.
bool, byte, int, unsigned int, long, unsigned long, they can all hold a 1 or a 0, High or LOW.
bool and byte are the least wasteful on an 8-bit MCU. They both use one byte.
boolean can only represent a HIGH or LOW.
int ledPin = 13; // uses two bytes in flash and two bytes in program memory
const byte ledPin = 13; // only uses one byte in flash
Don't be wasteful on an 8-bit MCU if you can avoid it.
Leo..
If you use the code from post#12, and you get a 0, then the switches are the problem.
Did you read the first line of the sketch.
Are you sure you connected them between pin and ground,
not between pin and 5volt, as you did before.
Every new setting of the dip switches requires a re-start of the Arduino of course.
Leo..