Need help with programming arduinos and C++

djccfm

Member
Joined
Aug 17, 2010
Messages
0
Hi everyone,

I'm Chris

I'm not actually a speed solver... i just solve puzzles as a small past time.

I'm a Good friend of the host of this site (Pat) and I asked him for some help programming an Arduino, he said that maybe this community could help.

So here goes:

The project is to replicate Mathew Bellamays (from the band Muse) signature guitars that have a built in touch pad. Image here
The touch pad controls a piece of audio sampling hardware called a KAOSS PAD(KP2 in my case) and it does this via MIDI, the Kaoss pad already has a touch pad built in, along with all the other features I wish to place in my guitar. the point of this is to access all the pads features via midi commands from analogue inputs in my guitar(the touch pad, momentary switch, and a rotary encoder for the effect selection.) as the pad is fairly unusable to a guitarist in its regular state.

I am not the first to want to do this, in fact I know of 2 people who have already done this, here are their videos on youtube:

bassman76jazz


gazlang81


Both examples are made in almost an identical way, although I prefer bassmans approach as although he hasnt revealed much, his came after gazlang and he told me some of the improvements he did on gaz's design.

The overview of the project is:

-To convert an analogue input of a resistive touch screen into midi output signals that the kaoss pad will read.
-To add a Hold Button on the guitar that does the same as the hold button on the pad
-To add a rotary encoder that will allow me to change the effects of the pad on the guitar.
-Have a LED backlight that has a color pattern when not used, but then is responsive to the input of the touch screen.

In the next few steps I'll explain how far I've gotten.

To convert the input signals and to regulate the LED effects I will do the same as the 2 guys I mentioned and I will use a Arduino, I have chosen the Arduino mini pro as it was bassman's recomendation. It runs on C++ (a language I know very little about) bassman has given me some tips over email about how the circuit board should be wired etc... so here is an extract of his email to me, and the schematics I have sort of done so far.

Email:
there is not really anything to the circuit, its just the screens four wires into the analog inputs of the arduino, the switch goes from ground to a digital input and the encoder connects to pin 2 and 4 (the interrupt pins) on the arduino the midi jack uses the tx pin and a ground and +5v pins and the led is connected to +5v and three pwm output pins. the pin numbers are arbitrary since they are controlled via the firmware. there are no extra chips, its just the arduino to control everything, the kit that gazlang81 makes uses the arduino mini, mine is the mini pro. as for the c code for the arduino I'm not willing to give this up just yet.

Schematics: (bare in mind I doubt very much that these are right)


If the wires are in the wrong place or not connected at all, Its because I literally don't know any better or where to put them.

here are the components I'd like to use:

for the LED I'd like to use a BlinkM smart LED

The Arduino mini pro

nintendo DS lite touch pad

For the other components I don't know any specific things to buy etc...

Thats all I know about the hardware I need and how to build it.

now for the software for the arduino, I am literally clueless, although I have had somewhat experience with computer languages, I know close to nothing about C++. But what I have is a list of codes that will prove helpful to anyone who does know about C++

here are the links:

Resistive strip midi input for the digitech whammy pedal:

programing a touch pad as a midi input:

here is the manual for the Kaoss pad, In page 12 it has the midi chart(that will tell you what command each midi value is associated with).

Kaoss pad manual.pdf

This is as far as I've gotten and all I know.

Also im not sureIon how to program the LED but i think that might be much simpler

What I would like to achieve is the same that those 2 guys achieved in their videos. Thankfully the rest I can do and I'm quite good at. I'd appreciate any help at all and thank you very much for your attention whilst reading this.

many thanks

Chris
 
So you have the schematic setup, have you been able to try it? Try using the premade code from the links you provided to see if it works (since it has already been done before). I copied the 3 piece into one below:
Code:
#define LCD_RS    8    // Register select
#define LCD_EN    9    // Enable
#define LCD_D4 10    // Data bits
#define LCD_D5 11    // Data bits
#define LCD_D6 12    // Data bits
#define LCD_D7 13    // Data bits

