ATMEGA328-PU-P issues

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);
}

How have you wired the button - you are using pinMode INPUT not INPUT_PULLUP so presumably
there is a pull-down resistor on pin 11? If not then it will read haywire...

ah that's true, I didn't think of that. I'll give it a go tomorrow. Another thing I need to check is if the 22pf caps I used are tantalum and polarised.

Need a pull-up resistor for the reset pin. Also, the part number is ATMEGA328P-PU, not PU-P

deved:
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.

That's incorrect. You do not need to change the code at all if you wrote it for an Arduino Uno and are now trying to run it on an atmega328p on a breadboard. Leave the code exactly the same, and use the pin mapping diagram at http://arduino.cc/en/Hacking/PinMapping168 to relate the logical pin numbers used in the code to physical pin numbers on the chip.

Also make sure that you have a 0.1uF ceramic capacitor connected between the Vcc and Gnd pins of the chip.

22pF capacitors are not polarised.

boom! thank you, that sounds like the answer I was looking for. Will implement suggestions tomorrow, and hopefully it'll work then.

Thanks again!

deved:
ah that's true, I didn't think of that. I'll give it a go tomorrow. Another thing I need to check is if the 22pf caps I used are tantalum and polarised.

The 22pF capacitors will be ceramic and unpolarised I reckon. Polarised capacitors are electrolytic and have values of around 1uF and up

You should have decoupling capacitors on the supply pins right next to the chip.

OK,

so I'm having mixed results with this now. For some reason I'm hoping someone can shed light on my breadboard atmega328 circuit is not working at all now.

After implementing the various suggestions earlier in this thread I realised I'd made a mistake in wiring another part of the stripboard version so went about breadboarding it from scratch. Whereas before I had no problems getting it to work on breadboard, now it does nothing at all when I run test code (blink). As far as I can tell (trust me, I spent most of yesterday trying to figure this out by myself so checked and rechecked) it's wired the same as before. The avr chip runs the code fine on the arduino but once I take it out and plop it into the breadboard it does nothing. My LED wiring works as I can power them using the blink code for all 4 LED's in my circuit (hooking up Arduino to breadboard).
Here's a photo.

I've got 22pf caps running to ground from pins 9 and 10, with a 16Mhz crystal on 9 and 10. Power linked between VCC, AREF and AVCC (7, 20 and 21). Grounds at 8 and 22 linked. I've a wire from pin 13 (pin 19 on the board) running through a resistor to an LED with the Blink example. Power and ground coming off the arduino. 10k resistor at reset pin to +V.

I've tried various capacitor combinations on the power rails but don't see why the above worked before and doesn't now. I've checked my wiring and remade the circuit several times. I feel very stupid at this point so if anyone sees something that's too obvious for me to see, please tell before it goes in the bin!

I'm an idiot, as my experiences with Arduino repeatedly attest. I realised when rereading my last post that I had 100nf rather than 22pf caps for the crystal. LED now blinking.

Nothing to see here (for now), sorry if I wasted anybody's time reading the above post.

The "22pF" caps do look "big" :wink:

Do not connect Aref to 5V. Only connect a 100nF cap from the pin to Gnd.
It connects to 5V or 1.1V internally.

You should also have a 100nF capacitor connected between the Vcc and Gnd pins of the atmega328p, as close to the chip as reasonably possible.