Arduino switches into Mac os X

Hi everyone,
Just a quick thing,
Im trying to interface my Arduino with my mac so that when I press one of 5 buttons on the arduino board it transferes a character to the mac via serial.

Ive written the code, however when I plug the board into the mac it will randomly think that one of the buttons has been pressed.

Code:

int next = 2;   // Next button
int prev = 3;   // Prev button
int play = 4;   // play/pause button
int louder = 5;   // Vol UP button
int quiter = 6;   // VOL down button

int val_next = 0;     // variable for reading the pin status
int val_prev = 0;     // variable for reading the pin status
int val_play = 0;     // variable for reading the pin status
int val_louder = 0;     // variable for reading the pin status
int val_quiter = 0;     // variable for reading the pin status

void setup() {
Serial.begin(9600);
  pinMode(next, INPUT);    // declare NEXT as input
   pinMode(prev, INPUT);    // declare Prevous as input
    pinMode(play, INPUT);    // declare PLAY / PAUSE as input
     pinMode(louder, INPUT);    // declare Louder Volume as input
      pinMode(quiter, INPUT);    // declare Quiter Volume as input
      
  digitalWrite(next, LOW);
  digitalWrite(prev, LOW);
  digitalWrite(play, LOW);
  digitalWrite(louder, LOW);
  digitalWrite(quiter, LOW);
  
  digitalWrite(val_next, LOW);
  digitalWrite(val_prev, LOW);
  digitalWrite(val_play, LOW);
  digitalWrite(val_louder, LOW);
  digitalWrite(val_quiter, LOW);
}

void loop(){
  val_next = digitalRead(next);  // read input value
  val_prev = digitalRead(prev);  // read input value
  val_play = digitalRead(play);  // read input value
  val_louder = digitalRead(louder);  // read input value
  val_quiter = digitalRead(quiter);  // read input value
  
  if (val_next == LOW) {         // check if the input is HIGH (button released)
    Serial.println("A");
    digitalWrite(next, LOW);
    delay(1000);
  }
   if (val_prev == LOW) {         // check if the input is HIGH (button released)
    Serial.println("B");
    digitalWrite(prev, LOW);
    delay(1000);
  }
   if (val_play == LOW) {         // check if the input is HIGH (button released)
    Serial.println("C");
    digitalWrite(play, LOW);
    delay(1000);
  }
   if (val_louder == LOW) {         // check if the input is HIGH (button released)
    Serial.println("D");
    digitalWrite(louder, LOW);
    delay(1000);
  }
   if (val_quiter == LOW) {         // check if the input is HIGH (button released)
    Serial.println("E");
    digitalWrite(quiter, LOW);
    delay(1000);
  }
}

Any ideas?
Thanks
Mathew

This block is not needed, and usualy you would not use digital write to a pin that is set to input

digitalWrite(next, LOW);
digitalWrite(prev, LOW);
digitalWrite(play, LOW);
digitalWrite(louder, LOW);
digitalWrite(quiter, LOW);

This block is wrong, what you are actualy doing here is setting pin 0 to LOW 5 times since all the variables are initialized to 0

digitalWrite(val_next, LOW);
digitalWrite(val_prev, LOW);
digitalWrite(val_play, LOW);
digitalWrite(val_louder, LOW);
digitalWrite(val_quiter, LOW);

What you probably mean to do is:

val_next=0;

But that is not really needed either since the variables are initialized to zero when you declared them.

In each of the IF statements the digitalWrite(XXX, LOW); is not needed.

if (val_next == LOW) { // check if the input is HIGH (button released)
Serial.println("A");
digitalWrite(next, LOW);
delay(1000);
}

Are your switches hooked up with pull up resistors ? if not this could explain the random behaviour.

Fantastic,
Thanks very much for your help.

Is their a way I can use the analogue interface rather than the digital inputs for this?

Thanks
Mathew

I think the analog inputs can be used as digital ins if that is what you mean.

well basically I need to have 5 switches register different characters into the computer and have the digital outputs for use with servos, so I would rather use the analogue inputs for the switch recognition and the digital pins for servo control?
if that's possible?

Thanks mathew

I am convinced that it is possible, the analog pins can be used as digital pins with numbers continuing the numbers used for the ordinary digital pins. Try to search the forum for it. use google to search the site, the built in search facility is not very good.

without making it too complicated you can
wire up the buttons to the analogue inputs as usual then you use analogRead to check the state.

if you use pull up resistors your buttons will go low when pressed. this can be detected by saying

if (analogRead(0) < 100) {
//button pressed

}

m