Monday, February 9, 2015

New website

manojkunthu.com is now online. Check it out.

Saturday, September 14, 2013

DIY Guitar to GarageBand (gPort)

Devices like the iRig offer a nice way to feed your guitar signal to GarageBand to play with a plethora of amps and pedals.  There are also much more expensive alternatives, i.e. Apogee Jam or iRig HD, which communicate with GarageBand digitally, and consequently produce a higher quality recording.

I decided to look at the lower-end analog version and create my own device to connect to GarageBand....I'll call it gPort for now.

Research

First I found how audio jacks on Apple devices work. A quick search got me to TRRS Standards. Using the info, here's a sketch summarizing the findings.

Setup

The wiring for gPort is simple enough.  The device will require 3 components: a guitar jack, a headphone jack, and a 3.5 mm male audio-connector w/ mic.



I went with the following components from DigiKey:

Guitar Jack - SC1316-ND
Headphone Jack - CP1-3524NG-ND
3.5 mm audio connector cable - 839-1030-ND

Total for all 3 components came out to $8.11 at the time I purchased them.  The total could be brought down even lower if I did some extra work with the audio connector.

DIY Build

I trimmed the audio connector cable down from 6 feet to about a foot. Using the data sheet for each component, I soldered the connections and the gPort looked as shown below.


All I needed was a case.  What to do?...what to do?....handy dandy card board?...nah.  I've got a better idea.