#define MDATA 2       // Mouse Data
#define MCLK 3        // Mouse Clock

int midivalueX;
int midivalueY;

int XCC = 19;        // Midi Continous Controler assigned by default to X
int YCC = 21;        // Midi Continous Controler assigned by default to Y

/**********************************************************************
 *
 * Mouse
 * Source : Arduino Playground
 * URL : http://www.arduino.cc/playground/ComponentLib/Ps2mouse
 * Author : Unknown
 **********************************************************************/


void gohi(int pin)
{
  pinMode(pin, INPUT);
  digitalWrite(pin, HIGH);
}

void golo(int pin)
{
  pinMode(pin, OUTPUT);
  digitalWrite(pin, LOW);
}

void mouse_write(char data)
{
  char i;
  char parity = 1;

  /* put pins in output mode */
  gohi(MDATA);
  gohi(MCLK);
  delayMicroseconds(300);
  golo(MCLK);
  delayMicroseconds(300);
  golo(MDATA);
  delayMicroseconds(10);
  /* start bit */
  gohi(MCLK);
  /* wait for mouse to take control of clock); */
  while (digitalRead(MCLK) == HIGH);
  /* clock is low, and we are clear to send data */
  for (i=0; i < 8; i++) {
    if (data & 0x01) {
    gohi(MDATA);
    }
    else {
    golo(MDATA);
    }
    /* wait for clock cycle */
    while (digitalRead(MCLK) == LOW)
    ;
    while (digitalRead(MCLK) == HIGH)
    ;
    parity = parity ^ (data & 0x01);
    data = data >> 1;
  }
  /* parity */
  if (parity) {
    gohi(MDATA);
  }
  else {
    golo(MDATA);
  }
  while (digitalRead(MCLK) == LOW)
    ;
  while (digitalRead(MCLK) == HIGH)
    ;
  /* stop bit */
  gohi(MDATA);
  delayMicroseconds(50);
  while (digitalRead(MCLK) == HIGH)
    ;
  /* wait for mouse to switch modes */
  while ((digitalRead(MCLK) == LOW) || (digitalRead(MDATA) == LOW))
    ;
  /* put a hold on the incoming data. */
  golo(MCLK);
  //  Serial.print("done.\n");
}

/*
 * Get a byte of data from the mouse
 */
char mouse_read(void)
{
  char data = 0x00;
  int i;
  char bit = 0x01;

  //  Serial.print("reading byte from mouse\n");
  /* start the clock */
  gohi(MCLK);
  gohi(MDATA);
  delayMicroseconds(50);
  while (digitalRead(MCLK) == HIGH)
    ;
  delayMicroseconds(5);  /* not sure why */
  while (digitalRead(MCLK) == LOW) /* eat start bit */
    ;
  for (i=0; i < 8; i++) {
    while (digitalRead(MCLK) == HIGH)
    ;
    if (digitalRead(MDATA) == HIGH) {
    data = data | bit;
    }
    while (digitalRead(MCLK) == LOW)
    ;
    bit = bit << 1;
  }
  /* eat parity bit, which we ignore */
  while (digitalRead(MCLK) == HIGH)
    ;
  while (digitalRead(MCLK) == LOW)
    ;
  /* eat stop bit */
  while (digitalRead(MCLK) == HIGH)
    ;
  while (digitalRead(MCLK) == LOW)
    ;

  /* put a hold on the incoming data. */
  golo(MCLK);
  //  Serial.print("Recvd data ");
  //  Serial.print(data, HEX);
  //  Serial.print(" from mouse\n");
  return data;
}

void mouse_init()
{
  gohi(MCLK);
  gohi(MDATA);
  //  Serial.print("Sending reset to mouse\n");
  mouse_write(0xff);
  mouse_read();  /* ack byte */
  //  Serial.print("Read ack byte1\n");
  mouse_read();  /* blank */
  mouse_read();  /* blank */
  //  Serial.print("Sending remote mode code\n");
  mouse_write(0xf0);  /* remote mode */
  mouse_read();  /* ack */
  //  Serial.print("Read ack byte2\n");
  delayMicroseconds(100);
}

