Arduino mobile drum that can be controlled over Bluetooth without any wires

 

arduino bluetooth drum


#include <SoftwareSerial.h>

// Define the pins used for the drum pads

#define SNARE_PIN 2

#define TOM1_PIN 3

#define TOM2_PIN 4

#define CRASH_PIN 5

#define RIDE_PIN 6

#define HIHAT_PIN 7

// Define the baud rate used for Bluetooth communication

#define BAUD_RATE 9600

// Define the pins used for the Bluetooth module

#define BT_RX_PIN 8

#define BT_TX_PIN 9

// Create a new SoftwareSerial object for Bluetooth communication

SoftwareSerial btSerial(BT_RX_PIN, BT_TX_PIN);

void setup() {

  // Set the pinMode for the drum pads

  pinMode(SNARE_PIN, INPUT_PULLUP);

  pinMode(TOM1_PIN, INPUT_PULLUP);

  pinMode(TOM2_PIN, INPUT_PULLUP);

  pinMode(CRASH_PIN, INPUT_PULLUP);

  pinMode(RIDE_PIN, INPUT_PULLUP);

  pinMode(HIHAT_PIN, INPUT_PULLUP);

  

  // Start the serial communication with the computer

  Serial.begin(BAUD_RATE);

  

  // Start the serial communication with the Bluetooth module

  btSerial.begin(BAUD_RATE);

}


void loop() {

  // Read the state of each drum pad and send it over Bluetooth

  sendDrumState("snare", digitalRead(SNARE_PIN));

  sendDrumState("tom1", digitalRead(TOM1_PIN));

  sendDrumState("tom2", digitalRead(TOM2_PIN));

  sendDrumState("crash", digitalRead(CRASH_PIN));

  sendDrumState("ride", digitalRead(RIDE_PIN));

  sendDrumState("hihat", digitalRead(HIHAT_PIN));

  

  // Wait a bit to avoid overwhelming the Bluetooth module

  delay(100);

}


void sendDrumState(String drum, int state) {

  // Create a string with the drum name and state

  String message = drum + ":" + String(state);

  

  // Send the message over Bluetooth

  btSerial.println(message);

  

  // Print the message to the serial monitor for debugging

  Serial.println(message);

}

This code uses the SoftwareSerial library to create a new serial port on pins 8 and 9, which are connected to a Bluetooth module. The Arduino reads the state of each drum pad and sends it over Bluetooth to a mobile device. The mobile device can then use this information to trigger drum sounds or perform other actions.

Note that this code assumes that the drum pads are connected to ground and the corresponding input pins are pulled up with internal resistors. When a drum pad is hit, it will pull the input pin low and the Arduino will read a digital value of 0. If your setup is different, you may need to modify the code accordingly.

No comments

Powered by Blogger.