Arduino Controled music responsive RGB light, and my coding attempt.

So im bout 1/2 way through my RGB desk lamp build. I have some programming experience, and feel im doing a decent job.
In the spirit of learning, i have posted my code for others to see! I hope to accomplish 2 things with this. I hope new coders can learn from my code, but most of all, i was hoping a more experience code than myself, could teach me a few things!

With that, here is my little arduino script, in code tags :smiley:
edit: sp error(s)
Edit: Code may be a bit messy, but i wrote it in about an hour, and realize i still have ALOT of cleaning house to do!
Just wanted to point out, my lamp works as expected :smiley: im very happy with the power if this tiny arduino! (uno r3)

int REDPIN=10;
int GREENPIN=9;
int BLUEPIN=11;
int micPin = A0;
int micValue = 0;
int mic_avg = 0; //better audio
int potPin = A1;
int potValue = 0;
int r=random(3);
int g=random(3);
int b=random(3);
int r_p=10;
int b_p=10;
int g_p=10;
int f_r=1;
int f_g=1;
int f_b=1;
int r_h=0;
int g_h=0;
int b_h=0;
int hold=0;
int mode=0;
int max_mode=5;
int hit=0;
int power_button = 2;     // the number of the pushbutton pin
int mode_button = 4;     // the number of the pushbutton pin
int power=1;
int pressed=0;
int buttonState=0;
int timer=0;
int change=100;
int speaker_pin=6;
float dub=1.3; //super hit

///auto calibrate stuff
int cal_avg =0;
int hit_counter=0;
int hit_thresh=2;
int no_hit_counter=0;
int no_hit_thresh=20;


int i;//sound
int n_pitch;//sound
int n_r;//sound

void setup() 
{
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  pinMode(speaker_pin, OUTPUT);
  pinMode(power_button, INPUT);
  Serial.begin(9600); 
  calibrate();   
}


void loop() 
{

  buttonState = digitalRead(power_button);
  if (buttonState == HIGH) {
    if (pressed==0){
      pressed=1; 
      if (power==-1){
        power=1;
      }
      else{
        power=-1;
      }  
      beep_on();
    }
  }
  if (buttonState == LOW) {
    if (pressed==1) {
      pressed=0; 
      beep_off(); 
    }
  }

  buttonState = digitalRead(mode_button);
  if (buttonState == HIGH) {
    if (pressed==0){
      pressed=1; 
      mode=(mode+1); 
      if (mode>max_mode){
        mode=0;
      } 
      beep_on(); 
      flash_mode();
    }
  }
  if (buttonState == LOW) {
    if (pressed==1) {
      pressed=0; 
      beep_off(); 
    }
  }

  get_audio();//gets audio from mic. sets hit to 1 or 0

  if (mode==0){
    if (hit==1){
      random_color();
    }
  }
  if (mode==1)
  {
    if (micValue>(potValue*dub)){
      random_color();
    }

    r=(r+((micValue/22)*f_r));
    g=(g+((micValue/21)*f_g));
    b=(b+((micValue/20)*f_b));

    if (r>255){
      r=255; 
      f_r=-1;
    }
    if (g>255){
      g=255; 
      f_g=-1;
    }
    if (b>255){
      b=255; 
      f_b=-1;
    }
    if (r<0){
      r=0; 
      f_r=1;
    }
    if (g<0){
      g=0; 
      f_g=1;
    }
    if (b<0){
      b=0; 
      f_b=1;
    }

  }
  if (mode==2)
  {
    if (micValue>(potValue*dub))
    {
      r_h=random(255);
      g_h=random(255); 
      b_h=random(255);
    }
    if (hit==1){
      r=0; 
      g=0; 
      b=0;
    }//r=r_h; g=g_h; b=b_h; 
    r=r+((r_h-r)/50); 
    g=g+((g_h-g)/50); 
    b=b+((b_h-b)/50);
    if (r>255){
      r=255; 
      f_r=-1;
    }
    if (g>255){
      g=255; 
      f_g=-1;
    }
    if (b>255){
      b=255; 
      f_b=-1;
    }
  }

  if (mode==3)
  {
    potValue=analogRead(potPin);
    r=(r+((potValue/110)*f_r));
    g=(g+((potValue/120)*f_g));
    b=(b+((potValue/101)*f_b));  

    if (r>255){
      r=255; 
      f_r=-1;
    }
    if (g>255){
      g=255; 
      f_g=-1;
    }
    if (b>255){
      b=255; 
      f_b=-1;
    }
    if (r<0){
      r=0; 
      f_r=1;
    }
    if (g<0){
      g=0; 
      f_g=1;
    }
    if (b<0){
      b=0; 
      f_b=1;
    }
  }

  if (mode==4)
  {
    if (micValue>(potValue*dub))
    {
      r_h=random(255);
      g_h=random(255); 
      b_h=random(255);
    }
    if (hit==1){
      r=(r-(micValue*5)); 
      g=(g-(micValue*5)); 
      b=(b-(micValue*5));
    }//r=r_h; g=g_h; b=b_h; 
    r=r+((r_h-r)/150); 
    g=g+((g_h-g)/150); 
    b=b+((b_h-b)/150);
    if (r<0){
      r=0; 
    }
    if (g<0){
      g=0;
    }
    if (b<0){
      b=0;
    }
    if (r>255){
      r=255;
    }
    if (g>255){
      g=255;
    }
    if (b>255){
      b=255; 
    }
  }

  if (power==-1){
    r=0; 
    g=0; 
    b=0;
  }
  color_write();
}
////////////END LOOP!!!!



