drums with arduino - nyu tandon school of...
TRANSCRIPT
• Drums are one of the most popular
percussion instruments and the
design has remained unchanged for
ages
• Traditionally, hands or sticks are
used to play it
• The goal of this project is to
develop automate the drums using an
Arduino with the iPhone as the GUI.
INTRODUCTION
4/29/2015 2
• A wooden frame is build
around the drums to act as a
supporting system for the servos.
• The servos are used to move
the drumsticks.
• The Arduino is used to receive
the signals and actuate the
servos.
Construction and the Hardware
4/29/2015 4
• A WIFLY shield is used to
receive the signals from the
iPhone and send it to the
Arduino.
• An iPhone app is used as
the GUI to play the drums.
Construction and the Hardware
4/29/2015 5
The X code (1/3)
4/29/2015 6
//
// ViewController.m
// sending data test
//
// Created by Prasanth on 2/9/15.
// Copyright (c) 2015 Prasanth. All rights reserved.
//
#import "ViewController.h"
#import <arpa/inet.h> // for IPPROTO_TCP
#include <netinet/tcp.h> // for TCP_NODELAY
@interface ViewController ()
@end
@implementation ViewController
@synthesize connectButton;
@synthesize disconnectButton;
@synthesize pingButton;
@synthesize statusLabel;
@synthesize ss1;
@synthesize ss2;
@synthesize hat;
@synthesize snare;
@synthesize doubleSnare;
NSString *host = @"192.168.1.111";
UInt16 port = 8080;
int drum = 0;
- (void)viewDidLoad {
[super viewDidLoad];
pingButton.hidden = true;
ss1.hidden = true;
ss2.hidden = true;
hat.hidden = true;
snare.hidden = true;
doubleSnare.hidden = true;
socket = [[AsyncSocket alloc] initWithDelegate:self];
disconnectButton.hidden = true;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {
NSLog(@"CONNECTED!");
CFSocketRef cfsock = [sock getCFSocket];
CFSocketNativeHandle rawsock = CFSocketGetNative(cfsock);
int flag = 1;
int result = setsockopt(rawsock, IPPROTO_TCP, TCP_NODELAY,
The X code (2/3)
4/29/2015 7
(char *)&flag, sizeof(int));
if (result != 0)
NSLog(@"Could Not Disable Nagle...");
else
NSLog(@"Nagle Is Disabled.");
statusLabel.text = @"Connected";
connectButton.hidden = true;
disconnectButton.hidden = false;
pingButton.hidden = false;
ss1.hidden = false;
ss2.hidden = false;
hat.hidden = false;
snare.hidden = false;
doubleSnare.hidden = false;
}
- (void)onSocketDidDisconnect:(AsyncSocket *)sock {
NSLog(@"DISCONNECTED!");
statusLabel.text = @"Disconnected";
disconnectButton.hidden = true;
connectButton.hidden = false;
pingButton.hidden = true;
ss1.hidden = true;
ss2.hidden = true;
hat.hidden = true;
snare.hidden = true;
doubleSnare.hidden = true;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)connectButtonPressed:(id)sender {
NSLog(@"Connecting...");
[socket connectToHost:(NSString *)host onPort:(UInt16)port error:(NSError **)-1];
}
- (IBAction)disconnectButtonPressed:(id)sender {
[socket disconnect];
}
- (IBAction)pingButtonPressed:(id)sender {
drum = 5; //kick
[self sendMotorReference];
}
-(void)sendMotorReference {
if ( drum == 1) { //ss1
refString = [NSString stringWithFormat:@"d"];
}
else if (drum == 2)
{ //ss2
refString = [NSString stringWithFormat:@"e"];
}
else if (drum == 3)
{ //hat
The X code (3/3)
4/29/2015 8
refString = [NSString stringWithFormat:@"b"];
}
else if (drum == 4)
{ // snare
refString = [NSString stringWithFormat:@"c"];
}
else if (drum == 5)
{ //kick
refString = [NSString stringWithFormat:@"a"];
}
else if (drum == 6)
{ //double
refString = [NSString stringWithFormat:@"f"];
}
NSLog(@"%@", refString);
refData = [refString dataUsingEncoding: NSUTF8StringEncoding];
[socket writeData:refData withTimeout:-1 tag:1];
}
- (IBAction)ss1Pressed:(id)sender {
drum = 1; //small snare 1
[self sendMotorReference];
}
- (IBAction)ss2Pressed:(id)sender {
drum = 2; //small snare 2
[self sendMotorReference];
}
- (IBAction)HatPressed:(id)sender {
drum = 3; //hats
[self sendMotorReference];
}
- (IBAction)snarePressed:(id)sender {
drum = 4; //big snare
[self sendMotorReference];
}
- (IBAction)doubleSnarePressed:(id)sender {
drum = 6; //double beat
[self sendMotorReference];
}
@end
The Arduino code (1/3)
4/29/2015 9
#include <SPI.h>
#include "WiFly.h"
#include<Servo.h>
#include <String.h>
char c;
WiFlyServer server(8080);
Servo loneRanger;
Servo hat;
Servo kick;
Servo bigSnare;
Servo smallSnare1;
Servo smallSnare2;
int tempo = 150 ;
void setup()
{
// put your setup code here, to run once:
WiFly.begin();
loneRanger.attach(9);
hat.attach(3);
kick.attach(4);
bigSnare.attach(5);
smallSnare1.attach(2);
smallSnare2.attach(6);
loneRanger.write(90);
hat.write(10);
kick.write(120);
bigSnare.write(90);
smallSnare1.write(160);
smallSnare2.write(130);
Serial.begin(9600);
Serial.print("IP: ");
Serial.println(WiFly.ip());
server.begin();
}
void loop()
{
// put your main code here, to run repeatedly:
WiFlyClient client = server.available();
//int dataFlag = 0;
//int messageIsolator = 0;
if(client){
//Serial.println("Connected to User!");
while(client.connected())
{
//Serial.println("Here after client.connected()");
if(client.available())
{
c = client.read();
Serial.println(c);
if ( c == 'a')
{
kickHit();
}
else if ( c == 'b')
{
The Arduino code (2/3)
4/29/2015 10
hatHit();
}
else if ( c == 'c')
{
bigSnareHit();
}
else if ( c == 'd')
{
smallSnare1Hit();
}
else if ( c == 'e')
{
smallSnare2Hit();
}
else if ( c == 'f')
{
loneRangerHit();
}
else
{
Serial.println("Unknown Command!");
}
The Arduino code (3/3)
4/29/2015 11
}
}
}
}
void kickHit(){
kick.write(110);
delay(tempo);
kick.write(120);
delay(tempo);
}
void hatHit(){
hat.write(25);
delay(tempo);
hat.write(10);
delay(tempo);
}
void bigSnareHit(){
bigSnare.write(110);
delay(tempo);
bigSnare.write(90);
delay(tempo);
}
void smallSnare1Hit(){
smallSnare1.write(145);
delay(tempo);
smallSnare1.write(160);
delay(tempo);
}
void smallSnare2Hit(){
smallSnare2.write(110);
delay(tempo);
smallSnare2.write(130);
delay(tempo);
}
void loneRangerHit(){
loneRanger.write(120);
delay(tempo + 50);
loneRanger.write(60);
delay(tempo + 50);
loneRanger.write(90);
delay(tempo);
}