Go Down

Topic: HELP HELP URGENT URGENT!! (Read 389 times) previous topic - next topic

minz

Dec 21, 2015, 07:42 am Last Edit: Dec 21, 2015, 10:23 am by minz
I am a newbie in arduino and I am a little stuck :(

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:

Code: [Select]
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.


AWOL

#1
Dec 21, 2015, 09:28 am Last Edit: Dec 21, 2015, 09:28 am by AWOL
Is there a question?

Can we expect the code tags you missed to be added any time soon?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Paul Stoffregen

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

Code: [Select]

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


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

Whatever you do, STOP using delay().

jurs


marco_c

Code: [Select]
delay(100000000);

This 100,000,000 milliseconds, or 100,000 seconds. In other words nearly 28 hours. Seriously?
Arduino Libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com
Arduino++ blog https://arduinoplusplus.wordpress.com

BenKissBox

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

Grumpy_Mike

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

AWOL

Maybe the testing involves real-time monitoring.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

BenKissBox

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy