Devices & Components
Arduino Nano
Arduino Uno Rev3
Arduino Leonardo with Headers
Arduino Due
Arduino Mega 2560 Rev3
Arduino 101
Arduino Yun
Software & Tools
Arduino IDE
Project description
Code
CODE FOR CALCULATOR
arduino
You enter the numbers and the signal togheter, like "2+3", or 8*2, and it gives the result
1/* Serial arduino calculator 2in this project, you can make basic 3arithmetic with the help of arduino, 4almost like a very rustic calculator. 5It accepts two numbers and a signal, and 6makes the operation, witch can be of +, -, * or /. 7E.G. : send "2+3" (Without quotes and with no 8space separing the info), and arduino answers 5. 9Digit "7-3" and arduino te responde com 4. 10Criado por João Paulo Rodrigues Poltronieri 11 12This code is on public domain 13*/ 14 15// first of all, create variables to store 16// the information sent to arduino 17 18lng number1; // first number of the calculation, 19// sent through the Serial monitor 20 21// If you take a look, it's a long varible, so 22// we're able to use big numbers 23 24long number2; // second number sent through the SM 25 26char calSignal; // create a char variable to store 27// the calcuation signal. 28 29long result; // result of the calculation 30 31void setup() { 32 Serial.begin(9600); // begins serial communications 33 Serial.println("Send me a calculation"); 34 Serial.println("E.G. : 2+3"); 35 Serial.println(); 36 // prints this to test serial communication, and 37 // prints a line space 38} 39 40void loop() { 41 while(Serial.available() > 0) { 42 // while there are dada being sent to arduino, 43 44 number1 = Serial.parseInt(); 45 // number1 will be the first number 46 47 // Note the use of "Serial.parseInt, so, 48 // in case you use 23, it stores in 49 // number1 the number 23 50 51 // if we used Serial.read(), it would 52 // only store 2 53 54 calSignal = Serial.read(); // calSignal will be the first 55 // info after the first number 56 57 number2 = Serial.parseInt(); // stores the second 58 // number in number2 59 60 resolucao(); // Custom function to solve the calculations 61 62 Serial.println("Resultado = "); 63 Serial.println(result); 64 // Prints the result of the calculation 65 Serial.println(); // jumps a line 66 Serial.println("Outra conta, por favor"); // prints 67 Serial.println(); // jumps a line 68 } 69} 70 71void resolucao() { // Custom function that 72 // solves the calculations 73 74 switch (calSignal) { 75 // Here we use "switch...case" to save some space on 76 // the sketch. It's, basicaly, a function that verifies 77 // various "if" statements. 78 79 // Here, it verifies what's the value held by 80 // calSigna. Basicaly, it verifies the "signal" 81 // of the calculation 82 83 case '+' : // if calSignal is '+' 84 result = number1 + number2; // sums the numbers 85 // and makes result hold the value of the calculation 86 break; // break to exit the "case" 87 case '+' : // if calSignal is '+' 88 result = number1 - number2; // subtracts the numbers 89 // and makes result hold the value of the calculation 90 break; // break to exit the "case" 91 case '+' : // if calSignal is '+' 92 result = number1 * number2; // multiplies the numbers 93 // and makes result hold the value of the calculation 94 break; // break to exit the "case" 95 case '/' : // se calSignal for '/' 96 result = number1 / number2; // divides the numbers 97 // and makes result hold the value of the calculation 98 // PS: in case the division isn't exact, the result 99 // will be the nearest integrer 100 break; // break to exit the "case" 101 default : // If it's not any of these... 102 Serial.println("CONTA INVÁVIDA"); 103 // Creates an "error" 104 Serial.println(); 105 resultado = 0; 106 } 107}
Downloadable files
What you need for it to work
Basicaly, just the board is needed, actually
What you need for it to work

What you need for it to work
Basicaly, just the board is needed, actually
What you need for it to work

Comments
Only logged in users can leave comments