//Fortune teller Code // Code snippets from Adafruit tutorials // Cross fader from https://www.arduino.cc/en/Tutorial/ColorCrossfader //Pin declaration // LED lights int backWhitePin = 4; int fortuneLightPin = 5; // RGB LED int redPin = 11; int greenPin = 10; int bluePin = 9; // Ultrasonic distance int pingPin = 2; int echoPin = 3; //Motor Driver int enablePin = 6; int motorPin = 12; //Audio ISD1820 module int voicePlay = 7; // Variable Declaration int randTime = 0; // Time to spin the fortune wheel int wait = 2; // Time inbetween the color fades in crystal ball int triggerDistance = 5; // Trigger distance in CM long duration, cm; // Variables for distance sensor // Color arrays for the RGB Transitions int black[3] = { 0, 0, 0 }; int white[3] = { 100, 100, 100 }; int red[3] = { 100, 0, 0 }; int green[3] = { 0, 100, 0 }; int blue[3] = { 0, 0, 100 }; int yellow[3] = { 40, 95, 0 }; int dimWhite[3] = { 30, 30, 30 }; // Set initial color int redVal = black[0]; int grnVal = black[1]; int bluVal = black[2]; // Initialize color variables int prevR = redVal; int prevG = grnVal; int prevB = bluVal; void setup() { // SET all pins as Output pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); pinMode(pingPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(motorPin, OUTPUT); pinMode(enablePin, OUTPUT); pinMode(voicePlay, OUTPUT); pinMode(backWhitePin, OUTPUT); pinMode(fortuneLightPin, OUTPUT); //Ensure the LEDS are switched off digitalWrite(backWhitePin, LOW); digitalWrite(fortuneLightPin, LOW); } void loop() { //Initialize digitalWrite(backWhitePin, LOW); //Operate the distance measure digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(10); digitalWrite(pingPin, LOW); // Calculate the ditance duration = pulseIn(echoPin, HIGH); cm = microsecondsToCentimeters(duration); delay(100); // So that the variables are loaded if (cm < triggerDistance) { // This means the mechanism is triggered to play //Play the voice digitalWrite(voicePlay, HIGH); delay(100); digitalWrite(voicePlay, LOW); //Turn on the background LED digitalWrite(backWhitePin, HIGH); //Crystal ball color fades crossFade(red); crossFade(green); crossFade(blue); crossFade(yellow); crossFade(white); //Set Motor PWM to high speed int speed =200; //Run Motor setMotor(speed, 1); // Get a random time between 0.5 Second to 1 Second randTime= random(500,1000); // Run motor for that random duration delay (randTime); //Reduce speed speed =150; //Run motor setMotor(speed, 1); // Get a random time between 0.2 Second to 0.5 Second randTime= random(200,500); delay (randTime); //Reduce speed still further and stop speed =80; setMotor(speed, 1); setMotorStop(); // Turn on the light on the firtune wheel digitalWrite(fortuneLightPin, HIGH); //Turn off the crystam ball crossFade(black); //Wait for 2 seconds delay(2000); //Turn off the lights digitalWrite(fortuneLightPin, LOW); } } void setColor(int red, int green, int blue) { analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue); } void setMotor(int speed, boolean Forward) { analogWrite(enablePin, speed); digitalWrite(motorPin, Forward); } void setMotorStop() { digitalWrite(motorPin, 0); } long microsecondsToInches(long microseconds) { return microseconds / 74 / 2; } long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; } /* BELOW THIS LINE IS THE MATH -- YOU SHOULDN'T NEED TO CHANGE THIS FOR THE BASICS * * The program works like this: * Imagine a crossfade that moves the red LED from 0-10, * the green from 0-5, and the blue from 10 to 7, in * ten steps. * We'd want to count the 10 steps and increase or * decrease color values in evenly stepped increments. * Imagine a + indicates raising a value by 1, and a - * equals lowering it. Our 10 step fade would look like: * * 1 2 3 4 5 6 7 8 9 10 * R + + + + + + + + + + * G + + + + + * B - - - * * The red rises from 0 to 10 in ten steps, the green from * 0-5 in 5 steps, and the blue falls from 10 to 7 in three steps. * * In the real program, the color percentages are converted to * 0-255 values, and there are 1020 steps (255*4). * * To figure out how big a step there should be between one up- or * down-tick of one of the LED values, we call calculateStep(), * which calculates the absolute gap between the start and end values, * and then divides that gap by 1020 to determine the size of the step * between adjustments in the value. */ int calculateStep(int prevValue, int endValue) { int step = endValue - prevValue; // What's the overall gap? if (step) { // If its non-zero, step = 1020/step; // divide by 1020 } return step; } /* The next function is calculateVal. When the loop value, i, * reaches the step size appropriate for one of the * colors, it increases or decreases the value of that color by 1. * (R, G, and B are each calculated separately.) */ int calculateVal(int step, int val, int i) { if ((step) && i % step == 0) { // If step is non-zero and its time to change a value, if (step > 0) { // increment the value if step is positive... val += 1; } else if (step < 0) { // ...or decrement it if step is negative val -= 1; } } // Defensive driving: make sure val stays in the range 0-255 if (val > 255) { val = 255; } else if (val < 0) { val = 0; } return val; } /* crossFade() converts the percentage colors to a * 0-255 range, then loops 1020 times, checking to see if * the value needs to be updated each time, then writing * the color values to the correct pins. */ void crossFade(int color[3]) { // Convert to 0-255 int R = (color[0] * 255) / 100; int G = (color[1] * 255) / 100; int B = (color[2] * 255) / 100; int stepR = calculateStep(prevR, R); int stepG = calculateStep(prevG, G); int stepB = calculateStep(prevB, B); for (int i = 0; i <= 1020; i++) { redVal = calculateVal(stepR, redVal, i); grnVal = calculateVal(stepG, grnVal, i); bluVal = calculateVal(stepB, bluVal, i); setColor (redVal,grnVal,bluVal); delay(wait); // Pause for 'wait' milliseconds before resuming the loop } // Update current values for next loop prevR = redVal; prevG = grnVal; prevB = bluVal; }