Guest User

obichamvlado

a guest
Feb 14th, 2020
70
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2.  
  3. #define LEDred 2
  4. #define LEDgreen 4
  5. #define numberLength 20 //How long the 2 numbers can be - 20 in this case. Please change only this number if bigger numbers are needed.
  6.  
  7. char firstNumber[numberLength];
  8. int firstNumberCount = 1;
  9. int firstNumberAsInt = 0;
  10. char secondNumber[numberLength];
  11. int secondNumberCount = 1;
  12. int secondNumberAsInt = 0;
  13. bool first = true;
  14. bool second = false;
  15. int result = 0;
  16.  
  17.  
  18. void setup() {
  19. Serial.begin(9600);
  20. pinMode(LEDred, OUTPUT);
  21. pinMode(LEDgreen, OUTPUT);
  22. firstNumber[0] = 0;
  23. secondNumber[0] = 0;
  24. }
  25.  
  26. void loop() {
  27. int charsRead = 0;
  28. while(Serial.available() > 0)
  29. {
  30. char c = Serial.read();
  31. if(c != '\n')
  32. {
  33. digitalWrite(LEDgreen, LOW);
  34. digitalWrite(LEDred, LOW);
  35. if(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9')
  36. {
  37. if(first)
  38. {
  39. firstNumber[charsRead] = c;
  40. charsRead++;
  41. firstNumberCount = charsRead;
  42. }
  43. else if (second)
  44. {
  45. secondNumber[charsRead] = c;
  46. charsRead++;
  47. secondNumberCount = charsRead;
  48. }
  49. }
  50. else if (c == '+' || c == '-' || c == '*' || c == '/')
  51. {
  52. switch(c)
  53. {
  54. case '+':
  55. //code for addition and converting the arrays into integers
  56. first = false;
  57. second = true;
  58. for(int i = 0; i < firstNumberCount; i++)
  59. {
  60. int integ = ((int)(firstNumber[i]) - 48) * pow((1*10),(firstNumberCount-i)); // na stepen count - i ??? kak e na stepen v C, da proverq formulata na vladan da kaja na vladan che atof e cheating
  61. firstNumberAsInt += integ;
  62. }
  63. for(int i = 0; i < secondNumberCount; i++)
  64. {
  65. int integ = ((int)(secondNumber[i]) - 48) * pow((1*10),(secondNumberCount-i)); // na stepen count - i ??? kak e na stepen v C, da proverq formulata na vladan da kaja na vladan che atof e cheating
  66. secondNumberAsInt += integ;
  67. }
  68. result = firstNumberAsInt + secondNumberAsInt;
  69. break;
  70.  
  71. case '-':
  72. //code for substraction and converting the arrays into integers
  73. first = false;
  74. second = true;
  75. for(int i = 0; i < firstNumberCount; i++)
  76. {
  77. int integ = ((int)(firstNumber[i]) - 48) * pow((1*10),(firstNumberCount-i)); // na stepen count - i ??? kak e na stepen v C, da proverq formulata na vladan da kaja na vladan che atof e cheating
  78. firstNumberAsInt += integ;
  79. }
  80. for(int i = 0; i < secondNumberCount; i++)
  81. {
  82. int integ = ((int)(secondNumber[i]) - 48) * pow((1*10),(secondNumberCount-i)); // na stepen count - i ??? kak e na stepen v C, da proverq formulata na vladan da kaja na vladan che atof e cheating
  83. secondNumberAsInt += integ;
  84. }
  85. result = firstNumberAsInt - secondNumberAsInt;
  86. break;
  87.  
  88. case '*':
  89. //code for multiplying and converting the arrays into integers
  90. first = false;
  91. second = true;
  92. for(int i = 0; i < firstNumberCount; i++)
  93. {
  94. int integ = ((int)(firstNumber[i]) - 48) * pow((1*10),(firstNumberCount-i)); // na stepen count - i ??? kak e na stepen v C, da proverq formulata na vladan da kaja na vladan che atof e cheating
  95. firstNumberAsInt += integ;
  96. }
  97. for(int i = 0; i < secondNumberCount; i++)
  98. {
  99. int integ = ((int)(secondNumber[i]) - 48) * pow((1*10),(secondNumberCount-i)); // na stepen count - i ??? kak e na stepen v C, da proverq formulata na vladan da kaja na vladan che atof e cheating
  100. secondNumberAsInt += integ;
  101. }
  102. result = firstNumberAsInt * secondNumberAsInt;
  103. break;
  104.  
  105. case '/':
  106. //code for dividing and converting the arrays into integers
  107. first = false;
  108. second = true;
  109. for(int i = 0; i < firstNumberCount; i++)
  110. {
  111. int integ = ((int)(firstNumber[i]) - 48) * pow((1*10),(firstNumberCount-i)); // na stepen count - i ??? kak e na stepen v C, da proverq formulata na vladan da kaja na vladan che atof e cheating
  112. firstNumberAsInt += integ;
  113. }
  114. for(int i = 0; i < secondNumberCount; i++)
  115. {
  116. int integ = ((int)(secondNumber[i]) - 48) * pow((1*10),(secondNumberCount-i)); // na stepen count - i ??? kak e na stepen v C, da proverq formulata na vladan da kaja na vladan che atof e cheating
  117. secondNumberAsInt += integ;
  118. }
  119. result = firstNumberAsInt / secondNumberAsInt;
  120. break;
  121. }
  122. }
  123. if(c == '=')
  124. {
  125. first = true;
  126. second = false;
  127. firstNumber[0] = 0;
  128. secondNumber[0] = 0;
  129. firstNumberCount = 1;
  130. secondNumberCount = 1;
  131. firstNumberAsInt = 0;
  132. secondNumberAsInt = 0;
  133. //reseting everything, printing result.
  134. Serial.print("Result is: "); Serial.println(result);
  135. //make LEDS light up
  136. if(result > 0)
  137. digitalWrite(LEDgreen, HIGH);
  138. else if (result < 0)
  139. digitalWrite(LEDred, HIGH);
  140. else
  141. {
  142. digitalWrite(LEDgreen, LOW);
  143. digitalWrite(LEDred, LOW);
  144. }
  145. result = 0;
  146. }
  147. }
  148. }
  149. }
RAW Paste Data