#define Coinin 12 // Declare GPIO 12 as coin input #define Coinout 11 // Declare GPIO 11 as coin output int analogValue = 0; // To be to hold analog value from button connected to A0 line int eightballz = 0; // To determine which answer the magic 8 ball gives // Connect a 10k pull up resistor between the A0 line and the 5v line. // Connect a button between A0 and ground. When the button is pushed, it shorts the 5v line to 0v safely through the pull up resistor void setup() { pinMode(Coinin, INPUT); // coinin is an input pinMode(Coinout, OUTPUT); // coinout is an output Serial.begin(9600); // start serial to PC delay(1000); // Wait for one full second Serial.println("The MATGIC 8 BALL Program - Insert Coins & Ask Your Questions"); // Print this to the serial monitor. You'll need to open the serial monitor to see this. delay(1000); // Wait for 1 full second } void loop() { eightball(); // Call the eightball function analogValue = analogRead(0); // Take an analog reading from A0 line. If button is pressed, we see 0v, and execute the next bit of code. If not, we see 5v. if(analogValue < 100) // If button is pushed, execute { if(digitalRead(Coinin) == HIGH) // If credits are available, execute the following code { ballprint(); // Execute ballprint function digitalWrite(Coinout,HIGH); // Send a pulse to the coinout line to decrement credits delay(50); digitalWrite(Coinout,LOW); delay(50); while(analogRead(0) < 100) { // While the button is still being pressed, do nothing. Wait. { } } } else { Serial.println("INSERT COINS IF YOU WANT TO KNOW THE ANSWER TO YOUR QUESTIONS!"); // In the case where no credits are available, this is what is printed. delay(150); startoverb(); // Call startoverb function while(analogRead(0) < 100) { // While the button is still being pressed, do nothing. { } } } } } void eightball() // This function acts to make the 8 ball answer random. There are 20x answers. Everytime this is called, it increments the value of eightballz // if int eightballz equals 20, it sets it back to a value of 0 { if(eightballz == 20) { eightballz = 0; } else { eightballz = eightballz + 1; } } void ballprint() // This function takes the value of eightballz, and prints the relative answer to the serial monitor based on the value that eightballz is currently at. { if(eightballz == 0) { Serial.println("It is certain!"); startover(); } if(eightballz == 1) { Serial.println("It is decidedly so!"); startover(); } if(eightballz == 2) { Serial.println("Without a doubt!"); startover(); } if(eightballz == 3) { Serial.println("Yes definitely!"); startover(); } if(eightballz == 4) { Serial.println("You may rely on it!"); startover(); } if(eightballz == 5) { Serial.println("As I see it, yes!"); startover(); } if(eightballz == 6) { Serial.println("Most likely!"); startover(); } if(eightballz == 7) { Serial.println("Outlook good!"); startover(); } if(eightballz == 8) { Serial.println("Yes!"); startover(); } if(eightballz == 9) { Serial.println("Signs point to yes!"); startover(); } if(eightballz == 10) { Serial.println("Reply hazy. Try again!"); startover(); } if(eightballz == 11) { Serial.println("Ask again later!"); startover(); } if(eightballz == 12) { Serial.println("Better not tell you now!"); startover(); } if(eightballz == 13) { Serial.println("Cannot predict now!"); startover(); } if(eightballz == 14) { Serial.println("Concentrate and ask again!"); startover(); } if(eightballz == 15) { Serial.println("Don't count on it!"); startover(); } if(eightballz == 16) { Serial.println("My reply is no!"); startover(); } if(eightballz == 17) { Serial.println("My sources say no!"); startover(); } if(eightballz == 18) { Serial.println("Outlook not so good!"); startover(); } if(eightballz == 19) { Serial.println("Very doubtful !"); startover(); } } void startover() // This function is called when you have no credits - 5 second delay { delay(5000); Serial.println("The MATGIC 8 BALL Program - Insert Coins & Ask Your Questions"); // Print this to the serial monitor. You'll need to open the serial monitor to see this. } void startoverb() // This function is calle dwhen you have no credits - 2 second delay { delay(2000); Serial.println("The MATGIC 8 BALL Program - Insert Coins & Ask Your Questions"); // Print this to the serial monitor. You'll need to open the serial monitor to see this. } // End of program