Skip to content

Instantly share code, notes, and snippets.

@j-mcc1993
Created June 27, 2014 05:52
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 j-mcc1993/7b6d0d56eeafd272160d to your computer and use it in GitHub Desktop.
Save j-mcc1993/7b6d0d56eeafd272160d to your computer and use it in GitHub Desktop.
Arduino sketch that turns 8 LEDs into a byte adder using two buttons.
const int pot = A15;
const int pushButton = 22;
const int pushButton2 = 27;
int bitList[8] = {
};
int pinSet[] = {
13,11,9,7,5,3,14,16};
byte sensorValue = 0;
int op1;
int op2;
void setup(){
for (int i = 0; i < 8; i++) {
pinMode(pinSet[i], OUTPUT);
}
Serial.begin(9600);
pinMode(pushButton, INPUT_PULLUP);
pinMode(pushButton2, INPUT_PULLUP);
}
void loop() {
int buttonState = digitalRead(pushButton);
int buttonState1 = digitalRead(pushButton2);
if (buttonState == HIGH && buttonState1 == HIGH) {
sensorValue = analogRead(pot)/4;
String potValue = String(sensorValue, BIN);
Serial.println(potValue);
for (int i = 0; i < 8; i++) {
bitList[7-i] = bitRead(sensorValue, i);
}
for (int i = 0; i < 8; i++) {
if (bitList[i] == 1) {
digitalWrite(pinSet[7-i], HIGH);
}
else {
digitalWrite(pinSet[7-i], LOW);
}
}
}
else if (buttonState == LOW && buttonState1 == HIGH) {
for (int i = 0; i < 8; i++) {
digitalWrite(pinSet[i], LOW);
}
delay(500);
sensorValue = analogRead(pot)/4;
String potValue = String(sensorValue, BIN);
Serial.println(potValue);
for (int i = 0; i < 8; i++) {
bitList[7-i] = bitRead(sensorValue, i);
}
for (int i = 0; i < 8; i++) {
if (bitList[i] == 1) {
digitalWrite(pinSet[7-i], HIGH);
}
else {
digitalWrite(pinSet[7-i], LOW);
}
}
op1 = sensorValue;
}
if (buttonState1 == LOW && buttonState == LOW) {
sensorValue = analogRead(pot)/4;
op2 = sensorValue;
String potValue = String((op1 + op2), BIN);
Serial.println(potValue);
for (int i = 0; i < 8; i++) {
bitList[7-i] = bitRead(op1 + op2, i);
}
for (int i = 0; i < 8; i++) {
if (bitList[i] == 1) {
digitalWrite(pinSet[7-i], HIGH);
}
else {
digitalWrite(pinSet[7-i], LOW);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment