Mini Piano V1.0

https://gist.github.com/vick5821/6390286
I made the mini piano using this coding. When I use atmegha 328 it works perfectly. However, when I upload the code through USB or USBasp to the atmega 8, it does not work as expected.

What is the problem actually ? Suppose any kind of cheap should work ?

Thanks !

What exactly do you mean by "does not work as expected"?

Do not play the tone. All they key are same tone. Same frequency instead

The ATMega 8 doesn't have as much flash (8K) or static ram (1K) as a 328 (32k and 2k) so your program is probably too big to run in the ATMega 8.

Pete

The binary sketch size for that code is just 3196 bytes :slight_smile:

You could still be running out of static ram.
Post your code.

Pete

int led = A0;
int speaker = 13;
int button_1 = 0;
int button_2 = 1;
int button_3=2;
int button_4 = 3;
int button_5 = 4;
int button_6=5;
int button_7=6;
int button_8=7;
int button_9=8;
int button_10=9;
int button_11=10;
int button_12=11;
int button_13=12;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
 
  pinMode(speaker,OUTPUT);
pinMode(0,INPUT);
pinMode(1,INPUT);
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(5,INPUT);
pinMode(6,INPUT);
pinMode(7,INPUT);
pinMode(8,INPUT);
pinMode(9,INPUT);
pinMode(10,INPUT);
pinMode(11,INPUT);
pinMode(12,INPUT);
pinMode(A0,OUTPUT);
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
noTone(13);
}

// the loop routine runs over and over again forever:
void loop() {
  if(digitalRead(0) == LOW)
  {
     digitalWrite(led, HIGH);
    tone(13,262);
     }
    else  if(digitalRead(1) == LOW)
     {digitalWrite(led, HIGH);
       tone(13,294);
     }
      else  if(digitalRead(2) == LOW)
     {
       digitalWrite(led, HIGH);
       tone(13,330);
     }
      else  if(digitalRead(3) == LOW)
     {
       digitalWrite(led, HIGH);
       tone(13,349);
     }
      else  if(digitalRead(4) == LOW)
     {
       digitalWrite(led, HIGH);
       tone(13,392);
     }
     else  if(digitalRead(5) == LOW)
     {
       digitalWrite(led, HIGH);
       tone(13,440);
     }
     else  if(digitalRead(6) == LOW)
     {
       digitalWrite(led, HIGH);
       tone(13,494);
     }
      else  if(digitalRead(7) == LOW)
     {
       digitalWrite(led, HIGH);
       tone(13,523);
     }
     else  if(digitalRead(8) == LOW)
     {
       digitalWrite(led, HIGH);
       tone(13,277);
     }
      else  if(digitalRead(9) == LOW)
     {
       digitalWrite(led, HIGH);
       tone(13,311);
     }
     else  if(digitalRead(10) == LOW)
     {
       digitalWrite(led, HIGH);
       tone(13,370);
     }
      else  if(digitalRead(11) == LOW)
     {
       digitalWrite(led, HIGH);
       tone(13,415);
     }
      else  if(digitalRead(12) == LOW)
     {
       digitalWrite(led, HIGH);
       tone(13,466);
     }
     else
     {
       digitalWrite(led, LOW);
       noTone(13);
     }
 
     
}

This is my code.

Thank you

From: http://arduino.cc/en/Reference/Board

On boards with an ATmega8, PWM output is available only on pins 9, 10, and 11

You will have to move a couple of pins around because pin 13 can't do PWM on the ATMega 8.

Pete

On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 through 13. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.

P.S. When you post code, read this: How to post code properly

You can edit your post with the code in it, click on Modify (just above and to the right of your message), find the [ quote] and [ /quote] tags around your code and change them to [ code] and [ /code] (without the space).

Pete

el_supremo:
P.S. When you post code, read this: How to post code properly

You can edit your post with the code in it, click on Modify (just above and to the right of your message), find the [ quote] and [ /quote] tags around your code and change them to [ code] and [ /code] (without the space).

Pete

Thank you for this ! :slight_smile:

Appreciate ^^

el_supremo:
From: http://arduino.cc/en/Reference/Board

On boards with an ATmega8, PWM output is available only on pins 9, 10, and 11

You will have to move a couple of pins around because pin 13 can't do PWM on the ATMega 8.

Pete

Ohhh, does that mean pin 13 can do PWM on atmega 328 ? I dont think so. Hahaha. I dont use any PWM on this piano :slight_smile: The arduino is generating square waveform to generate tone I think :0

int led = A0;
int speaker = 13;
int button_1 = 0;
int button_2 = 1;
int button_3=2;
int button_4 = 3;
int button_5 = 4;
int button_6=5;
int button_7=6;
int button_8=7;
int button_9=8;
int button_10=9;
int button_11=10;
int button_12=11;
int button_13=12;

You could save yourself a fair bit of RAM by making those "const byte"

I don't get what you mean. 'const byte' ?

Thanks

I mean declare them as "const byte" instead of "int".
(Unless you plan on changing their values, in which case just "byte" will do)

Okay, I will try that !

Thank you :slight_smile:

So what's the real problem of can't using an atmega 8 ?

@@

The arduino is generating square waveform to generate tone I think

Yup. My bad. I thought it used 50% duty-cycle PWM.

Pete

AWOL:

int led = A0;

int speaker = 13;
int button_1 = 0;
int button_2 = 1;
int button_3=2;
int button_4 = 3;
int button_5 = 4;
int button_6=5;
int button_7=6;
int button_8=7;
int button_9=8;
int button_10=9;
int button_11=10;
int button_12=11;
int button_13=12;



You could save yourself a fair bit of RAM by making those "const byte"

You could save SRAM and save yourself some compile time by removing the declarations of button_1 through button_13 since they are not actually used in the code. :slight_smile:

So you get one note at a time out of that?
I used a '1284P, no PWM, and get up to 13 notes at a time.
Playing 8 in that video as that was all the switches I had.

piano13keyBurstFix.ino (11.2 KB)