Infrared Calculator giving wrong results with operands over 6 digits
i have made calculator controlled infrared remote. wiring not faulty , neither remote. new arduino (1 week) , have mega 2560. there seems problem when input operands collectively above 7 digits or more, results erroneous. example, 111111 * 111111 gives: 48177, , 111111 + 111111 gives: 25614, , 1 * 100000 gives: 34464. however, 111111 - 111111 gives: 0. input remote stored (when it's number) string, , when +, - , *, or / operators found, string input converted int , put first operand, , string input cleared, , new numbers stored in string. when '=' found, input string stops , converted int , stored in second operand. after that, operation supposedly computed , displayed on screen. when signal ('c') found, clears display , variables, starting program again. here code(i still beginner programmer):
code: [select]
#include <liquidcrystal.h> //lcd library
#include "irremote.h" //ir remote/receiver library
int receiver = 22; //pin ir receiver results
char val; //what returned receiver
string oper; //input stream
unsigned int oper1; //operand 1
char operate; //operator
unsigned int oper2; //operand 2
unsigned int ret; //final result
// initialize library numbers of interface pins
liquidcrystal lcd(12, 11, 5, 4, 3, 2);
irrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
void setup() {
serial.begin(9600);
serial.println("ir calculator");
irrecv.enableirin(); // start receiver
lcd.begin(16, 2);
}
void loop() {
if (irrecv.decode(&results)) // have received ir signal?
{
parseresult(translateir()); //evaluate signal
delay(500);
irrecv.resume(); // receive next value
}
}
void parseresult(char result) {
if (result == 'c') { //clear values , display
oper1 = 0;
oper2 = 0;
oper = "";
operate = 0;
ret = 0;
lcd.clear();
}
else if ((result < 58) && (result > 47)) { // result number?
oper += result; //add result string
lcd.print(result);
}
else if (result == ' '); //result not accomodated when evaluating signals
else if (!operate && oper) { //no operator, there number input
oper1 = oper.toint(); //set first number
oper = "";
operate = result; //set operator
lcd.clear();
lcd.setcursor(0, 1);
lcd.print(operate);
lcd.setcursor(0, 0);
}
else if (result == '=' && operate && oper1 && oper) { // perform operations if there operator, '=' sign, , first operand, , more number input
oper2 = oper.toint(); //set second operand
switch (operate) { // perform operation based on operator
case '+': ret = oper1 + oper2; break; //set final result
case '-': ret = oper1 - oper2; break;
case '*': ret = oper1 * oper2; break;
case '/': ret = oper1 / oper2; break;
}
lcd.clear();
lcd.print('=');
lcd.setcursor(0, 1);
lcd.print(ret);// show final result
}
else { // nothing odd behavior
;
}
}
char translateir() // takes action based on ir code received
// describing remote ir codes
{
switch (results.value)
{
case 0xff629d: serial.println(" +"); val = '+'; break;
case 0xff22dd: serial.println(" *"); val = '*'; break;
case 0xff02fd: serial.println(" ="); val = '='; break;
case 0xffc23d: serial.println(" /"); val = '/'; break;
case 0xffa857: serial.println(" -"); val = '-'; break;
case 0xff6897: serial.println(" 0"); val = '0'; break;
case 0xff30cf: serial.println(" 1"); val = '1'; break;
case 0xff18e7: serial.println(" 2"); val = '2'; break;
case 0xff7a85: serial.println(" 3"); val = '3'; break;
case 0xff10ef: serial.println(" 4"); val = '4'; break;
case 0xff38c7: serial.println(" 5"); val = '5'; break;
case 0xff5aa5: serial.println(" 6"); val = '6'; break;
case 0xff42bd: serial.println(" 7"); val = '7'; break;
case 0xff4ab5: serial.println(" 8"); val = '8'; break;
case 0xff52ad: serial.println(" 9"); val = '9'; break;
case 0xffa25d: serial.println(" c"); val = 'c'; break; // clear calculator value
default:
serial.println(" other button ");
val = ' '; // nothing value
}// end case
serial.println(results.value);
return val; // return character after determining signal
}
you need bigger numbers int. example long.
Arduino Forum > Using Arduino > Programming Questions > Infrared Calculator giving wrong results with operands over 6 digits
arduino
Comments
Post a Comment