button problem

I want to make a project in which a buzzer beeps after a delay when you press a button( its part of a big project)
but I don’t know why you need to hold down the button for about 1 second before it beeps
here’s the code:

#include <neotimer.h>

int buttonState = 0;
const int buttonPin = 2;

int piezoPin = 3;

void loop()
{
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) 
  {
  Neotimer mytimer = Neotimer(1800);

  void setup()
  {
    mytimer.start();
  }

  void loop()
  {
    if(mytimer.done())
    {
    tone (piezoPin,2200,1000);
}

Two loops !
Post all the code.

okay

#include <neotimer.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);


#define echoPin 6 // Echo Pin
#define trigPin 13 // Trigger Pin
#define SPEED_SOUND 58.2 //speed of sound

//buttonState = digitalRead(buttonPin);

int piezoPin = 3;
int  duration = pulseIn(echoPin, HIGH);
int distance = duration / SPEED_SOUND;
int sensorValue1 = analogRead(A0);
int sensorValue2 = analogRead(A1);
int buttonState = 0;

const int buttonPin = 2;

void UltrasoundSystem (void)
{ 
 long duration, distance;
 
 digitalWrite(trigPin, LOW);
 delay(2);

 digitalWrite(trigPin, HIGH);
 delay(10);

 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);

 distance = duration / SPEED_SOUND;

 Serial.print("distance:");
 Serial.println(distance);
 Serial.println("/r");
 delay(1000);
 lcd.setCursor(10,0);
 lcd.print(distance);
 delay(200);

 if (distance < 31)
 {
   Serial.println("too close!");
   //lcd.setCursor(4,1);
   //lcd.write("too close!");
   //tone(piezoPin, 2200, 1000);
   //delay(1000);
   //lcd.clear();
 }
}

void LightSystem (void)
{
 int sensorValue1 = analogRead(A0);
 int sensorValue2 = analogRead(A1);
 Serial.print("sensor value 1:");
 Serial.println(sensorValue1);
 Serial.println("/r");
 delay(1000);
 Serial.print("sensor value 2:");
 Serial.println(sensorValue2);
 Serial.println("/r");
 lcd.setCursor(0,0);
 lcd.print(sensorValue1);
 lcd.setCursor(5,0);
 lcd.print(sensorValue2);
 if (sensorValue1>sensorValue2)
{
 //lcd.setCursor(7,0);// x,y
 //lcd.print("too dim!");
 Serial.println("too dim!");
 //tone(piezoPin, 2200, 1000);
 //delay(1000);
 //lcd.clear();
}
 }

void ButtonTimer (void)
{
 const int buttonPin = 2;     // the number of the pushbutton pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
 // 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) 
 {
 Neotimer mytimer = Neotimer(1800);//add 000 at end after test
//delay(1800000);
 void setup()
 {
   mytimer.start();
 }

 void loop()
 {
   if(mytimer.done())
   {
   tone (piezoPin,2200,1000);
   Serial.println("time's up!");
   }
 }
   
  
 }
}
void setup()
{
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
}

void loop()
{
 UltrasoundSystem();
 LightSystem();
 ButtonTimer();
}

and i got this error message:

Arduino: 1.8.5 (Windows 8), Board: "Arduino/Genuino Uno"



ultrasonicLightSensor:95: error: a function-definition is not allowed here before '{' token

  {

  ^

ultrasonicLightSensor:100: error: a function-definition is not allowed here before '{' token

  {

  ^

ultrasonicLightSensor:123: error: expected '}' at end of input

}

^

ultrasonicLightSensor:123: error: expected '}' at end of input

exit status 1
a function-definition is not allowed here before '{' token

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

You are supposed to post your code using the </> button.
Your code does not compile, though you implied that it ran.

Why have you got two loop functions?
You need to get rid of your compilation errors.
Comment out large sections of code so that you have a skeleton that compiles cleanly, then gradually add code back.

hi!
im new
i got this really big project, and then when i compile it, it gives me this:
Arduino: 1.8.5 (Windows 8), Board: “Arduino/Genuino Uno”

ultrasonicLightSensor:95: error: a function-definition is not allowed here before ‘{’ token

{

^

ultrasonicLightSensor:100: error: a function-definition is not allowed here before ‘{’ token

{

^

ultrasonicLightSensor:123: error: expected ‘}’ at end of input

}

^

ultrasonicLightSensor:123: error: expected ‘}’ at end of input

exit status 1
a function-definition is not allowed here before ‘{’ token
my code: (really long!)

#include <neotimer.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);


#define echoPin 6 // Echo Pin
#define trigPin 13 // Trigger Pin
#define SPEED_SOUND 58.2 //speed of sound

//buttonState = digitalRead(buttonPin);

int piezoPin = 3;
int  duration = pulseIn(echoPin, HIGH);
int distance = duration / SPEED_SOUND;
int sensorValue1 = analogRead(A0);
int sensorValue2 = analogRead(A1);
int buttonState = 0;

const int buttonPin = 2;

void UltrasoundSystem (void)
{ 
  long duration, distance;
  
  digitalWrite(trigPin, LOW);
  delay(2);

  digitalWrite(trigPin, HIGH);
  delay(10);

  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);

  distance = duration / SPEED_SOUND;

  Serial.print("distance:");
  Serial.println(distance);
  Serial.println("/r");
  delay(1000);
  lcd.setCursor(10,0);
  lcd.print(distance);
  delay(200);

  if (distance < 31)
  {
    Serial.println("too close!");
    //lcd.setCursor(4,1);
    //lcd.write("too close!");
    //tone(piezoPin, 2200, 1000);
    //delay(1000);
    //lcd.clear();
  }
}

void LightSystem (void)
{
  int sensorValue1 = analogRead(A0);
  int sensorValue2 = analogRead(A1);
  Serial.print("sensor value 1:");
  Serial.println(sensorValue1);
  Serial.println("/r");
  delay(1000);
  Serial.print("sensor value 2:");
  Serial.println(sensorValue2);
  Serial.println("/r");
  lcd.setCursor(0,0);
  lcd.print(sensorValue1);
  lcd.setCursor(5,0);
  lcd.print(sensorValue2);
  if (sensorValue1>sensorValue2)
{
  //lcd.setCursor(7,0);// x,y
  //lcd.print("too dim!");
  Serial.println("too dim!");
  //tone(piezoPin, 2200, 1000);
  //delay(1000);
  //lcd.clear();
}
  }

void ButtonTimer (void)
{
  const int buttonPin = 2;     // the number of the pushbutton pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
  // 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) 
  {
  Neotimer mytimer = Neotimer(1800);//add 000 at end after test
//delay(1800000);
  void setup()
  {
    mytimer.start();
  }

  void loop()
  {
    if(mytimer.done())
    {
    tone (piezoPin,2200,1000);
    Serial.println("time's up!");
    }
  }
    
   
  }
}
void setup()
{
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop()
{
  UltrasoundSystem();
  LightSystem();
  ButtonTimer();
}

line 100 has the problem, but i dont know what problem!

You have a setup() and loop() function inside of the ButtonTimer function. You cannot define a function inside of another function.

You can't have more than one setup() function or loop() function in a sketch.
combine the code in the setup functions into one setup function and the same for loop.

Your indenting could be improved to make your code easier to resd. Autoformat (ctrl-t or Tools, Auto Format) will indent your code in a standard way. I see that you tried to use code tags. Thanks for that, but the opening tag should be not have the '/' just the closing tag has '/'.

I provided some help in your other thread. Posting the same question in multiple threads is cross posting. Please do not cross post.

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH)
{
Neotimer mytimer = Neotimer(1800);//add 000 at end after test
//delay(1800000);
void setup()
{

Do you REALLY believe that you can define setup() in the middle of ButtonTimer()? The compiler KNOWS that you can't.

so i just take away the void setup?
sorry for bad question, i am a beginner

This is why cross posting is bad. I answered that question in the other thread (that you haven’t bothered to read, i guess).

You can’t have more than one setup() function or loop() function in a sketch.
combine the code in the setup functions into one setup function and the same for loop.

okay sorry, so cross posting is posting the same project in different topics?

Yes.

Do you actually read what is posted?

From reply #1 of this thread.

Posting the same question in multiple threads is cross posting.

so what resistance should i use for the button?
origanally, it should be 10k ohm, but since the power is shared with lcd, ultrasound sensor and piezo, it doesnt work when i use 10k
the power is 5v from the arduino uno

devcoder:
so what resistance should i use for the button?

Wait. WHAT?!?

You ask a coding question and post your code.
Multiple people try to help you with your code problems and ask you to make changes to how you posted your code, how you format your code and how you constructed your code.

And from all this, you ask about the value of a resistor?

cross posted at http://forum.arduino.cc/index.php?topic=527194.0.

 delay(1000);

THAT is why you have to hold the button down for a second for it to be recognized.

Threads merged.

Darn those electronic things,

always doing what their told.

Curse them.

wait, but I dont have a delay 1 sec in the button code, and i don't know which one,cause i have several af them in the whole project.

devcoder:
wait, but I dont have a delay 1 sec in the button code, and i don't know which one,cause i have several af them in the whole project.

What I don't understand is how you can get a delay when you said your code gives compile errors.
Why don't you;

  • Get some code that compiles cleanly (with lots of diagnostic print statements in it ).
  • Post the full code.
  • Describe fully how the code actually fails to do what you are expecting.