Components and supplies
Arduino Nano R3
Bluetooth Low Energy (BLE) Module (Generic)
Tools and machines
3D Printer (generic)
Apps and platforms
Android Studio
Project description
Code
whack_a_mole.ino
c_cpp
Three servomotors can be operated via BLE.
1#include <ArduinoBLE.h> 2#include <Servo.h> 3 4const int ledPin = LED_BUILTIN; // Set ledPin to on-board LED 5const int angleMin = 10; 6const int angleMax = 100; 7Servo servoA; 8Servo servoB; 9Servo servoC; 10 11BLEService wamService("19B10010-E8F2-537E-4F6C-D104768A1214"); 12 13BLEByteCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); 14BLEByteCharacteristic buttonCharacteristicA("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); 15BLEByteCharacteristic buttonCharacteristicB("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); 16BLEByteCharacteristic buttonCharacteristicC("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); 17 18void setup() { 19 Serial.begin(9600); 20 21 pinMode(ledPin, OUTPUT); // use the LED as an output 22 23 // Initialization 24 if (!BLE.begin()) { 25 Serial.println("Starting BLE failed!"); 26 while (1); 27 } 28 29 // Set the local name peripheral advertises 30 BLE.setLocalName("Whack A Mole"); 31 32 // Set the UUID for the service this peripheral advertises: 33 BLE.setAdvertisedService(wamService); 34 35 // Add the characteristics to the service 36 wamService.addCharacteristic(ledCharacteristic); 37 wamService.addCharacteristic(buttonCharacteristicA); 38 wamService.addCharacteristic(buttonCharacteristicB); 39 wamService.addCharacteristic(buttonCharacteristicC); 40 41 // Add the service 42 BLE.addService(wamService); 43 44 ledCharacteristic.writeValue(0); 45 buttonCharacteristicA.writeValue(0); 46 buttonCharacteristicB.writeValue(0); 47 buttonCharacteristicC.writeValue(0); 48 49 // Start advertising 50 BLE.advertise(); 51 52 // Set servos 53 servoA.attach(5); 54 servoB.attach(6); 55 servoC.attach(7); 56 servoA.write(angleMin); 57 servoB.write(angleMin); 58 servoC.write(angleMin); 59 Serial.println("Bluetooth device active, waiting for connections..."); 60} 61 62void loop() { 63 // Poll for BLE events 64 BLE.poll(); 65 66 if (ledCharacteristic.written()) { 67 if (ledCharacteristic.value()) { 68 Serial.println("LED on"); 69 digitalWrite(ledPin, HIGH); 70 } else { 71 Serial.println("LED off"); 72 digitalWrite(ledPin, LOW); 73 } 74 } 75 76 if (buttonCharacteristicA.written()) { 77 if (buttonCharacteristicA.value()) { 78 Serial.println("ServoA on"); 79 digitalWrite(ledPin, HIGH); 80 servoA.write(angleMax); 81 } else { 82 Serial.println("ServoA off"); 83 digitalWrite(ledPin, LOW); 84 servoA.write(angleMin); 85 } 86 } 87 88 if (buttonCharacteristicB.written()) { 89 if (buttonCharacteristicB.value()) { 90 Serial.println("ServoB on"); 91 digitalWrite(ledPin, HIGH); 92 servoB.write(angleMax); 93 } else { 94 Serial.println("ServoB off"); 95 digitalWrite(ledPin, LOW); 96 servoB.write(angleMin); 97 } 98 } 99 100 if (buttonCharacteristicC.written()) { 101 if (buttonCharacteristicC.value()) { 102 Serial.println("ServoC on"); 103 digitalWrite(ledPin, HIGH); 104 servoC.write(angleMax); 105 } else { 106 Serial.println("ServoC off"); 107 digitalWrite(ledPin, LOW); 108 servoC.write(angleMin); 109 } 110 } 111} 112
whack_a_mole.ino
c_cpp
Three servomotors can be operated via BLE.
1#include <ArduinoBLE.h> 2#include <Servo.h> 3 4const int ledPin 5 = LED_BUILTIN; // Set ledPin to on-board LED 6const int angleMin = 10; 7const 8 int angleMax = 100; 9Servo servoA; 10Servo servoB; 11Servo servoC; 12 13BLEService 14 wamService("19B10010-E8F2-537E-4F6C-D104768A1214"); 15 16BLEByteCharacteristic 17 ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); 18BLEByteCharacteristic 19 buttonCharacteristicA("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); 20BLEByteCharacteristic 21 buttonCharacteristicB("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); 22BLEByteCharacteristic 23 buttonCharacteristicC("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); 24 25void 26 setup() { 27 Serial.begin(9600); 28 29 pinMode(ledPin, OUTPUT); // use the 30 LED as an output 31 32 // Initialization 33 if (!BLE.begin()) { 34 Serial.println("Starting 35 BLE failed!"); 36 while (1); 37 } 38 39 // Set the local name peripheral 40 advertises 41 BLE.setLocalName("Whack A Mole"); 42 43 // Set the UUID for 44 the service this peripheral advertises: 45 BLE.setAdvertisedService(wamService); 46 47 48 // Add the characteristics to the service 49 wamService.addCharacteristic(ledCharacteristic); 50 51 wamService.addCharacteristic(buttonCharacteristicA); 52 wamService.addCharacteristic(buttonCharacteristicB); 53 54 wamService.addCharacteristic(buttonCharacteristicC); 55 56 // Add the service 57 58 BLE.addService(wamService); 59 60 ledCharacteristic.writeValue(0); 61 buttonCharacteristicA.writeValue(0); 62 63 buttonCharacteristicB.writeValue(0); 64 buttonCharacteristicC.writeValue(0); 65 66 67 // Start advertising 68 BLE.advertise(); 69 70 // Set servos 71 servoA.attach(5); 72 73 servoB.attach(6); 74 servoC.attach(7); 75 servoA.write(angleMin); 76 servoB.write(angleMin); 77 78 servoC.write(angleMin); 79 Serial.println("Bluetooth device active, waiting 80 for connections..."); 81} 82 83void loop() { 84 // Poll for BLE events 85 86 BLE.poll(); 87 88 if (ledCharacteristic.written()) { 89 if (ledCharacteristic.value()) 90 { 91 Serial.println("LED on"); 92 digitalWrite(ledPin, HIGH); 93 94 } else { 95 Serial.println("LED off"); 96 digitalWrite(ledPin, 97 LOW); 98 } 99 } 100 101 if (buttonCharacteristicA.written()) { 102 if 103 (buttonCharacteristicA.value()) { 104 Serial.println("ServoA on"); 105 digitalWrite(ledPin, 106 HIGH); 107 servoA.write(angleMax); 108 } else { 109 Serial.println("ServoA 110 off"); 111 digitalWrite(ledPin, LOW); 112 servoA.write(angleMin); 113 114 } 115 } 116 117 if (buttonCharacteristicB.written()) { 118 if (buttonCharacteristicB.value()) 119 { 120 Serial.println("ServoB on"); 121 digitalWrite(ledPin, HIGH); 122 123 servoB.write(angleMax); 124 } else { 125 Serial.println("ServoB off"); 126 127 digitalWrite(ledPin, LOW); 128 servoB.write(angleMin); 129 } 130 } 131 132 133 if (buttonCharacteristicC.written()) { 134 if (buttonCharacteristicC.value()) 135 { 136 Serial.println("ServoC on"); 137 digitalWrite(ledPin, HIGH); 138 139 servoC.write(angleMax); 140 } else { 141 Serial.println("ServoC off"); 142 143 digitalWrite(ledPin, LOW); 144 servoC.write(angleMin); 145 } 146 } 147} 148
Downloadable files
Whack-A-Mole
How to connect servomotors.
Whack-A-Mole
Whack-A-Mole
How to connect servomotors.
Whack-A-Mole
Documentation
Gear
Gear for SG90 Linear Actuator
Gear
Mount
Mount for SG90 Linear Actuator
Mount
Mount
Mount for SG90 Linear Actuator
Mount
Linear
Linear for SG90 Linear Actuator
Linear
Gear
Gear for SG90 Linear Actuator
Gear
Comments
Only logged in users can leave comments
toyonaga
0 Followers
•0 Projects
Table of contents
Intro
1
0