It is my first program on Arduino , it is basically on the simple funtions of calculator , using ir sensor , and uses tv remote as the keyboard of the calculator..
#include <IRremote.h>
int sensor = 3;
static double number[11] = {0};
static char sign[10];
int count = 0;
int cFix;
char lastIndex, currentIndex;
boolean err = false;
IRrecv ip(sensor);
decode_results results;
void setup() {
Serial.begin(9600);
ip.enableIRIn();
pinMode(sensor, INPUT);
// put your setup code here, to run once:
}
void loop()
{
// put your main code here, to run repeatedly:
if (ip.decode(&results))
{
switch (results.value)
{
//------------------------------------------takes the numbers to array---------------------------------------------
case 0x80BF49B6:
{
Serial.print('1');
number[count] = number[count] * 10 + 1;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BFC936:
{
Serial.print("2");
number[count] = number[count] * 10 + 2;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BF33CC:
{
Serial.print("3");
number[count] = number[count] * 10 + 3;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BF718E:
{
Serial.print("4");
number[count] = number[count] * 10 + 4;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BFF10E:
{
Serial.print("5");
number[count] = number[count] * 10 + 5;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BF13EC:
{
Serial.print("6");
number[count] = number[count] * 10 + 6;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BF51AE:
{
Serial.print("7");
number[count] = number[count] * 10 + 7;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BFD12E:
{
Serial.print("8");
number[count] = number[count] * 10 + 8;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BF23DC:
{
Serial.print("9");
number[count] = number[count] * 10 + 9;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BFE11E:
{
Serial.print("0");
number[count] = number[count] * 10 + 0;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
//------------------------------------------------takes the sign to array---------------------------------------
case 0x80BF53AC://possitive sign
{
sign[count] = '+';
Serial.print('+');
count++;
number[count] = 0;
lastIndex=currentIndex;
currentIndex='s';
if(lastIndex==currentIndex)
{
err=true;
}
break;
}
case 0x80BF4BB4 ://negetive sign
{
sign[count] = '-';
Serial.print('-');
count++;
number[count] = 0;
lastIndex=currentIndex;
currentIndex='s';
if(lastIndex==currentIndex)
{
err=true;
}
break;
}
case 0x80BF9966://multiplication sign
{
sign[count] = '*';
Serial.print('*');
count++;
number[count] = 0;
lastIndex=currentIndex;
currentIndex='s';
if(lastIndex==currentIndex)
{
err=true;
}
break;
}
case 0x80BF837C://division sign
{
sign[count] = '/';
Serial.print('/');
count++;
number[count] = 0;
lastIndex=currentIndex;
currentIndex='s';
if(lastIndex==currentIndex)
{
err=true;
}
break;
}
case 0x80BF738C://equal sign
{
Serial.print('=');
cFix = count;
count = 0;
if (number[cFix] == 0||err==true)//----------------------for checking the valid last number--------------------------
{
Serial.println("Syntax error/math error");
count = 0;
number[0] = 0;
break;
}
else
{
//------------------------for division-------------------
for (int i = 0; i < cFix;)
{
if (sign[i] == '/')
{
int c;
number[i] = number[i] / number[i + 1];
for (c = i + 1; c < cFix; c++)
{
number[c] = number[c + 1];
sign[c - 1] = sign[c];
}
sign[c - 1] = '0';
i = 0;
}
else
{
i++;
}
}
//------------------------for multiplication-------------------
for (int i = 0; i < cFix;)
{
if (sign[i] == '*')
{
int c;
number[i] = number[i] * number[i + 1];
for (c = i + 1; c < cFix; c++)
{
number[c] = number[c + 1];
sign[c - 1] = sign[c];
}
sign[c - 1] = '0';
i = 0;
}
else
{
i++;
}
}
//------------------------for addition-------------------
for (int i = 0; i < cFix; i++)
{
if (sign[i] == '+')
{
number[0] = number[0] + number[i + 1];
}
}
//------------------------for substruction----------------
for (int i = 0; i < cFix; i++)
{
if (sign[i] == '-')
{
number[0] = number[0] - number[i + 1];
}
}
Serial.print(number[0]);
Serial.println("\n");
number[0] = 0;
break;
}
}
default:
{
break;
}
}
ip.resume();
delay(20);
}
}
#include <IRremote.h>
int sensor = 3;
static double number[11] = {0};
static char sign[10];
int count = 0;
int cFix;
char lastIndex, currentIndex;
boolean err = false;
IRrecv ip(sensor);
decode_results results;
void setup() {
Serial.begin(9600);
ip.enableIRIn();
pinMode(sensor, INPUT);
// put your setup code here, to run once:
}
void loop()
{
// put your main code here, to run repeatedly:
if (ip.decode(&results))
{
switch (results.value)
{
//------------------------------------------takes the numbers to array---------------------------------------------
case 0x80BF49B6:
{
Serial.print('1');
number[count] = number[count] * 10 + 1;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BFC936:
{
Serial.print("2");
number[count] = number[count] * 10 + 2;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BF33CC:
{
Serial.print("3");
number[count] = number[count] * 10 + 3;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BF718E:
{
Serial.print("4");
number[count] = number[count] * 10 + 4;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BFF10E:
{
Serial.print("5");
number[count] = number[count] * 10 + 5;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BF13EC:
{
Serial.print("6");
number[count] = number[count] * 10 + 6;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BF51AE:
{
Serial.print("7");
number[count] = number[count] * 10 + 7;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BFD12E:
{
Serial.print("8");
number[count] = number[count] * 10 + 8;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BF23DC:
{
Serial.print("9");
number[count] = number[count] * 10 + 9;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
case 0x80BFE11E:
{
Serial.print("0");
number[count] = number[count] * 10 + 0;
lastIndex = currentIndex;
currentIndex = 'n';
break;
}
//------------------------------------------------takes the sign to array---------------------------------------
case 0x80BF53AC://possitive sign
{
sign[count] = '+';
Serial.print('+');
count++;
number[count] = 0;
lastIndex=currentIndex;
currentIndex='s';
if(lastIndex==currentIndex)
{
err=true;
}
break;
}
case 0x80BF4BB4 ://negetive sign
{
sign[count] = '-';
Serial.print('-');
count++;
number[count] = 0;
lastIndex=currentIndex;
currentIndex='s';
if(lastIndex==currentIndex)
{
err=true;
}
break;
}
case 0x80BF9966://multiplication sign
{
sign[count] = '*';
Serial.print('*');
count++;
number[count] = 0;
lastIndex=currentIndex;
currentIndex='s';
if(lastIndex==currentIndex)
{
err=true;
}
break;
}
case 0x80BF837C://division sign
{
sign[count] = '/';
Serial.print('/');
count++;
number[count] = 0;
lastIndex=currentIndex;
currentIndex='s';
if(lastIndex==currentIndex)
{
err=true;
}
break;
}
case 0x80BF738C://equal sign
{
Serial.print('=');
cFix = count;
count = 0;
if (number[cFix] == 0||err==true)//----------------------for checking the valid last number--------------------------
{
Serial.println("Syntax error/math error");
count = 0;
number[0] = 0;
break;
}
else
{
//------------------------for division-------------------
for (int i = 0; i < cFix;)
{
if (sign[i] == '/')
{
int c;
number[i] = number[i] / number[i + 1];
for (c = i + 1; c < cFix; c++)
{
number[c] = number[c + 1];
sign[c - 1] = sign[c];
}
sign[c - 1] = '0';
i = 0;
}
else
{
i++;
}
}
//------------------------for multiplication-------------------
for (int i = 0; i < cFix;)
{
if (sign[i] == '*')
{
int c;
number[i] = number[i] * number[i + 1];
for (c = i + 1; c < cFix; c++)
{
number[c] = number[c + 1];
sign[c - 1] = sign[c];
}
sign[c - 1] = '0';
i = 0;
}
else
{
i++;
}
}
//------------------------for addition-------------------
for (int i = 0; i < cFix; i++)
{
if (sign[i] == '+')
{
number[0] = number[0] + number[i + 1];
}
}
//------------------------for substruction----------------
for (int i = 0; i < cFix; i++)
{
if (sign[i] == '-')
{
number[0] = number[0] - number[i + 1];
}
}
Serial.print(number[0]);
Serial.println("\n");
number[0] = 0;
break;
}
}
default:
{
break;
}
}
ip.resume();
delay(20);
}
}
0 Comments