Introduction: Sad Cat Wack-A-Mole

A two-motor whack-A-mole game designed to entertain a cat.

I cannot grab the link, it won't appear on my monitor

Supplies

The list of materials are:

-4 220 ohm resistors

-2 servo motors

-2 LEDs

-an Arduino Uno

-wires, any colour is fine

Step 1: Setting Up the Board

First, begin by wiring the ground wire to the ground line, do the same with the 5V, black to black, red to red. Next bridge the wires so you can use either side of the terminal.

Step 2: Adding the Resistors and Lights

Next, add the 4 220 ohm resistors. Plug these directly into pins 3, 5, 9 and 10, these will be used later. Next, connect 2 wires to the resistors plugged into 9 and 10, leading these to any available spot on the breadboard. Once placed, add your LEDs, ensure that the cathode is pointing into the ground terminals.

Step 3: Finishing the Build

The final step is adding in the servo motors, these can be tricky if you've never used them before. My trick is to follow the colour coding of the wires. The orange is the furthest left wire, plus these into pin 3 and 5 resistors. The black wire leading into the right side is ground, leaving the middle red to be connected to power. Once all is attached, you can go on to the final coding stage.

Step 4: Coding the Setup

To begin the coding, add the servo library, initialize the servo motors and the pins. Make sure to connect them to the pins you used earlier in the process. You also need to set up the long variable for the randomizer, you will use long because it allows a much greater range of random numbers.


Next, you'll want to write

Serial.println("Starting new Random Number Sequence");

This will allow your readers and users of your code to understand what is happening in the serial monitor. Following this, designate your LEDs as outputs so you can have a visual of what's on or off. The next will line is the most important line of this code

 randomSeed(analogRead(A0));  

This may seem confusing, but this will read the background noise of the A0 pin allowing a true randomizer, rather than the consistent generation of the rand() command.


#include <Servo.h> //include the servo library 


//Declare and initialize LED pin variables
int LED_1 = 8; 
int LED_2 = 9;

Servo myservo; //setting up the servos and their names
Servo myservo1;
Servo myservo2;


long randomNumber; //this variable will store a random number 

void setup() //This code will only run once
{
  myservo.attach(3); //attaches the servo named "myservo" to pin 3
  myservo.write(0); //sets the starting angel to 0
  myservo1.attach(5);
  myservo1.write(0);

  Serial.begin(9600);   //setup serial communications through the USB
  
  Serial.println("Starting new Random Number Sequence"); //prints the message in the serial monitor


  //set the LED pins as outputs
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
      
  //Let's make it more random
  randomSeed(analogRead(A0));   
      
}


Step 5: Void Loop

The next step will be to finish the random number generator, set the numbers for the number you want to start on and the second being the number after you want it to end. So (9,11) would start on 9 and end on 10. This step is optional, but you can choose to have the randomizer print the text to the serial monitor so you can observe the randomly generated sequence.

Next, set digital write to set the randomized output as high, since this is connected to the Led, it will give you a visual of which it is. Choose your delay and then set it to turn the randomized output back off so it may begin again.

The final part of this program entails detecting which pin is outputting a high signal, depending on which one is active, which will randomize the servos. If one is on, the other is off for simplicity. If you'd like, you can endlessly expand the servo count by expanding the randomizer limits and adding new detections, but for simplicities sake, 2 will do.

void loop() 
{
  //generate a random number between 9 and 10, this is to connect it to those pins
  randomNumber = random(9,11);
  
  //display the random number on the serial monitor
  Serial.print("The Random Number is = ");
  delay(500); //delays the code by 500 milliseconds
  Serial.println(randomNumber); //prints the random numbers
  
  digitalWrite(randomNumber, HIGH); //sets the randomized pin to high
  
  delay(1500); //delays the code 1500 millisecond 
  
  digitalWrite(randomNumber, LOW); //turns off the randomly selected pin 

if (randomNumber == 9) //if the randomly chosen pin is pin 9...
  {
    myservo.write(90); //rotate the servo 90 degrees
  delay(1500); //wait 1500 milliseconds
  myservo.write(0); //return the servo to it's starting point 
  }
  
  if (randomNumber == 10) //if the randomly chosen pin is 10...
  {
  myservo1.write(90); //rotate the servo 90 degrees
  delay(1500); //wait 1500 milliseconds
  myservo1.write(0); //return the servo to it's starting point 
  }
}