Skip to content

Instantly share code, notes, and snippets.

@bbevan
Last active January 4, 2020 16:49
Show Gist options
  • Select an option

  • Save bbevan/3d6dd08ea4084f8679dd9c5dd560b8c3 to your computer and use it in GitHub Desktop.

Select an option

Save bbevan/3d6dd08ea4084f8679dd9c5dd560b8c3 to your computer and use it in GitHub Desktop.
Arduino Binary Adder
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
// Digital Pins 10,9,8,7 are for binary input
// for the left Dip-Switch.
// INPUT_PULLUP is used since the DipSwitches
// are wired to ground.
pinMode(10, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
// Analog Pins A0, A1,A2, A3 are for binary input
// for the right Dip-Switch.
pinMode(A0, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
}
int digital_binary_to_int()
{
int a = digitalRead(10);
int b = digitalRead(9);
int c = digitalRead(8);
int d = digitalRead(7);
return a*1 + b*2 + c*4 + d*8;
}
int analog_binary_to_int()
{
float a = floor(digitalRead(A0));
float b = floor(digitalRead(A1));
float c = floor(digitalRead(A2));
float d = floor(digitalRead(A3));
return d*1 + c*2 + b*4 + a*8;
}
void loop()
{
String output_add;
int x = digital_binary_to_int();
int y = analog_binary_to_int();
int ans = x + y;
output_add = String(ans);
lcd.begin(16,2);
lcd.print(output_add);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment