I need help with a program not working

I’m new to this forum, and I’m not sure if this was the correct spot to put this topic, but I’m making an Arduino program for a project in a Media and Technology class, and my program isn’t working, and I’m not sure what’s wrong. I’m trying to set up a program that plays a melody when I press a button, but I’ve uploaded the program and nothing’s happening. There are also some things that I’ve made into comments, as I’m just trying to figure out how to get the first melody to play, and I’m not setting up a second one at the moment.

I’m going to paste my current program down below, and I’d be grateful if someone could let me know where I went wrong.

#include “pitches.h”

int const buzzer1 = 2;
//int const buzzer2 = 3;
int const button = 4;
//int const touchsensor = 5;
int buttonValue;
//int touchValue;
int melody = {
NOTE_F5, NOTE_C6, NOTE_B6, NOTE_C6, NOTE_B6, NOTE_C6, NOTE_GS6, NOTE_F5
};

int noteDurations = {
2, 4, 8, 8, 8, 8, 4, 4

};

void setup() {
// put your setup code here, to run once:
pinMode(buzzer1, OUTPUT);
//pinMode(buzzer2, OUTPUT);
pinMode(button, INPUT);
//pinMode(touchsensor, INPUT);

}

void loop() {
// put your main code here, to run repeatedly:
buttonValue = digitalRead(button);
//touchValue = digitalRead(touchsensor);

if (buttonValue == 1) {
for (int thisNote = 0; thisNote < 8; thisNote++) {

int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);

noTone(8 );
}
}
else {
digitalWrite(buzzer1, LOW);
}
}

Note: In the noTone part, I had to put a space between the 8 and the ) because it turned into an emoji.

Edit: I have no idea how to use this website, can a moderator delete this topic?

When you're not sure how to do something, re the opening line of your post, it is often best to pause for a minute and read the directions. There is a post at the top of every board titled "How to Use This Forum". Read it and not only will you learn about the boards but also how to post code with none of it turning to smileys or italics.

The tendency to read first and write second is what separates the good programmers from the crappy ones.

The reason you don't hear anything is that you call noTone immediately after calling Tone so it turns the note back off before you have a chance to hear it. Tone isn't blocking.

oh ok thanks, i'll see if i can move this topic to a related forum area

What forum would you like this moved to?

I think you're in the right section. It's the code tags you forgot. But if you would actually read what I pointed you to you'd know that.

Hello anyone,

I need some help in my project, I'm programming a Wireless Temperature and Humidity Monitor With ESP8266 to link to ThingSpeak channel filed and having problems with my code @: know response to display the data,

line @ "bool"

void gettemperature(){
// To wait at leaast 2 seconds between measurements and showing
// how you can change the values on the temp
// if there are different values between the current time and last time
// on sensor are bigger than the initial reading you set, this mins that the sensor worked better than delays.
// you have to unsigned long current Millis
unsigned long currentMillis = millis();

if(currentMillis - previousMillis >= interval){

// To save the last sensor readings
previousMillis = currentMillis;

Serial.println("AT"); // To see the AT information
delay(5000); //can very e.g 10000=10ms or 5000=5ms

if(Serial.find("OK")){
//connect to your wifi netowork
@ boolen connected = connectWiFi();
}else{
if(!connected){
//failure, need to check your values and try again

Error();

}else{

Error();
}

First, don't hijack someone else's thread.

Hello evanmars,

If you know the answer to this problem why not help instead, I'm doing this project for my final year project not threading it.

I just need some help and if you can then please do,

username12345678907:
I’m trying to set up a program that plays a melody when I press a button, but I’ve uploaded the program and nothing’s happening.

#include "pitches.h"

int const buzzer1 = 2;
int const button = 4;

int buttonValue;

int melody = {
NOTE_F5, NOTE_C6, NOTE_B6, NOTE_C6, NOTE_B6, NOTE_C6, NOTE_GS6, NOTE_F5
};

int noteDurations = {
2, 4, 8, 8, 8, 8, 4, 4
};

void setup() {
// put your setup code here, to run once:
pinMode(buzzer1, OUTPUT);
pinMode(button, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
buttonValue = digitalRead(button);

if (buttonValue == 1) {
for (int thisNote = 0; thisNote < 8; thisNote++) {

int noteDuration = 1000 / noteDurations[thisNote];
  tone(8, melody[thisNote], noteDuration);
 
  noTone(8 );
}
}
else {
digitalWrite(buzzer1, LOW);
}
}

The sketch looks functional (poorly formatted but technically correct) and should work. My guess is that either the button is wired incorrectly so the input pin never reads HIGH (1) or the speaker output is wired incorrectly so you don’t hear the playing, or both. If you use Pin 13 (on an UNO) for your Tone pin you should see the onboard LED light dimly (half brightness) when the music is ‘playing’. If the LED lights when you push the button then the button is good and the speaker is bad. If it doesn’t light then the button isn’t working.

I just noticed: You declare 'buzzer1' to be 2 but you tell Tone() to play on Pin 8. If you have your speaker wired to Pin 2 that would explain why you don't hear the output.

https://www.arduino.cc/en/Reference/Tone

NetworkingStudio:
Hello evanmars,

If you know the answer to this problem why not help instead, I'm doing this project for my final year project not threading it.

I just need some help and if you can then please do,

The reason not to hijack because you are blocking some one else post. Also it leads confusions to other member to understand which issue is discussing here. It's a good practice to post the question in a different thread than posting an unrelated question in an existing thread.

In this forum no one is getting paid for answering questions, every one is doing a free service to help fellow members. So please be courteous to some one trying to help you.

NetworkingStudio:
If you know the answer to this problem why not help instead,

Can you imagine the confusion to other members, not least the OP, to get two completely unrelated sets of answers in the same thread?

I agree with sarouje and evanmars that your ESP8266 question should be split out.