CalculatorFinished

This commit is contained in:
lunarised 2019-03-22 10:57:33 +13:00
parent 94a2ecdc60
commit cfef9bbf8a

220
Calculator/Calculator.ino Normal file
View File

@ -0,0 +1,220 @@
/*
*@author James McKenzie
*@lang C89 Based
*@contact lunarised@outlook.com
*/
void setup() {
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode (11, INPUT_PULLUP);
pinMode (8, INPUT_PULLUP);
pinMode (9, INPUT_PULLUP);
pinMode (10, INPUT_PULLUP);
int i = 0;
int j = 0;
int sum = 0;
resetDisplay();
}
int i;
int j;
int sum;
int result;
int error;
void loop() {
int error = 0;
i = stateA(i);
delay (250);
j = stateA(j);
delay(250);
int operand = stateB();
stateC(operand);
delay(250);
stateD();
digitalWrite(1, LOW);
delay(1000);
digitalWrite(1, HIGH);
}
int stateA(int val){
while (digitalRead(11)==HIGH){
if (digitalRead(8)==LOW){
val = (val + 1)%10;
displayDigit(val, 100);
}
if (digitalRead(9)==LOW){
val = (val + 9)%10;
displayDigit(val, 100);
}
displayDigit(val, 100);
}
return val;
}
int stateB(){
int operand = 0;
digitalWrite(1, LOW);
while (operand == 0){
if (digitalRead(11) == LOW){
operand = 1;
}
if (digitalRead(10) == LOW){
operand = 2;
}
if (digitalRead(9) == LOW){
operand = 3;
}
if (digitalRead(8) == LOW){
operand = 4;
}
}
digitalWrite(1, HIGH);
return operand;
}
void stateC(int operand){
if (operand == 1){
sum = i + j;
}
if (operand == 2){
sum = i - j;
}
if (operand == 3){
sum = i * j;
}
if (operand == 4){
if (j == 0){
error = 1;
}
else{
sum = i / j;
}
}
}
void stateD(){
if (error == 1){
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
delay (5000);
error = 0;
}
else{
while (digitalRead(11)==HIGH){
int splitSum = sum;
while(splitSum >= 9){
displayDigit(splitSum%10, 250);
splitSum /= 10;
}
displayDigit(splitSum, 250);
}
}
}
void displayDigit(int inp, int fTime){
switch (inp){
case 1:
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(fTime);
resetDisplay();
break;
case 2:
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(fTime);
resetDisplay();
break;
case 3:
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(fTime);
resetDisplay();
break;
case 4:
digitalWrite(1, LOW);
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(fTime);
resetDisplay();
break;
case 5:
digitalWrite(1, LOW);
digitalWrite(0, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(6, LOW);
delay(fTime);
resetDisplay();
break;
case 6:
digitalWrite(1, LOW);
digitalWrite(0, LOW);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(6, LOW);
delay(fTime);
resetDisplay();
break;
case 7:
digitalWrite(2, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(fTime);
resetDisplay();
break;
case 8:
digitalWrite(2, LOW);
digitalWrite(1, LOW);
digitalWrite(0, LOW);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(fTime);
resetDisplay();
break;
case 9:
digitalWrite(2, LOW);
digitalWrite(1, LOW);
digitalWrite(0, LOW);
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(fTime);
resetDisplay();
break;
case 0:
digitalWrite(2, LOW);
digitalWrite(0, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(fTime);
resetDisplay();
break;
}
};
void resetDisplay(){
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
}