Project showcase
Whack-A-Mole

Whack-A-Mole © GPL3+

I made a Whack-A-Mole for a cat using an Arduino with BLE. Source code and 3D printing data is available on GitHub.

  • 29 views
  • 0 comments
  • 0 respects

Components and supplies

Bluetooth Low Energy (BLE) Module (Generic)
×1
Ard nano
Arduino Nano R3
×1

Necessary tools and machines

3drag
3D Printer (generic)

Apps and online services

About this project


I made a Whack-A-Mole for a cat using an Arduino with BLE (Bluetooth Low Energy).

Three servomotors connected to the Arduino are controlled from a smartphone via Bluetooth.

Source code and 3D printing data is available on GitHub.

I used Arduino IDE 1.8.13 for Arduino development and Android Studio 4.0 for Android app development.

I used to focus on completing services inside the server and the smartphone, but this kind of IoT development is also interesting!

Code

whack_a_mole.inoC/C++
Three servomotors can be operated via BLE.
#include <ArduinoBLE.h>
#include <Servo.h>

const int ledPin = LED_BUILTIN; // Set ledPin to on-board LED
const int angleMin = 10;
const int angleMax = 100;
Servo servoA;
Servo servoB;
Servo servoC;

BLEService wamService("19B10010-E8F2-537E-4F6C-D104768A1214");

BLEByteCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEByteCharacteristic buttonCharacteristicA("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEByteCharacteristic buttonCharacteristicB("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEByteCharacteristic buttonCharacteristicC("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

void setup() {
  Serial.begin(9600);

  pinMode(ledPin, OUTPUT); // use the LED as an output

  // Initialization
  if (!BLE.begin()) {
    Serial.println("Starting BLE failed!");
    while (1);
  }

  // Set the local name peripheral advertises
  BLE.setLocalName("Whack A Mole");
  
  // Set the UUID for the service this peripheral advertises:
  BLE.setAdvertisedService(wamService);

  // Add the characteristics to the service
  wamService.addCharacteristic(ledCharacteristic);
  wamService.addCharacteristic(buttonCharacteristicA);
  wamService.addCharacteristic(buttonCharacteristicB);
  wamService.addCharacteristic(buttonCharacteristicC);

  // Add the service
  BLE.addService(wamService);

  ledCharacteristic.writeValue(0);
  buttonCharacteristicA.writeValue(0);
  buttonCharacteristicB.writeValue(0);
  buttonCharacteristicC.writeValue(0);

  // Start advertising
  BLE.advertise();

  // Set servos
  servoA.attach(5);
  servoB.attach(6);
  servoC.attach(7);
  servoA.write(angleMin);
  servoB.write(angleMin);
  servoC.write(angleMin);
  Serial.println("Bluetooth device active, waiting for connections...");
}

void loop() {
  // Poll for BLE events
  BLE.poll();

  if (ledCharacteristic.written()) {
    if (ledCharacteristic.value()) {
      Serial.println("LED on");
      digitalWrite(ledPin, HIGH);
    } else {
      Serial.println("LED off");
      digitalWrite(ledPin, LOW);
    }
  }
  
  if (buttonCharacteristicA.written()) {
    if (buttonCharacteristicA.value()) {
      Serial.println("ServoA on");
      digitalWrite(ledPin, HIGH);
      servoA.write(angleMax);
    } else {
      Serial.println("ServoA off");
      digitalWrite(ledPin, LOW);
      servoA.write(angleMin);
    }
  }
  
  if (buttonCharacteristicB.written()) {
    if (buttonCharacteristicB.value()) {
      Serial.println("ServoB on");
      digitalWrite(ledPin, HIGH);
      servoB.write(angleMax);
    } else {
      Serial.println("ServoB off");
      digitalWrite(ledPin, LOW);
      servoB.write(angleMin);
    }
  }
  
  if (buttonCharacteristicC.written()) {
    if (buttonCharacteristicC.value()) {
      Serial.println("ServoC on");
      digitalWrite(ledPin, HIGH);
      servoC.write(angleMax);
    } else {
      Serial.println("ServoC off");
      digitalWrite(ledPin, LOW);
      servoC.write(angleMin);
    }
  }
}

Custom parts and enclosures

Gear for SG90 Linear Actuator
Linear
Linear for SG90 Linear Actuator
Mount for SG90 Linear Actuator

Schematics

Whack-A-Mole
How to connect servomotors.
Circuit 04 qhl8fwubpo

Comments

Similar projects you might like

RCar | Robots for All!

Project tutorial by Luís Rita

  • 9,709 views
  • 2 comments
  • 44 respects

Arduino Based Mini CNC 2D Plotter

Project showcase by Mrinnovative

  • 117,487 views
  • 19 comments
  • 270 respects

Reef Controller

Project showcase by Shawn Leclair

  • 18,672 views
  • 0 comments
  • 25 respects

How To Control Arduino From Mobile Phone

Project showcase by lubosrusnak

  • 12,679 views
  • 0 comments
  • 13 respects

Android Controlled Toy Using Raspberry Motor Shield

Project tutorial by Volkan Sarıoğlu

  • 8,028 views
  • 6 comments
  • 24 respects

Infrared Remote Emulator

Project tutorial by TATCO

  • 6,854 views
  • 1 comment
  • 15 respects
Add projectSign up / Login