Skip to content

Instantly share code, notes, and snippets.

@arielchuri
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arielchuri/9592236 to your computer and use it in GitHub Desktop.
Save arielchuri/9592236 to your computer and use it in GitHub Desktop.
// Binary Decoder Project
// Sparkle Labs
// Set a binary number with a DIP switch and decode it in the
// serial port monitor.
//
//
// +--------------------------------+
// | +----------------------------+ |
// | | +------------------------+ | |
// | | | +--------------------+ | | |
// +----------------------+-+-+-+-------+ | | | |
// | G 1 1 1 1 9 8 7 6 5 4 3 2 1 0 | +-+-+-+-+-+
// | N 3 2 1 0 | | |
// | D ~ ~ ~ ~ ~ ~ | | 1 2 3 4 |
// +--+-+ | | |
// | USB| +-+ +-+-+-+-+-+
// +--+-+ +---------------+ | | | | |
// | | ARDUINO UNO | | +-O-O-O
// | +---------------+ | |
// | | |
// | G G | |
// | 5 N N A A A A A A | |
// | V D D 0 1 2 3 4 5 +-+ |
// +------------------------------------+ ---+---
// --+--
// -+-
//
// Here are variables to store the state of the different switches.
int onebit;
int twobit;
int fourbit;
int eightbit;
// Here we store the total value.
int halfbyte;
void setup() {
pinMode(2,INPUT_PULLUP); // This setting allows us to have a switch without using a resistor.
pinMode(3,INPUT_PULLUP); // The pin is pulled up to 5volts when the switch is open.
pinMode(4,INPUT_PULLUP);
pinMode(5,INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// First we read in all of the pins.
onebit = digitalRead(5);
twobit = digitalRead(4);
fourbit = digitalRead(3);
eightbit = digitalRead(2);
// Then add the values together.
halfbyte = onebit + twobit * 2 + fourbit * 4 + eightbit * 8;
Serial.println(halfbyte);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment