// Prize Wheel with Button Control and Exact Stop
// Pin definitions
#define stepPin 9
#define dirPin 8
#define buttonPin 2
int stepDelay = 1000; // Delay between steps (microseconds)
int targetSteps[] = {30, 60, 90, 120, 150}; // Target step positions for Eel, Albatross, Rooster, Turtle, Horse
int currentSpin = 0; // Track current spin
int randomSteps=0;
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
if (digitalRead(buttonPin) == LOW) { // When button is pressed
spinWheel();
delay(1000); // Debounce delay
}
}
void spinWheel() {
if (currentSpin == 2)
{
randomSteps = 400;
}
else
{
randomSteps = (random(1, 5) * 200); // Random complete rotations (200 steps per rotation)
}
int finalSteps = randomSteps + targetSteps[currentSpin];
digitalWrite(dirPin, HIGH); // Set direction
for (int i = 0; i < finalSteps; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
}
currentSpin = (currentSpin + 1) % 5; // Move to next position in sequence
}