Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Tribble Calculator--to multiply and look cute
- By Chris Chungbin
- input button on pin 7
- speaker on 5
- */
- unsigned long fact1=0; //factors
- unsigned long fact2=0;
- unsigned long prod=0; //product
- 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
- int dig2[9]={0,0,0,0,0,0,0,0,0}; //and fact2 digits
- int digp[10]={0,0,0,0,0,0,0,0,0,0}; //and prod digits
- int in=0; //input
- int digits1=0; //number of digits in fact1
- int digits2=0; //and fact2
- int digitsp=0; //and product
- unsigned long p=0; //powers of 10. useful for int to array conversion
- void setup() {
- pinMode(6,INPUT);
- pinMode(7,INPUT);
- pinMode(5,OUTPUT);
- }
- void loop() {
- in=0;
- digits1=0;
- fact1=0;
- while(in!=-1) { //fact1 input stuff
- while(!digitalRead(7)) { //wait for button to take input
- }
- in=input();
- if(in>10) { //11 isn't a digit
- squeak();
- }
- else {
- if(digits1==9&&in!=-1) { //input overflow
- squeak();
- }
- else {
- for(int i=0; i<in; i++) { //echo input
- purr();
- delay(300);
- }
- if(in==10) { //correct for 10 presses is a 0
- in=0;
- }
- if(in!=-1) { //assign the value of the input to the digit in the array
- dig1[digits1]=in;
- digits1++;
- }
- if(in==-1&&digits1==0) { //a number must have been entred before moving on
- squeak();
- in=0;
- }
- }
- }
- }
- in=0;
- digits2=0; //and all the same stuff for factor 2. I should have made it an array ...
- fact2=0;
- while(in!=-1) {
- while(!digitalRead(7)) {
- }
- in=input();
- if(in>10) {
- squeak();
- }
- else {
- if(digits2==9&&in!=-1) {
- squeak();
- }
- else {
- for(int i=0; i<in; i++) {
- purr();
- delay(300);
- }
- if(in==10) {
- in=0;
- }
- if(in!=-1) {
- dig2[digits2]=in;
- digits2++;
- }
- if(in==-1&&digits2==0) {
- squeak();
- in=0;
- }
- }
- }
- }
- for(int i=0; i<digits1; i++) { //arrays to unsigned ints
- fact1=10*fact1+dig1[i];
- }
- for(int i=0; i<digits2; i++) {
- fact2=10*fact2+dig2[i];
- }
- prod=fact1*fact2; //the easiest part of making a calculator: multiplication
- delay(1000); //pause before answer return
- if(prod==0) { //the product being zero messes with later things
- for(int i=0; i<10; i++) { //purr zero
- delay(300);
- purr();
- }
- }
- else { //if the product wasn't zero, do the normal stuff
- digitsp=1;
- p=10;
- while(float(prod)/p>=1) { //finds value of digitsp and p
- digitsp++;
- p*=10;
- }
- for(int i=0; i<digitsp; i++) { //finds digits and purrs value
- p=1;
- for(int j=0; j<digitsp-i-1; j++) { //find p, the order of magnitude of this digit
- p*=10;
- }
- digp[i]=int(float(prod)/p); //here's where the digit gets pulled out of the number
- prod=prod-p*digp[i]; //I had to take off the digits as I went to make subsequent digits work
- if(digp[i]==0) { //set up to purr 10 instead of 0
- digp[i]=10;
- }
- for(int j=0; j<digp[i]; j++) { //purr value
- purr();
- delay(300);
- }
- delay(700);
- }
- }
- }
- int input() { //returns number of presses or -1 for a long hold
- int n=0;
- unsigned long t=0;
- while(1) {
- t=millis();
- while(digitalRead(7)) { //wait to let go
- if(t+1000<millis()) { //purr in a hold
- tone(5, 440);
- delay(40);
- noTone(5);
- delay(10);
- }
- }
- if(t+1000<millis() && n==0) { //if it's a hold, return -1
- return -1;
- }
- if(millis()-t>20) { //debounce button
- n++;
- }
- t=millis();
- while(!digitalRead(7)) { //wait to push button again
- if(t+1000<millis()) { //unless time runs out
- return n;
- }
- }
- delay(20);
- }
- }
- void squeak() { //error ... or klingons
- for(int i=0; i<2; i++) {
- tone(5, 3300);
- delay(40);
- noTone(5);
- delay(15);
- for(int i=0; i<5; i++) {
- tone(5, 3500);
- delay(40);
- noTone(5);
- delay(15);
- }
- delay(50);
- }
- }
- void purr() { //short chirp for digits
- for(int i=0; i<2; i++) {
- tone(5, 523);
- delay(40);
- noTone(5);
- delay(10);
- }
- tone(5, 518);
- delay(40);
- noTone(5);
- delay(10);
- tone(5, 512);
- delay(40);
- noTone(5);
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement