How do I change the code to do the opposite? (conductive paint)

Here's my problem. At the moment when I press down a piece of conductive painted card onto the painted circuit to close the circuit, a song stops playing, however I want it so that there is no sound, then you press the conductive painted card down to complete the circuit and sound starts?

(I am new to Arduino and code!)

// Project 11 - Melody Player
// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 9;     // the number of the pushbutton pin
const int speakerPin =  9;      // the number of the speaker pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status


int length = 15; // the number of notes 
char notes[] = "ccggaagffeeddc "; // a space represents a rest 
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; 
int tempo = 300;

void playTone(int tone, int duration) { 
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
     digitalWrite(speakerPin, HIGH);
     delayMicroseconds(tone);
     digitalWrite(speakerPin, LOW);
     delayMicroseconds(tone);
  }
}


void playNote(char note, int duration) { 
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; 
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; 
  // play the tone corresponding to the note name for (int i = 0; i < 8; i++) {
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) { 
      playTone(tones[i], duration);
     }
  }
} 

void setup() {
  // initialize the speaker pin as an output:
  pinMode(speakerPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     


  pinMode(speakerPin, OUTPUT);
}

void loop() { 
   // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn speaker on:    
    digitalWrite(speakerPin, HIGH);  
  } 
  else {
    // turn speaker off:
    digitalWrite(speakerPin, LOW); 
  }

  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') { 
      delay(beats[i] * tempo); // rest
    } else { 
      playNote(notes[i], beats[i] * tempo);
    }
    
    // pause between notes
    delay(tempo / 2);
  }
}

I'm just going to make a guess...

Old code:

 // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn speaker on:    
    digitalWrite(speakerPin, HIGH);

Try changing the condition from HIGH to LOW...

 // check if the pushbutton is pressed.
  // if it is, the buttonState is LOW:
  if (buttonState == LOW) {     
    // turn speaker on:    
    digitalWrite(speakerPin, HIGH);

Unfortunately that didn't work but thanks anyway. I have a video in my blog (2nd post) which may help give an idea of what I mean...

While I'm new to Arduino, I've done a fair bit of coding. What jumps out at me is that the speaker pin and button pin are defined as the same pin at the top; then, in the setup, that pin is set to OUTPUT, then INPUT, then back to OUTPUT, and there it stays for the loop.

Can you describe how you have this project wired? I wonder if, somehow, when you apply the painted card, it actually opens the circuit somehow....?

Edit: just watched the vid. I think the issue is in the wiring (and the definition). Is there a project template that you borrowed this from? I think when you put the card down, you basically short Pin 9 to ground?

I have posted an image of the circuit of my blog. The digital wire is 9 and anologue wires are 5v and ground

Does that help?

I used a piezo sound example and arduino button example. Any ideas on how to fix it?

 digitalWrite(speakerPin, HIGH);

This does not turn the speaker on nor LOW turn it off. It simply puts the pin high or low.

Why have you opened another thread on this and what happens when you tried the code I gave you in the other thread?

This is the code with the code you gave me, I think I may have put it in wrong as the sound remains the same. Should I change the digitalWrites to digitalReads? Sorry I'm still trying to understand the code as I am new to this.

// Project 11 - Melody Player
// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 9;     // the number of the pushbutton pin
const int speakerPin =  9;      // the number of the speaker pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int pin = 0;

int length = 15; // the number of notes 
char notes[] = "ccggaagffeeddc "; // a space represents a rest 
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; 
int tempo = 300;

void playTone(int tone, int duration) { 
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
     digitalWrite(speakerPin, HIGH);
     delayMicroseconds(tone);
     digitalWrite(speakerPin, LOW);
     delayMicroseconds(tone);
  }
}


void playNote(char note, int duration) { 
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; 
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; 
  // play the tone corresponding to the note name for (int i = 0; i < 8; i++) {
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) { 
      playTone(tones[i], duration);
     }
  }
} 

void setup() {
  // initialize the speaker pin as an output:
  pinMode(speakerPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     


  pinMode(speakerPin, OUTPUT);
}

void loop() { 
   // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

if(digitalRead(pin) == LOW) { // make the sound if you read LOW from the button.
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      playNote(notes[i], beats [i] * tempo);
    }
    
    // pause between notes
    delay(tempo / 2);
  }
}

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn speaker on:    
    digitalWrite(speakerPin, HIGH);  
  } 
  else {
    // turn speaker off:
    digitalWrite(speakerPin, LOW); 
  }

  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') { 
      delay(beats[i] * tempo); // rest
    } else { 
      playNote(notes[i], beats[i] * tempo);
    }
    
    // pause between notes
    delay(tempo / 2);
  }
}

I am new to this.

The thing you must learn is that things happen for a reason, and that reason can be found if you investigate. Trying things at random and simply saying it doesn't work will get you nowhere.

First off as you have an unusual switch and you are a beginner have you tested this switch in isolation? Write a sketch to simply turn on an off the LED on pin 13 in response to the switch. Get that working and then you can move on to using it.

Next the code you posted is NOT what I told you to do. I gave you the code for the WHOLE loop() function, you seem to have incorporated it into part of a loop. What that code is doing is testing the switch and if necessary skipping the play bit BUT then you have the same play bit of code all over again so you will get sound no matter what state the switch is in.

You read code in the same way as you read a book, one line at a time. Follow it like you follow a story.

Try this:-

