#include "mylib.h"
#define SMALLDELAY 200000
#define VERYSMALLDELAY 100000
#define LONGDELAY 500000
// Function declarations
void displaynumbers(long );
void displayalpha(char );
void loopnumbers(long, bool);
void loopalpha();
void displaysum();
long takeinput();
void setup() {
// put your setup code here, to run once:
myinit();
displaynumbers(0); // display 0 by default
}
void displaynumbers(long num)
{
char arr[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x67};
output(arr[num]);
}
void displayalpha(char alpha)
{
char alpharr[] = {0x77, 0x7F, 0x39, 0x3F, 0x79, 0x71};
switch (alpha)
{
case 'A':
output(alpharr[0]);
break;
case 'B':
output(alpharr[1]);
break;
case 'C':
output(alpharr[2]);
break;
case 'D':
output(alpharr[3]);
break;
case 'E':
output(alpharr[4]);
break;
case 'F':
output(alpharr[5]);
break;
}
}
void loopnumbers(long start, bool reverse = false)
{
if (reverse)
{ for (volatile long i = start; i >= 0; i--)
{
displaynumbers(i);
timedelay(SMALLDELAY);
}
}
else {
for (volatile long i = start; i < 10; i++)
{
displaynumbers(i);
timedelay(SMALLDELAY);
}
}
}
void loopalpha()
{
for (volatile char alpha = 'A'; alpha <= 'F'; alpha++)
{
displayalpha(alpha);
timedelay(SMALLDELAY);
}
}
void displaysum() {
long sum = 0;
for (long i = 0; i < 4; i++) {
sum += takeinput();
timedelay(VERYSMALLDELAY);
}
// If sum is 10 or greater, reduce it to a single digit
if (sum >= 10) {
sum = sum % 10 + sum / 10;
}
timedelay(SMALLDELAY);
displaynumbers(sum);
}
long takeinput() {
// Wait for input to be received
while (input() == 0) {
// loop until a value is received
}
long value = input(); // Read the input value
// Wait for input to be released
switch (value)
{
case 1:
value = 1;
break;
case 2:
value = 2;
break;
case 4:
value = 3;
break;
case 8:
value = 4;
break;
case 16:
value = 5;
break;
case 32:
value = 6;
break;
case 64:
value = 7;
break;
case 128:
value = 8;
break;
}
displaynumbers(value);
while (input() != 0) {
// loop until input is released
}
return value;
}
void loop() {
// put your main code here, to run repeatedly:
// //Press any switch & displaynumbers that number.
// Ex: 1 - > 1, 2 ->2, ... 8 -> 8.
// (input() & 0x01)?displaynumbers(1):output(0x00);
// (input() & 0x02)?displaynumbers(2):output(0x00);
// (input() & 0x04)?displaynumbers(3):output(0x00);
// (input() & 0x08)?displaynumbers(4):output(0x00);
// (input() & 0x10)?displaynumbers(5):output(0x00);
// (input() & 0x20)?displaynumbers(6):output(0x00);
// (input() & 0x40)?displaynumbers(7):output(0x00);
// (input() & 0x80)?displaynumbers(8):output(0x00);
//Press any switch & displaynumbers the reverse order
// Ex: 1 -> 8, 2-> 7, 8 -> 1
// (input() & 0x01) ? displaynumbers(8) : output(0x00);
// (input() & 0x02) ? displaynumbers(7) : output(0x00);
// (input() & 0x04) ? displaynumbers(6) : output(0x00);
// (input() & 0x08) ? displaynumbers(5) : output(0x00);
// (input() & 0x10) ? displaynumbers(4) : output(0x00);
// (input() & 0x20) ? displaynumbers(3) : output(0x00);
// (input() & 0x40) ? displaynumbers(2) : output(0x00);
// (input() & 0x80) ? displaynumbers(1) : output(0x00);
//Press any switch & displaynumbers should start from
// that number till nine and restart from that number
// till 9. Continuous loop.
// (input() & 0x01) ? loopnumbers(1) : output(0x00);
// (input() & 0x02) ? loopnumbers(2) : output(0x00);
// (input() & 0x04) ? loopnumbers(3) : output(0x00);
// (input() & 0x08) ? loopnumbers(4) : output(0x00);
// (input() & 0x10) ? loopnumbers(5) : output(0x00);
// (input() & 0x20) ? loopnumbers(6) : output(0x00);
// (input() & 0x40) ? loopnumbers(7) : output(0x00);
// (input() & 0x80) ? loopnumbers(8) : output(0x00);
//Press switch 7 & displaynumbers from 0 to 9 and 9 to 0 one
// time.
// if (input() & 0x80)
// { loopnumbers(0);
// loopnumbers(9, true);
// }
// else
// output(0x00);
//Display A – F in a loop
// loopalpha();
//
// Initially display 0.
// When switch 1 is pressed eVERY time increment by 1
// if(input() & 0x01)
// {
// static long num = 0;
// num++;
// if (num>9)
// {
// num = 9;
// }
// displaynumbers(num);
// timedelay(SMALLDELAY);
// }
//Initially display 9.
// When switch 2 is pressed eVERY time decrement by 1
// if(input() & 0x02)
// {
// static long num = 9;
// num--;
// if (num<0)
// {
// num = 0;
// }
// displaynumbers(num);
// timedelay(SMALLDELAY);
// }
// Initially display 0
// When switch 1 is pressed start displaying 0 to 9
// Continuously. When switch 2 is pressed stop display.
if (input() & 0x01)
{
for (static volatile long i = 1; i < 10; i++)
{
if (input() & 0x02)
{
break;
}
else
{
displaynumbers(i);
timedelay(SMALLDELAY);
}
}
}
// Initially display 9
// When switch 1 is pressed start displaying 9 to 0
// Continuously. When Switch 2 is pressed stop display.
//make sure defualt output set to 9 in setup
if (input() & 0x01)
{
for (static volatile long i = 8; i >= 0; i--)
{
if (input() & 0x02)
{
break;
}
else
{
displaynumbers(i);
timedelay(SMALLDELAY);
}
}
}
//Get a 4 digit number from user (Press the switch
// Sequentially (Example 1256) Add the sum of
// Them the sum is more than 1 digit then add
// them again and show the sum on display.
// displaysum();
// timedelay(LONGDELAY);
// displaynumbers(0);
}