Guest User

Tribble_Calculator.ino

a guest
Jul 3rd, 2013
149
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Tribble Calculator--to multiply and look cute
  3. By Chris Chungbin
  4. input button on pin 7
  5. speaker on 5
  6. */
  7.  
  8. unsigned long fact1=0; //factors
  9. unsigned long fact2=0;
  10. unsigned long prod=0; //product
  11. int dig1[9]={0,0,0,0,0,0,0,0,0}; //fact1 digits where dig1[0] is most significant digit. 10 represents *not* a digit
  12. int dig2[9]={0,0,0,0,0,0,0,0,0}; //and fact2 digits
  13. int digp[10]={0,0,0,0,0,0,0,0,0,0}; //and prod digits
  14. int in=0; //input
  15. int digits1=0; //number of digits in fact1
  16. int digits2=0; //and fact2
  17. int digitsp=0; //and product
  18. unsigned long p=0; //powers of 10. useful for int to array conversion
  19.  
  20. void setup() {
  21. pinMode(6,INPUT);
  22. pinMode(7,INPUT);
  23. pinMode(5,OUTPUT);
  24. }
  25.  
  26.  
  27.  
  28. void loop() {
  29. in=0;
  30. digits1=0;
  31. fact1=0;
  32. while(in!=-1) { //fact1 input stuff
  33. while(!digitalRead(7)) { //wait for button to take input
  34. }
  35. in=input();
  36. if(in>10) { //11 isn't a digit
  37. squeak();
  38. }
  39. else {
  40. if(digits1==9&&in!=-1) { //input overflow
  41. squeak();
  42. }
  43. else {
  44. for(int i=0; i<in; i++) { //echo input
  45. purr();
  46. delay(300);
  47. }
  48. if(in==10) { //correct for 10 presses is a 0
  49. in=0;
  50. }
  51. if(in!=-1) { //assign the value of the input to the digit in the array
  52. dig1[digits1]=in;
  53. digits1++;
  54. }
  55. if(in==-1&&digits1==0) { //a number must have been entred before moving on
  56. squeak();
  57. in=0;
  58. }
  59. }
  60. }
  61. }
  62.  
  63.  
  64. in=0;
  65. digits2=0; //and all the same stuff for factor 2. I should have made it an array ...
  66. fact2=0;
  67. while(in!=-1) {
  68. while(!digitalRead(7)) {
  69. }
  70. in=input();
  71. if(in>10) {
  72. squeak();
  73. }
  74. else {
  75. if(digits2==9&&in!=-1) {
  76. squeak();
  77. }
  78. else {
  79. for(int i=0; i<in; i++) {
  80. purr();
  81. delay(300);
  82. }
  83. if(in==10) {
  84. in=0;
  85. }
  86. if(in!=-1) {
  87. dig2[digits2]=in;
  88. digits2++;
  89. }
  90. if(in==-1&&digits2==0) {
  91. squeak();
  92. in=0;
  93. }
  94. }
  95. }
  96. }
  97.  
  98.  
  99. for(int i=0; i<digits1; i++) { //arrays to unsigned ints
  100. fact1=10*fact1+dig1[i];
  101. }
  102. for(int i=0; i<digits2; i++) {
  103. fact2=10*fact2+dig2[i];
  104. }
  105.  
  106. prod=fact1*fact2; //the easiest part of making a calculator: multiplication
  107. delay(1000); //pause before answer return
  108. if(prod==0) { //the product being zero messes with later things
  109. for(int i=0; i<10; i++) { //purr zero
  110. delay(300);
  111. purr();
  112. }
  113. }
  114. else { //if the product wasn't zero, do the normal stuff
  115. digitsp=1;
  116. p=10;
  117. while(float(prod)/p>=1) { //finds value of digitsp and p
  118. digitsp++;
  119. p*=10;
  120. }
  121. for(int i=0; i<digitsp; i++) { //finds digits and purrs value
  122. p=1;
  123. for(int j=0; j<digitsp-i-1; j++) { //find p, the order of magnitude of this digit
  124. p*=10;
  125. }
  126. digp[i]=int(float(prod)/p); //here's where the digit gets pulled out of the number
  127. prod=prod-p*digp[i]; //I had to take off the digits as I went to make subsequent digits work
  128. if(digp[i]==0) { //set up to purr 10 instead of 0
  129. digp[i]=10;
  130. }
  131. for(int j=0; j<digp[i]; j++) { //purr value
  132. purr();
  133. delay(300);
  134. }
  135. delay(700);
  136. }
  137. }
  138. }
  139.  
  140.  
  141.  
  142.  
  143. int input() { //returns number of presses or -1 for a long hold
  144. int n=0;
  145. unsigned long t=0;
  146. while(1) {
  147. t=millis();
  148. while(digitalRead(7)) { //wait to let go
  149. if(t+1000<millis()) { //purr in a hold
  150. tone(5, 440);
  151. delay(40);
  152. noTone(5);
  153. delay(10);
  154. }
  155. }
  156. if(t+1000<millis() && n==0) { //if it's a hold, return -1
  157. return -1;
  158. }
  159. if(millis()-t>20) { //debounce button
  160. n++;
  161. }
  162. t=millis();
  163. while(!digitalRead(7)) { //wait to push button again
  164. if(t+1000<millis()) { //unless time runs out
  165. return n;
  166. }
  167. }
  168. delay(20);
  169. }
  170. }
  171.  
  172.  
  173. void squeak() { //error ... or klingons
  174. for(int i=0; i<2; i++) {
  175. tone(5, 3300);
  176. delay(40);
  177. noTone(5);
  178. delay(15);
  179. for(int i=0; i<5; i++) {
  180. tone(5, 3500);
  181. delay(40);
  182. noTone(5);
  183. delay(15);
  184. }
  185. delay(50);
  186. }
  187. }
  188.  
  189.  
  190. void purr() { //short chirp for digits
  191. for(int i=0; i<2; i++) {
  192. tone(5, 523);
  193. delay(40);
  194. noTone(5);
  195. delay(10);
  196. }
  197. tone(5, 518);
  198. delay(40);
  199. noTone(5);
  200. delay(10);
  201. tone(5, 512);
  202. delay(40);
  203. noTone(5);
  204. delay(10);
  205. }
RAW Paste Data