(Arduino + Processing) Scarttings noise comming from mini speakers?

Info about the projcet:
I'm interfering with the arduino over a serial port system. What its supposed to do is to change the output from when I pressed Q and W to high and low, respectally. Beside that I want it to play different notes on the keys from A->K. Theres not much going into it. It's one of my first real "on my own builds" and I had only a few random parts lying around. What I used in this was:

  • A mini speaker
  • A 470 resistor hooked to pin 9
  • A 1k resistor hooked to pin 3
  • An arduino :smiley :slight_smile:

The problem in a nutshell:
So basicly when I use the arduinos serial and put my commands into it, my mini speaker works perfectly. So I won't be sending picture of the circuit, nor do I have anything that can take pictures. Beside that the problem is more or less that when I input the commands into Arduinos serial it produce clear sound, thus when I run my program over processing and hits my keys it fucks up.
The power is lower so is the sound. I get scattering sounds and it just sounds awful.

The Codes:
Arduino part:

/*
          beep(speakerPin,2093,tempo); 	//C: play the note C (C7 from the chart linked to above) for tempoms
 beep(speakerPin,2349,tempo); 	//D
 beep(speakerPin,2637,tempo); 	//E
 beep(speakerPin,2793,tempo); 	//F
 beep(speakerPin,3136,tempo); 	//G
 beep(speakerPin,3520,tempo); 	//A
 beep(speakerPin,3951,tempo); 	//B
 beep(speakerPin,4186,tempo); 	//C
 */

int High = 9;
int Low = 3;
int Volume = 9;
int LoopTime = 379*2;


void beep (int speakerPin, int Hertz,char Note)     // the sound producing function
{
  int x = 0;
  Serial.println("Beep was called by the note of:");	 
  long delayAmount = (long)(1000000/Hertz);
  Serial.println(delayAmount);

  do{
    digitalWrite(speakerPin,HIGH);
    delayMicroseconds(delayAmount);
    digitalWrite(speakerPin,LOW);
    delayMicroseconds(delayAmount);
    x = x+1;
  } 
  while (Serial.read()!='R');
  Serial.println("I was released");
}
void setup() 	 
{ 	 
  Serial.begin(9600);
  pinMode(High, OUTPUT);
  pinMode(Low, OUTPUT);

  //Testing sound
  analogWrite(Volume, 255);
} 	 

void loop() 	// run over and over again
{ 
  while(Serial.available() == 0);
  switch(char val = Serial.read()){
  case 'H':
    Volume = 9;
    Serial.println("Volume was set to high");
    break;
  case 'L':
    Volume = 3;
    Serial.println("Volume was set to low");
    break;
  case 'C':
    beep(Volume,2093,'C');
    break;
  case 'D':
    beep(Volume,2349,'D');
    break;
  case 'E':
    Serial.println("PLAYING E");
    Serial.println(Volume);
    beep(Volume,2637,'E');
    break;
  case 'F':
    beep(Volume,2793,'F');
    break;
  case 'G':
    beep(Volume,3136,'G');
    break;
  case 'c':
    beep(Volume,4186,'c');
    break;
  case 'A':
    beep(Volume,3520,'A');
    break;
  case 'B':
    beep(Volume,3951,'B');
    break;
  }
}

Processing part:

import processing.serial.*;
Serial port;

int HighVol = 8;
int LowVol = 3;
int volume = LowVol;

void setup() {
  size(100, 400);
  port = new Serial(this, "COM8", 9600);
  port.bufferUntil('\n');
}

void draw() {
  if (keyPressed) {
    switch(key) {
    case 'q':
      port.write("L");
    case 'w':
      port.write("H");
    case 'a':
      port.write("C");
    case 's':
      port.write("D");
    case 'd':
      port.write("E");
    case 'f':
      port.write("F");
    case 'g':
      port.write("G");
    case 'h':
      port.write("A");
    case 'j':
      port.write("B");
    case 'k':
      port.write("c");
    }
  }
  port.write("R");
}