/**********************************************************************
 *
 * MIDI
 *
 **********************************************************************/

// Send a MIDI note-on message.  Like pressing a piano key
void noteOn(byte channel, byte note, byte velocity) {
  midiMsg( (0x80 | (channel<<4)), note, velocity);
}

// Send a MIDI note-off message.  Like releasing a piano key
void noteOff(byte channel, byte note, byte velocity) {
  midiMsg( (0x80 | (channel<<4)), note, velocity);
}

// Send a general MIDI message
void midiMsg(byte cmd, byte data1, byte data2) {
  //digitalWrite(ledPin,HIGH);  // indicate we're sending MIDI data
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
 // digitalWrite(ledPin,LOW);
}
 
/**********************************************************************
 *
 * LCD
 * Based on 4bit interface example
 * 2006 Massimo Banzi
 * Based on code written by Craig Lee 1998
 * 2007 Ludovic Drochon : LCDbar127
 *
 **********************************************************************/

void lcd_strobe()
{
    digitalWrite(LCD_EN,HIGH);
    digitalWrite(LCD_EN,LOW);
}

/* write a byte to the LCD in 4 bit mode */

void lcd_write(byte c)
{
    if(c & 0x80) digitalWrite(LCD_D7,HIGH); else  digitalWrite(LCD_D7,LOW);
    if(c & 0x40) digitalWrite(LCD_D6,HIGH); else  digitalWrite(LCD_D6,LOW);
    if(c & 0x20) digitalWrite(LCD_D5,HIGH); else  digitalWrite(LCD_D5,LOW);
    if(c & 0x10) digitalWrite(LCD_D4,HIGH); else  digitalWrite(LCD_D4,LOW);
    lcd_strobe();
    if(c & 0x08) digitalWrite(LCD_D7,HIGH); else  digitalWrite(LCD_D7,LOW);
    if(c & 0x04) digitalWrite(LCD_D6,HIGH); else  digitalWrite(LCD_D6,LOW);
    if(c & 0x02) digitalWrite(LCD_D5,HIGH); else  digitalWrite(LCD_D5,LOW);
    if(c & 0x01) digitalWrite(LCD_D4,HIGH); else  digitalWrite(LCD_D4,LOW);
    lcd_strobe();
    delayMicroseconds(40);
}

/*
 *     Clear and home the LCD
 */

void lcd_clear(void)
{
    digitalWrite(LCD_RS,LOW);

    lcd_write(0x1);
    delay(2);
}

/* write a string of chars to the LCD */

void lcd_puts(const char * s)
{
    digitalWrite(LCD_RS,HIGH);    // write characters

    while(*s) lcd_write(*s++);
}

/* write one character to the LCD */

void lcd_putch(byte c)
{
    digitalWrite(LCD_RS,HIGH); // write characters

    lcd_write(c);
}


/*
 * Go to the specified position
 */

void
lcd_goto(byte pos)
{
    digitalWrite(LCD_RS,0);

    lcd_write(0x80 + pos);
}

/* initialise the LCD - put into 4 bit mode */

void
lcd_init(void)
{


  pinMode(LCD_D7,OUTPUT);
    pinMode(LCD_D6,OUTPUT);
    pinMode(LCD_D5,OUTPUT);
    pinMode(LCD_D4,OUTPUT);
    pinMode(LCD_EN,OUTPUT);
    pinMode(LCD_RS,OUTPUT);

    digitalWrite(LCD_RS, LOW);    // write control bytes

    delay(15);// power on delay

    digitalWrite(LCD_D4, HIGH);    // init!
    digitalWrite(LCD_D5, HIGH); //
    lcd_strobe();
    delay(5);

    lcd_strobe();// init!
    delayMicroseconds(100);

    lcd_strobe();// init!
    delay(5);

    digitalWrite(LCD_D4, LOW);    // set 4 bit mode
    lcd_strobe();
    delayMicroseconds(40);

    lcd_write(0x28);// 4 bit mode, 1/16 duty, 5x8 font, 2lines
    lcd_write(0x0C);// display on
    lcd_write(0x06);// entry mode advance cursor
    lcd_write(0x01);// clear display and reset cursor
}

