Skip to content

Instantly share code, notes, and snippets.

@hsiboy
Last active February 20, 2022 23:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save hsiboy/b87b5cde33514a16bc96 to your computer and use it in GitHub Desktop.
Save hsiboy/b87b5cde33514a16bc96 to your computer and use it in GitHub Desktop.
read 4 position dip switch - arduino
//Create and Define Global Variables
int dipPins[] = {2, 3, 4, 5}; // DIP Switch Pins
int transAddress;
void setup()
{
Serial.begin(9600);
int i;
for(i = 0; i<=3; i++){
pinMode(dipPins[i], INPUT); // set the digital pins (defined above) as input
digitalWrite(dipPins[i], HIGH); // set internal pullup resistor on
}
}
void loop()
{
transAddress = address();
Serial.println(transAddress);
delay(1000);
}
//Read state from DIP Switch (4 positions used)
byte address(){
int i,j=0;
//Get the switches state
for(i=0; i<=3; i++){
j = (j << 1) | digitalRead(dipPins[i]); // read each input pin
}
return j; //return address
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment