Hi,
I've been working on my first Arduino-based MIDI controller for the past couple of weeks. It's simple enough - 6 pots, 2 buttons and a couple of LED's. It works fine on the Arduino / breadboard but I want to move it over to a stripboard and ATMEGA chip (bootloaded with Arduino) and am having problems with it.
I've got it set up on the breadboard with a 16Mhz crystal and 2 22pf caps, and have been powering it off the Arduino. I upload the sketch with altered pinout for the chip(eg, analog 0 = pin 23, TX = pin 3 etc.), and connected power and ground as per the instructions on the Arduino site and various instructables. It ain't working though, and I'm sure it's something elementary but it's got me stumped.
I've tried running a blink sketch on it which had the odd behaviour of making an LED on pin 6 flash even though it wasn't specified in the code. Below is my main MIDI code - there may be some basic mistakes in it but none that I can see would make it not work at all.
Any suggestions on how to troubleshoot this is welcome and appreciated.
//int newVal, oldVal;
int pots[5]={
23, 24, 25, 26, 27}; //POT PINS ARRAY (arduino 14, 15, 16, 17, 18)
int index = 0;
int val = 0;
int oldVal[5]; //ARRAY WHICH HOLDS 5 ELEMENTS
int threshold = 1;
//LED MODE CHANGE//
//10k resistor from pin 1 (reset) to ground to prevent resetting
int buttonPin = 11; //12; // the number of the input pin
//int muteButton = 13; //8; // Livid button
int ledRed = 4; // 11; // the number of the output pin
int ledBlue = 5; // 10;
int ledGreen = 6; // 7;
//int muteLed = 14; // 9; //Livid button LED
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
int muteState = 0;
int lastMuteState = 0;
int mutePushCounter = 0; // counter for the number of button presses
void setup(){
Serial.begin(31250);
pinMode(buttonPin, INPUT);
// pinMode(muteButton, INPUT);
pinMode(ledRed, OUTPUT);
pinMode(ledBlue, OUTPUT);
// pinMode(muteLed, OUTPUT);
pinMode(ledGreen, OUTPUT);
digitalWrite(buttonPin, LOW);
// digitalWrite(muteButton, HIGH);
digitalWrite(ledBlue, LOW);
digitalWrite(ledRed, LOW);
// digitalWrite(muteLed, LOW);
}
void loop(){
// CODE FOR MODE CHANGE BUTTON
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
delay(10);
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// went from off to on:
buttonPushCounter = buttonPushCounter + 1;
if(buttonPushCounter ==3)buttonPushCounter = 0;
}
else {
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter == 0) {
digitalWrite(ledRed, HIGH);
digitalWrite(ledBlue, LOW);
digitalWrite(ledGreen, LOW);
ccBatch1(0);
}
else if (buttonPushCounter == 1){
digitalWrite(ledRed, LOW);
digitalWrite(ledBlue, HIGH);
digitalWrite(ledGreen, LOW);
ccBatch1(6);
}
else if (buttonPushCounter == 2){
digitalWrite(ledRed, LOW);
digitalWrite(ledBlue, LOW);
digitalWrite(ledGreen, HIGH);
ccBatch1(12);
}
// muteState = digitalRead(muteButton);
// if(muteState != lastMuteState){
// delay(10);
// if(muteState == LOW){
// mutePushCounter = mutePushCounter + 1;
//
// if(mutePushCounter ==2)mutePushCounter = 0;
// delay(10);
// }
//
// }
// lastMuteState = muteState;
// if(mutePushCounter = 0){
// digitalWrite(muteLed, HIGH);
// CC(0, 17, 127);
// }
// else{
// digitalWrite(muteLed, LOW);
// CC(0, 17, 0);
// }
}
void ccBatch1(int add){//maps pots to CC 1 - 5
int number = 1 + add; //depending on state of button, this will map to 0-4 or 5-9
for (int i = 0; i<6;i++){ // look at the analogue inputs one at a time
val = analogRead(pots[i]); // get the analogue input value
// val = map(val, 0, 1023, 0, 255);
val >> 2;
// compare it to what you had last time
if (abs(val - oldVal[i]) > threshold) {
CC(0, number + i, val/8);
}
oldVal[i] = val; // store the reading in the array for next time
delay(10); // send the reading out if it has changed a lot
} // end of the for loop
}
// Continuos Controller Function
void CC(int ChannelByte,int ControlNumber,int ControlValue){
Serial.write(ChannelByte + 0xb0);
Serial.write(ControlNumber);
Serial.write(ControlValue);
}