void downloadcustomcharacter(int CGRAMaddress, byte character[])
{
  for (int i = 0; i<8; i++) //load character bytes in to CGRAM
  {
    lcd_putch(character[i]);
  }
  delay(20);
}

void LCDbar127(int midiValue, short charBlock, short line)
{
  char  SmidiValue[3];
  lcd_goto(0x40*line);

  midiValue = constrain (midiValue, 0, 127); // limit input into 0 and 127
  int pixelPos = (midiValue  * charBlock * 5) / 127;
  int blockPos = (midiValue  * charBlock) / 127;

  for (short idx = 1; idx <= blockPos; idx++)
  {
    lcd_putch(5);
  }

  if (pixelPos % 5) lcd_putch(pixelPos % 5);  // display the not entire block
  blockPos++;

  for (short idx = blockPos ; idx < charBlock; idx++)
  {
    lcd_putch(0);
  }

  lcd_goto(0x40*line + charBlock);
  itoa(midiValue, SmidiValue, 10);
  if (midiValue<10) lcd_puts(" ");
  if (midiValue<100) lcd_puts(" ");

  lcd_puts(SmidiValue);
}

/**********************************************************************
 *
 * Main
 *
 **********************************************************************/

void setup() {
  delay (20);
  lcd_init();
  delay (200);
  lcd_write(0x40);
  //delay(40);
  byte barre1[] = {0x00,0x1F,0x00,0x00,0x00,0x00,0x1F,0x00}; //progress bar characters
  byte barre2[] = {0x00,0x1F,0x10,0x10,0x10,0x10,0x1F,0x00};
  byte barre3[] = {0x00,0x1F,0x18,0x18,0x18,0x18,0x1F,0x00};
  byte barre4[] = {0x00,0x1F,0x1C,0x1C,0x1C,0x1C,0x1F,0x00};
  byte barre5[] = {0x00,0x1F,0x1E,0x1E,0x1E,0x1E,0x1F,0x00};
  byte barre6[] = {0x00,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x00};

  downloadcustomcharacter (0 , barre1);
  downloadcustomcharacter (1 , barre2);
  downloadcustomcharacter (2 , barre3);
  downloadcustomcharacter (3 , barre4);
  downloadcustomcharacter (4 , barre5);
  downloadcustomcharacter (5 , barre6);

  Serial.begin(31250);// midi
  //Serial.begin(9600); //debug
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, OUTPUT);

  mouse_init();

  lcd_clear();
  lcd_goto(0);
  lcd_puts("TouchPad");
  lcd_goto(0x40);
  lcd_puts("  MIDI");
}

void loop() {
  char mstat;
  char mx;
  char my;

  /* get a reading from the mouse */
  mouse_write(0xeb);  /* give me data! */
  mouse_read();    /* ignore ack */

  mstat = mouse_read();
  mx = mouse_read();
  my = mouse_read();

  if (digitalRead(4)==HIGH)
    {
    XCC += mx;
    XCC = constrain (XCC, 0 , 127);
    lcd_goto(0);
    lcd_puts("CC# on X");
    LCDbar127(XCC, 5, 1);
    }
    else if (digitalRead(5)==HIGH)
    {
    YCC += mx;
    YCC = constrain (YCC, 0 , 127);
    lcd_goto(0);
    lcd_puts("CC# on Y");
    LCDbar127(YCC, 5, 1);
    }
  else if (mx!=0 || my!=0 )
  {

    midivalueX += mx;
    midivalueY += my;

    midivalueX = constrain (midivalueX, 0 , 127);
    midivalueY = constrain (midivalueY, 0 , 127);

    midiMsg(0xb0, XCC, midivalueX);
    midiMsg(0xb0, YCC, midivalueY);

    LCDbar127(midivalueX, 5, 0);
    LCDbar127(midivalueY, 5, 1);
    }


}
 
Back
Top