I designed a case for gPort and sent the 3D model to Shapeways (http://shpws.me/pcrT).


Pictures of the final gPort device:




Notes

Including parts shipping (~ $9 =(2 w/ digikey, 7 with shapeways) the total cost for making a single gPort is about $25.

Some slight miscalculations with the 3D print caused some assembly problems, but I got it to fit regardless.  The case is not for sale, unless there's some interest.

So was as it worth it? ....yep.


Saturday, February 2, 2013

Arduino clapper Update

I've updated and improved the code for better readability and added some enhancements. It's now possible to change the number of claps in the variable declaration. It's also easier to alter the offset value for clap detection and manipulate the time allowed between each clap.

I modified the diagram as well by removing the enable button.

/*
* Clapper project
* Author: Manoj Kunthu
* Update: 2/2/13
*/

/*-----------------------------
*   Method Prototypes
*-----------------------------*/

void initialize();
void runDetector();
boolean clapDetected();

int detectClaps(int numClaps);
void indicateClaps();
void readMic();

void printStats();

/*-----------------------------
*   Variable Declarations
*-----------------------------*/

int TOTAL_CLAPS_TO_DETECT = 2; //The number of claps detected before output is toggled
int offset = 80;    // The point above average that the clap is detected
int CLAP_TIME=4000; // The time allowed between each clap


int sensorValue = 0; //the value read through mic 

int toggleOutput = -1;

int SIZE = 3;
int buffer[3];
int loopIteration = 0;
int average = 0;
int total = 0;



//BOARD INPUT MIC
const int inPin0 = A0; 

//BOARD OUTPUT SIGNALS
const int clapLed1 = 12, clapLed2 = 11, out = 10, readyPin = 13;

//CLAP STATE CONSTANTS
const int FINAL_DETECTED = 0, LOST_CONTINUITY = 1, CLAP_NOT_DETECTED = 2;

void setup() {
  Serial.begin(9600);
  
  //direct representation of the light bulb that toggles on/off  
  pinMode(out, OUTPUT);
  
  //once initialize() runs the ready pin turns on
  pinMode(readyPin, OUTPUT);
  
  //respective clap LEDs, more can be added
  pinMode(clapLed1, OUTPUT);
  pinMode(clapLed2, OUTPUT);
}


void loop() {
  initialize();
  runDetector();
}

/**
* Purpose: Prepares the buffer to recognize ambient noise levels in room.
*/
void initialize()
{
  loopIteration = 0; 
  total = 0;
  average =0;
  
  digitalWrite(clapLed1, LOW);
  digitalWrite(clapLed2, LOW);
  digitalWrite(out, LOW);
  
  for(int i = 0; i < SIZE; i++)
  {
    readMic();
    
    buffer[i] = sensorValue;
    total = total + sensorValue;
    average = (total/(i+1));
    
    Serial.print("INIT - AVE: ");
    Serial.print(average);
    Serial.print("    Total: ");
    Serial.print(total); 
    Serial.print("    Sensor: ");
    Serial.print(sensorValue); 
    Serial.print("    Change: ");
    Serial.println(sensorValue-average); 
      
    delay(50);
  }
  digitalWrite(readyPin, HIGH);
}

/**
* Purpose: Runs the detector algorithm. Developers can change the number of claps by adjusting TOTAL_CLAPS_TO_DETECT variable up at the top.
*/
void runDetector()
{
  while(true)
  {
    int clapState = detectClaps(TOTAL_CLAPS_TO_DETECT);
    
    if(clapState == FINAL_DETECTED || clapState == LOST_CONTINUITY)
    {
       Serial.println("--done--");
       indicateClap(0);//turn off any clap indicating lights
    }
  }
}

/**
* Purpose:  Detects the number of claps specified. This method is recursive
*/
int detectClaps(int numClaps)
{
  int clapNum = numClaps;
  
  //Base Case - if clapNum is 0, then all claps have been accounted.
  if(clapNum == 0)
  {
    //the output can now be toggled.
    toggleOutput *= -1;
    indicateClap(clapNum);
    
    Serial.println("-----  Clap Limit Reached - Output Toggled -----");
    
    return FINAL_DETECTED;
  }
  
  //Read from mic and update ambient noise levels.
  readMic();

  total = (total - buffer[loopIteration]) + sensorValue; 
  average = (total/SIZE);
  buffer[loopIteration] = sensorValue;
  
  loopIteration = (loopIteration+1)%SIZE;
  
  if(clapDetected())
  { 
    Serial.print("detectClaps - Claps:");
    Serial.println(TOTAL_CLAPS_TO_DETECT + 1 - numClaps); 
    
    printStats();
    indicateClap(clapNum);
    
    delay(100);
    for(int i = 0; i < CLAP_TIME; i++)
    {
      int clapState = detectClaps(clapNum - 1);   
      
      if(clapState == FINAL_DETECTED || clapState == LOST_CONTINUITY)
      {
         return clapState;
      }
    }
    return LOST_CONTINUITY;
  }
  return CLAP_NOT_DETECTED;
}

/**
* Purpose: Turns the LED on appropriately to signal a clap detection.
*/
void indicateClap(int clapNum)
{
  if(clapNum == 0)
  {
    if(toggleOutput == 1)
    {
      digitalWrite(out, HIGH);
    }
    else
    {
      digitalWrite(out, LOW);
    }
    digitalWrite(clapLed1, LOW);
    digitalWrite(clapLed2, LOW);
  }
  else if(clapNum == 1)
  {
     digitalWrite(clapLed1, HIGH);
  }
  else if(clapNum == 2)
  {
     digitalWrite(clapLed2, HIGH);
  }
  delay(110);
}

/**
* Purpose: Prints basic statistics data for more info with sensor readouts and data points.
*/
void printStats()
{
  Serial.print("--- AVE: ");
  Serial.print(average);
  Serial.print("    Total: ");
  Serial.print(total); 
  //Serial.print("    iterNum: ");
  //Serial.print(loopIteration); 
  Serial.print("    Sensor: ");
  Serial.print(sensorValue); 
  Serial.print("    Change: ");
  Serial.println(sensorValue-average); //This is what I used to determine the 'offset' value
}

/**
* Purpose:  A clap is detected when the sensor value is greater than the average plus 
*     an offset.  The offset might need to be fine tuned for different sound sensors.
*/
boolean clapDetected()
{
    return sensorValue > average + offset;
}

/**
* Purpose: Reads mic input and stores it in a global variable.
*/
void readMic()
{
  sensorValue = analogRead(inPin0);  
}


Thursday, January 19, 2012

Arduino clapper


This is my documentation of the arduino clapper project that I did in June 2011.  I apologize for the poor code readability, I was trying to make something work as quickly as possible.

The code below is old - Click for updated code: 2/2/13

If you have any doubts or something about the code/schematic isn't clear, just comment and I'll try to explain.  Once the concept/code flow becomes clear, it's easy to code.


/*

* Clapper project
* author: Manoj Kunthu
* last update: 6/5/11
*/


int clap = 1;
boolean enabled = true;
int sensorValue = 0;


int SIZE = 25;
int buffer[25];


int now = 0;
int average = 0;
int total = 0;
int offset = 30;
boolean initialized = false;


int out = 10;


const int inPin0 = A0; 
const int clapLed1 = 12;
const int clapLed2 = 11;


const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 3;       // the pin that the LED is attached to


// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(out, OUTPUT);
  
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  
  pinMode(clapLed1, OUTPUT);
  pinMode(clapLed2, OUTPUT);


  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      buttonPushCounter++;
    } 
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;


  if (buttonPushCounter % 2 == 0) {
    digitalWrite(ledPin, HIGH);
    
    enabled = true;
  } 
  else {
    digitalWrite(ledPin, LOW);
    digitalWrite(clapLed1, LOW);
    digitalWrite(clapLed2, LOW);
    enabled = false;
  }


  if(enabled){
    sensorValue = analogRead(inPin0); 


    //insert the sensor value into the array
    if(initialized){          
      total = total - buffer[now] + sensorValue;    
    }          
    else{
      total = total + sensorValue;


      if(now + 1 >= SIZE)
        initialized = true;
    }


    //Find the average room noise level
    if(initialized)
      average = (total/SIZE) + offset; 
    else
      average = (total/(now+1)) + offset;
    
    if (sensorValue > average)
    {
      Serial.print(" AVE: ");
      Serial.print(average);
      Serial.print("    Sensor: ");
      Serial.println(sensorValue);
    }


    if (sensorValue > average+30)
    {
      
      digitalWrite(clapLed1, HIGH);
      
      Serial.print(" AVE: ");
      Serial.print(average);
      Serial.print("    Sensor: ");
      Serial.println(sensorValue);
      //clap *= -1;
      Serial.println("---Single req");
      delay(100); 


      int inc;
      for(inc = 0; inc<2000; inc++)
      {
        sensorValue = analogRead(inPin0); 
        //insert the sensor value into the array
        if(initialized){          
          total = total - buffer[now] + sensorValue;    
        }          
        else{
          total = total + sensorValue;


          if(now + 1 >= SIZE)
            initialized = true;
        }


        //Find the average room noise level
        if(initialized)
          average = (total/SIZE) + offset; 
        else
          average = (total/(now+1)) + offset;


        if (sensorValue > average)
        {
          digitalWrite(clapLed2, HIGH);
          Serial.print("--- AVE: ");
          Serial.print(average);
          Serial.print("    Sensor: ");
          Serial.println(sensorValue);
          clap *= -1;
          Serial.println("--------Double Clap");


          if(clap == 1){
            digitalWrite(out, HIGH);   // set the LED on 
          }
          else{
            digitalWrite(out, LOW);   // set the LED off
          }
          delay(1000); 
          break;
        }
        buffer[now] = sensorValue; 
        now = (now+1)%SIZE;
      }
      Serial.println("---Single ack");
      delay(500);
      Serial.println("done");
    }
    buffer[now] = sensorValue; 
    now = (now+1)%SIZE;


    if(average < 0 || average > 800)
    {
      initialized = false;
      
      for(now = 0;now < SIZE; now++)
      {
       buffer[now] = 0;
      }
      
      digitalWrite(out, LOW);
      total = 0;
      now = 0; 
      average =0;
      Serial.print(average);
      Serial.println("RESET");


    }
  }
  else
  {
    digitalWrite(3, LOW);
    Serial.println("disabled ");
  }
  digitalWrite(clapLed1, LOW);
  digitalWrite(clapLed2, LOW);
}

Here's the schematic:
(Note: Vcc in the arduino schematic refers to the 5V supplied from left pin and same with GND)