// Project 11 - Melody Player
// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 9;     // the number of the pushbutton pin
const int speakerPin =  9;      // the number of the speaker pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int pin = 0;

int length = 15; // the number of notes 
char notes[] = "ccggaagffeeddc "; // a space represents a rest 
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; 
int tempo = 300;

void playTone(int tone, int duration) { 
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
     digitalWrite(speakerPin, HIGH);
     delayMicroseconds(tone);
     digitalWrite(speakerPin, LOW);
     delayMicroseconds(tone);
  }
}


void playNote(char note, int duration) { 
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; 
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; 
  // play the tone corresponding to the note name for (int i = 0; i < 8; i++) {
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) { 
      playTone(tones[i], duration);
     }
  }
} 

void setup() {
  // initialize the speaker pin as an output:
  pinMode(speakerPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     


  pinMode(speakerPin, OUTPUT);
}

void loop() { 
   // read the state of the pushbutton value:

if(digitalRead(buttonPin) == LOW) { // make the sound if you read LOW from the button.
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      playNote(notes[i], beats [i] * tempo);
    }
    
    // pause between notes
    delay(tempo / 2);
  }
 }
}

Now this will play the sound if your switch reads high and not when it reads low, if this is the wrong way round change the HIGH to LOW in the if statement.
Note this only checks for the switch at the start of each sequence, so once a sequence is started it will carry on playing until the end irrespective of the state of the switch. If this is not what you want then say so, it is a big beginners mistake to use a vague sort of language and not spell out exactly what you want to happen.

EDIT
I also noticed you defined 'pin' as 0 this means even if you had been putting the code in correctly you would have been reading the wrong input.

I copy and pasted your code and i'm afraid it still plays the sound before pressing down the button. When the button (or in my case card) is pressed the music stops. I also tried swapping the 'if' from LOW to HIGH but that stopped the sound all together.

I noiced you said -

"I also noticed you defined 'pin' as 0 this means even if you had been putting the code in correctly you would have been reading the wrong input."

If I change this might this help to make it work?

I really appreciate your help!

*Also to clarify I would like it to play as soon as you press the card down, it doesn't matter where in the sequence the sound is as long as there is sound.

I also tried swapping the 'if' from LOW to HIGH but that stopped the sound all together.

That means you switch is not working. Did you test it like I said?

If I change this might this help to make it work?

No I edited the code to make this not matter by using:-

if(digitalRead(buttonPin) == LOW) { // make the sound if you read LOW from the button.

That is changing my use of the variable pin (which I did say make the value the pin number you are using ) into buttonPin which is what you were already using.

*Also to clarify I would like it to play as soon as you press the card down

It will do this, but it won't stop when the card is lifted until the end of the sequence.

yeah it worked when I tested it

You said:-

I copy and pasted your code and i'm afraid it still plays the sound before pressing down the button. When the button (or in my case card) is pressed the music stops.

Fair enough but then you say:-

I also tried swapping the 'if' from LOW to HIGH but that stopped the sound all together.

Does not make any sense at all if the previous statement was true. Sorry but computers are very logical devices.

When I used the code you gave me the music started playing automatically, I pressed the card down and the music stopped as before. I then tried swapping the if from LOW to HIGH hoping that this would reverse it like you suggested. No music played when I uploaded it and when I pressed the card down again, no sound occurred. I'm wondering if perhaps I have the wires in the wrong place or the wrong type of resistor, might this be a reason for it to not work?

If you got the music stopping and starting in response to the switch then that suggests that the switch is working fine.
The only thing that controls the music is that if statement. So if you only reverse the logic sense of the statement the switch should still control the music in the same way but reversed. What you are reporting is logically inconsistent.
I can only think that either something else changes when you change it from LOW to HIGH or else there is some error you are making when doing this.
Try this:- get the code working as before with the switch starting and stopping the music but the wrong way round. Then just change the == in the if statement to != and see if that works.

Hmm I changed == to =! in the if statement but it won't upload. The message "Ivalue required as left operand of assignment" occurs.

EDIT: Sorry I just re-read. I tried != and that again stopped the music all together. HIGH remained the same as well playing the music automatically and stopping with the card.

Ok post the code with your changes, the one that doesn't work and I will take a look at it.

This is the code that currently plays no sound, that I was hoping would make sound once the card has touched the circuit.

// Project 11 - Melody Player
// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 9;     // the number of the pushbutton pin
const int speakerPin =  9;      // the number of the speaker pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int pin = 0;

int length = 15; // the number of notes 
char notes[] = "ccggaagffeeddc "; // a space represents a rest 
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; 
int tempo = 300;

void playTone(int tone, int duration) { 
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
     digitalWrite(speakerPin, HIGH);
     delayMicroseconds(tone);
     digitalWrite(speakerPin, LOW);
     delayMicroseconds(tone);
  }
}


void playNote(char note, int duration) { 
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; 
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; 
  // play the tone corresponding to the note name for (int i = 0; i < 8; i++) {
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) { 
      playTone(tones[i], duration);
     }
  }
} 

void setup() {
  // initialize the speaker pin as an output:
  pinMode(speakerPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     


  pinMode(speakerPin, OUTPUT);
}

void loop() { 
   // read the state of the pushbutton value:

if(digitalRead(buttonPin) != LOW) { // make the sound if you read LOW from the button.
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      playNote(notes[i], beats [i] * tempo);
    }
    
    // pause between notes
    delay(tempo / 2);
  }
 }
}

(deleted)