-
-
Save ikbelkirasan/90195ef3765aad890750d97ff4532560 to your computer and use it in GitHub Desktop.
read 4 position dip switch - arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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