My stupid pet trick is similar to a prize wheel. The difference is that, instead of being a full circle, my prize wheel is a semi-circle, since the servo motor can spin for a maximum of 180 degrees. Depending on how much the user is willing to input, the speed of spinning and the time of spinning will be different. Here’s an illustration of my circuit for the prize wheel.
Three photocells are put in parallel on the breadboard. They serve as the input sensors for the prize wheel. The main idea is to ask users to put coins on the photocells to launch the prize wheel. Users can decide to put one, two or three coins. If the user puts one coin for the prize wheel, the coin will block light for one of the photocells. Then the prize wheel will launch with speed and time corresponding to the input of one coin. The situations are similar when the user inputs two coins or three coins. The change of lighting condition on the photocells will trigger different responses from the prize wheel. After the arrow on the wheel spins for enough time, it would stop at a random place between 0 and 180 degrees. Of course, the place that the arrow stops is related to the input from the user. The more coins the user inputs, the more likely the arrow is to stop at a higher amount of money. An illustration video, along with my code, is attached below.
#include <Servo.h> Servo myservo; // create servo object to control a servo const int sensorPin1 = A0; // pin that the sensor 1 is attached to const int sensorPin2 = A1; // pin that the sensor 2 is attached to const int sensorPin3 = A2; // pin that the sensor 3 is attached to // variables: int sensorValue1 = 0; // the sensor 1 value int sensorValue2 = 0; // the sensor 2 value int sensorValue3 = 0; // the sensor 3 value int sensorMin = 1023; // minimum sensor value int sensorMax = 0; // maximum sensor value int coin_num; void setup() { pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); // turn on LED to signal the start of the calibration period: pinMode(13, OUTPUT); digitalWrite(13, HIGH); Serial.begin(9600); // calibrate during the first five seconds while (millis() < 5000) { sensorValue1 = analogRead(sensorPin1); sensorValue2 = analogRead(sensorPin2); sensorValue3 = analogRead(sensorPin3); // record the maximum sensor value 1 if (sensorValue1 > sensorMax) { sensorMax = sensorValue1; } // record the minimum sensor value 1 if (sensorValue1 < sensorMin) { sensorMin = sensorValue1; } // record the maximum sensor value 2 if (sensorValue2 > sensorMax) { sensorMax = sensorValue2; } // record the minimum sensor value 2 if (sensorValue2 < sensorMin) { sensorMin = sensorValue2; } // record the maximum sensor value 3 if (sensorValue3 > sensorMax) { sensorMax = sensorValue3; } // record the minimum sensor value 3 if (sensorValue3 < sensorMin) { sensorMin = sensorValue3; } } // signal the end of the calibration period digitalWrite(13, LOW); //servo part begins myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { myservo.write(0); //reset the servo Serial.println("Servo reset"); digitalWrite(2, LOW); //turn LED low digitalWrite(3, LOW); //turn LED low digitalWrite(4, LOW); //turn LED low // read the sensor: sensorValue1 = analogRead(sensorPin1); sensorValue2 = analogRead(sensorPin2); sensorValue3 = analogRead(sensorPin3); // apply the calibration to the sensor reading sensorValue1 = map(sensorValue1, sensorMin, sensorMax, 0, 255); sensorValue2 = map(sensorValue2, sensorMin, sensorMax, 0, 255); sensorValue3 = map(sensorValue3, sensorMin, sensorMax, 0, 255); // in case the sensor value is outside the range seen during calibration sensorValue1 = constrain(sensorValue1, 0, 255); sensorValue2 = constrain(sensorValue2, 0, 255); sensorValue3 = constrain(sensorValue3, 0, 255); Serial.println(sensorValue1); Serial.println(sensorValue2); Serial.println(sensorValue3); //determine the number of coins if (sensorValue1 < 100) { coin_num = 1; } if (sensorValue1 < 100 && sensorValue2 < 100) { coin_num = 2; } if (sensorValue1 < 100 && sensorValue2 < 100 && sensorValue3 < 100) { coin_num = 3; } switch (coin_num) { //if one coin is inserted case 1: myservo.write(0); Serial.println("Coin 1 starts"); //light up one LED digitalWrite(2, HIGH); //launch the servo for one round for (int round_num = 0; round_num < 1; round_num += 1) { Serial.println(round_num); //first half circle in each round for (int i = 0; i < 185; i += 5) { myservo.write(i); delay(100); } //second half circle in each round for (int j = 180; j > -5; j -= 5) { myservo.write(j); delay(100); } } //make the servo stop myservo.write(random(0, 180)); delay(2000); break; //if two coins are inserted case 2: myservo.write(0); Serial.println("Coin 2 starts"); digitalWrite(2, HIGH); digitalWrite(3, HIGH); //launch the servo for two rounds for (int round_num = 0; round_num < 2; round_num += 1) { //first half circle in each round for (int i = 0; i < 200; i += 20) { myservo.write(i); delay(200); } //second half circle in each round for (int j = 180; j > -20; j -= 20) { myservo.write(j); delay(200); } } //make the servo stop myservo.write(0); myservo.write(random(30, 150)); delay(2000); break; case 3: myservo.write(0); Serial.println("Coin 3 starts"); digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, HIGH); //launch the servo for three rounds for (int round_num = 0; round_num < 3; round_num += 1) { //first half circle in each round for (int i = 0; i < 200; i += 20) { myservo.write(i); delay(112); } //second half circle in each round for (int j = 180; j > -20; j -= 20) { myservo.write(j); delay(112); } } //make the servo stop myservo.write(0); myservo.write(random(60, 120)); delay(2000); break; } }