HELP HELP URGENT URGENT!!

I am a newbie in arduino and I am a little stuck :frowning:

I am trying to use digitalwrite to activate my buzzer instead of analogwrite.
The purpose is to: press and release a push button and a short beep will activated and stop. The second time you press and release the button 2 short beep will activated and stop. Until the 5th time you press it will automatically reset back to 1 short beep.

Here's my code:

const int buttonPin = 2;  // Input
const int speakerPin =  3;   // Output

int value;                // variable for reading the value status
int value1;               // variable for reading the value1 status
int button;               // variable for reading the button status
int state;                // variable for reading the state



void setup()  // runs once.
{ 
 pinMode (speakerPin, OUTPUT);  // Initialise ledpin1 as output pin.
 pinMode (buttonPin, INPUT); // Initialise buttonpin as input pin.
 
}

void loop()
{
 value = digitalRead(buttonPin);        // read input value and store it in box
 delay(10);                         
 value1 = digitalRead(buttonPin);      // read the input again to check for bounces
 
 if (value == value1)                 // make sure we got 2 consistent readings
 {                 
   if (value != button)               // the button state has changed
   {          
     if (value == LOW)               // check if the button is pressed
     {                
       if (state == 0)
       {          
         state = 1; 
       }
       else
       {
         if (state == 1)
         {        
           state = 2;           
         }
         else
         {
           if (state == 2)
           {      
             state = 3;           
           }
           else
           {
             if (state == 3)
             { 
               state = 4;   
             }
              else
               {
                 if (state == 4)
               {      
                  state = 1;           
               }
              }
            }
         }
       }  
     }
   }
 }
   button = value; 


 /* LED lighting logic... */
 if (state == 1)   //State = 1 =  led 1 ON.
 {
   digitalWrite(speakerPin, HIGH);  //send 5V to pin 3on the Arduino 
   delay(1000);
   digitalWrite(speakerPin, LOW);
   delay(100000000);

 }

if (state == 2)
 {
   digitalWrite(speakerPin, HIGH);   
   delay(1000);
   digitalWrite(speakerPin, LOW);
   delay(1000);
   digitalWrite(speakerPin, HIGH);  
   delay(1000);
   digitalWrite(speakerPin, LOW);
   delay(100000000);

 }

if (state == 3)
 {
   digitalWrite(speakerPin, HIGH);  
   delay(2000);
   digitalWrite(speakerPin, LOW);
   delay(2000);
   digitalWrite(speakerPin, HIGH);  
   delay(2000);
   digitalWrite(speakerPin, LOW);
   delay(2000);
   digitalWrite(speakerPin, HIGH);   
   delay(2000);
   digitalWrite(speakerPin, LOW);
   delay(100000000);
 }
               
if (state == 4)
 {
   digitalWrite(speakerPin, HIGH);   
   delay(2000);
   digitalWrite(speakerPin, LOW);
   delay(2000);
   digitalWrite(speakerPin, HIGH);   
   delay(2000);
   digitalWrite(speakerPin, LOW);
   delay(2000);
   digitalWrite(speakerPin, HIGH);  
   delay(2000);
   digitalWrite(speakerPin, LOW);
   delay(2000);
   digitalWrite(speakerPin, HIGH);  
   delay(2000);
   digitalWrite(speakerPin, LOW);
   delay(100000000);


 }           
}

Thank you.

Is there a question?

Can we expect the code tags you missed to be added any time soon?

You're doing it wrong, on so many levels.

Do yourself a favor and use the Bounce library to read the button. It works. Then you can use

mybutton.update()
if (mybutton.fallingEdge()) {
  // do something
}

Then you can use the tone() function to create the beeps.

Whatever you do, STOP using delay().

minz:
Here's my code:

Here is the suitable song to your topic:

minz:
Thank you.

No problem and have a nice day!

delay(100000000);

This 100,000,000 milliseconds, or 100,000 seconds. In other words nearly 28 hours. Seriously?

Hello Minz,

it is useless to say you are beginner, we see it in your code (just joking)

But it's rather hard to tell you what to do, since you do not explain what you expect from us.

First, I see many issues with your code :

  • make it clear using a switch/case structure rather than if/else cascade. This is awful to read and understand

  • what do you expect from the speaker by sending a '1' or a '0' for one second? The loudspeaker will never sound, you will just hear a "cloc" every time. If you expect to get a sound from it, you have to use Tone, not a logical level

  • the delay values are HUGE, HUGE!!! It will takes hours for your sketch to run (the delay(100000000) will stop the sketch for one day!! How did you get such values

  • I think that you get the Arduino to be multitasking or something like that. Do not forget that when you are in a delay(), everything else is blocked, except if you have implemented an interrupt

Benoit

It can't be that urgent if he has not come back to the thread for three days.

Maybe the testing involves real-time monitoring.

Maybe I should have not made the joke at the beginning of my answer...