Skip to content

Instantly share code, notes, and snippets.

@ikbelkirasan
Forked from hsiboy/dip_sw.ino
Created July 28, 2016 00:13
Show Gist options
  • Save ikbelkirasan/90195ef3765aad890750d97ff4532560 to your computer and use it in GitHub Desktop.
Save ikbelkirasan/90195ef3765aad890750d97ff4532560 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