void beep_on()
{
  i=0; 
  n_pitch=213;   
  n_r=(50000/n_pitch);

  for (i = 0; i < n_r; i++) 
  {
    analogWrite(speaker_pin, 255); 
    delayMicroseconds(n_pitch); 
    analogWrite(speaker_pin, 0);
    delayMicroseconds(n_pitch);
  }
}



void beep_off()
{
  i=0; 
  n_pitch=196;
  n_r=(50000/n_pitch);

  for (i = 0; i < n_r; i++) 
  {
    analogWrite(speaker_pin, 255); 
    delayMicroseconds(n_pitch); 
    analogWrite(speaker_pin, 0);
    delayMicroseconds(n_pitch);
  }
}



void get_audio()
{
  //potValue = map(analogRead(potPin),0,1024,0,1024);
  hit=0;
  Serial.print("Mode:");
  Serial.print(mode);
  Serial.print(":___:");
  Serial.print("Cal:");
  Serial.print(potValue);


  //////////////////////////////////////////
  mic_avg=0;
  for (i = 0; i < 20; i++) 
  {
    mic_avg = (mic_avg+analogRead(micPin));
  }
  mic_avg=(mic_avg/20);
  micValue =mic_avg;
  /////////////////////////////////////////////////
  Serial.print(":___:");
  Serial.print("Mic:");
  Serial.print(micValue);
  Serial.print(":___:");
  if (micValue>potValue){
    Serial.print("(B)"); 
    hit=1; 
    no_hit_counter=0; 
    hit_counter=(hit_counter+1);
  }
  else{
    Serial.print(""); 
    hit_counter=0; 
    no_hit_counter=(no_hit_counter+1);
  }
  if (hit_counter>hit_thresh){
    calibrate(); 
    hit_counter=0;
  }
  if (no_hit_counter>no_hit_thresh){
    calibrate(); 
    no_hit_counter=0;
  }
  if (micValue>(potValue*dub)){
    Serial.print("(D)");
  }

  Serial.println();
}
void random_color()
{
  r=random(255);
  g=random(255); 
  b=random(255);

  if (random(100)>90){
    r=0; 
    g=0; 
    b=0;
  }
}

void color_write()
{
  analogWrite(REDPIN, r);
  analogWrite(BLUEPIN, b);
  analogWrite(GREENPIN, g);
}

void flash_mode()
{
  r_h=r;
  g_h=g;
  b_h=b;
  r=0; 
  g=0; 
  b=0;

  if (mode==0){ 
    color_write(); 
    delay(100); 
    r=255; 
    color_write(); 
    delay(300); 
    r=0; 
    color_write(); 
    delay(10);
  }
  if (mode==1){ 
    color_write(); 
    delay(100); 
    g=255; 
    color_write(); 
    delay(300); 
    r=0; 
    color_write(); 
    delay(10);
  }
  if (mode==2){ 
    color_write(); 
    delay(100); 
    b=255; 
    color_write(); 
    delay(300); 
    r=0; 
    color_write(); 
    delay(10);
  }
  if (mode==3){ 
    color_write(); 
    delay(100); 
    b=255; 
    g=255; 
    color_write(); 
    delay(300); 
    r=0; 
    color_write(); 
    delay(10);
  }
  if (mode==4){ 
    color_write(); 
    delay(100); 
    b=255; 
    r=255; 
    color_write(); 
    delay(300); 
    r=0; 
    color_write(); 
    delay(10);
  }
  r=r_h;
  g=g_h;
  b=b_h;

}

void calibrate()
{
  cal_avg=0;
  for (i = 0; i < 50; i++) 
  {
    cal_avg = (cal_avg+analogRead(micPin));
  }
  cal_avg=(cal_avg/50);
  potValue=cal_avg;

}

Without even actually trying to understand what the code does, I have some comments.

White space is your friend.

int REDPIN=10;
int GREENPIN=9;
int BLUEPIN=11;
int micPin = A0;
int micValue = 0;
int mic_avg = 0; //better audio
int potPin = A1;
int potValue = 0;
int r=random(3);
int g=random(3);
int b=random(3);
int r_p=10;

would look much better as:

int REDPIN=10;
int GREENPIN=9;
int BLUEPIN=11;

int micPin = A0;
int micValue = 0;
int mic_avg = 0; //better audio

int potPin = A1;
int potValue = 0;

int r=random(3);
int g=random(3);
int b=random(3);

int r_p=10;
int i;//sound

Sometimes, one letter names are OK, like r, b, and d. Not in this case, though. There is nothing that makes me think of sound when I see the variable i. I think "for loop index", which should not be a global variable.

if (buttonState == HIGH) {
  }
  if (buttonState == LOW) {

If it isn't HIGH, can it be anything but LOW? If the answer is no (as in this case), an else statement is far more appropriate.

Does pressed go with power_button or mode_button (both of which should have _pin, not _button, in the name)? You are using it with both, with no comments, so it's confusing.

  get_audio();//gets audio from mic. sets hit to 1 or 0

A get function should return a value. This one appears not to. Calling a function that gets a value, and not storing the value, is pointless.

  if (mode==1)
  {
  }
  if (mode==2)
  {
  }
  if (mode==3)
  {
  }
  if (mode==4)
  {
  }

Definitely should be a switch statement. What happens in each case definitely belongs in another function. The loop() function is far too long.

How do beep_on() and beep_off() differ? Only in the value assigned to n_pitch, as far as I can tell. Too much common code replicated in those two functions.