Pages: [1]   Go Down
Author Topic: Button Help!!!!!  (Read 797 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need to create a climate control system, So far it gets the temperature switches different leds on depending on the temp and pulses a fan at different speeds also depending on the temp. If it goes above a certain temp it will sound an alarm using a piezo buzzer and only operates when the light is at a sufficient level(defined by me).

What i have to do now is create an alarm reset code using button pushes e.g 3 button pushes in succession that disables the alarm for 5 seconds. i tried by inserting a function in my code to read the state of the button. But unless i get the timing exactly right and press the button when the arduino attempts to read the state either HIGH or LOW it wont work.

 Resulting in me mashing the button and then 10secs later it reads the button. Any ideas would be greatly appreciated?

here is a snippet of the code im having trouble with smiley-grin

Code:
                  if ( temp <= 303)   // if temp is less than 30deg celcius switch on all three leds and pulse the fan
                     {
                       digitalWrite(LED1 ,HIGH);
                       digitalWrite(LED2 ,HIGH);
                       digitalWrite(LED3 ,HIGH);
                       sysState=1;
                       digitalWrite(fanPin, HIGH);
                       delay(250);
                       digitalWrite(fanPin, LOW);
                       delay(250);
                     }
                     else  // if temp was not less than 30deg setalarm()
                          
                          setAlarm();
                          
                          
        
     }  
  else
   {
       Serial.println("Lights are off or too dim to operate"); // test to see if light levels are sufficient
       sysState = 0;
       digitalWrite(LED1 ,LOW);
       digitalWrite(LED2 ,LOW);
       digitalWrite(LED3 ,LOW);
     }
  }

 void setAlarm()
 {
   long frequency = 523; //C5 tone frequency in HZ.
   long length = 1000; // Required tone length in milliseconds
  
   long delayValue = 1000000/frequency/2;
   long numCycles = frequency * length/1000;
  
  if(alarmState=1)  // alarm state is 1 by default
   {
     Serial.println("Alarm!!");
     for (long i=0; i < numCycles; i++)
     {
       digitalWrite(speakerPin,HIGH);
       delayMicroseconds(delayValue);
       digitalWrite(speakerPin,LOW);
       delayMicroseconds(delayValue);
     }
   }
   else
     Serial.println("You Have diasbaled the alarm"); // if alarm state is not 1 then alarm must have been disable
 
 }
 
 void buttonStuff() // here is where i dont know what to do!!!!!!!!
 {
   Serial.println("Press the button once to ");
    delay(250);
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH)
     {
       alarmState=0; // set alarm state to 0 therefore it will skip over the setAlarm() function and print a message saying alarm has been disabled.
       Serial.println("Alarm OFF");
     }
}  

I was first just trying to disabled the alarm completely until i could read the button state properly.
« Last Edit: September 27, 2011, 10:43:52 pm by qwertymnbvc » Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 626
Posts: 34183
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
here is a snippet of the code in general
It would help if you posted the bit of code you are having trouble with.

Quote
3 button pushes in succession that disables the alarm for 5 seconds.
So how long between button presses do you need? What happens if three don't occur in the time allotted for the presses?

Basically you need to implement a state variable, it increments each time it sees a new button press and resets it there has been no button press for x seconds. When it reaches 3 it sets a flag that disables your alarm and sets a time when the alarm should be disabled. When this time times out clear the disable flag.
Use the disable flag to over ride the alarm.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Basically you need to implement a state variable, it increments each time it sees a new button press and resets it there has been no button press for x seconds. When it reaches 3 it sets a flag that disables your alarm and sets a time when the alarm should be disabled. When this time times out clear the disable flag. Use the disable flag to over ride the alarm.

i edited my code and added some comments. i tried to create a counter variable that increased it by 1 each time a button press was detected and when it reached 3 it would print a message(just for testing purposes) but the only way i could reach 3 recorded button presses was to continually press it and after about 30 button presses it had recorded 3. i need to press the button 3 times throughout 1 iteration of the program and this will disable the alarm for 5secs. here is how i imagine it should work , i just initialize the counter to 0 at the start of the loop() and then somehow(i dont know how) it needs to be able to detect three button presses and chuck in a delay of 5secs just before the alarm would sound because all that would do is halt the alarm and the temp has already been processed and the applicable fan speed as well. The difficulty i have is i cant get my program to detect the change in state for the button presses!
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 626
Posts: 34183
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Do not initialise the count variable at the start of loop. Make it a static variable defined outside of any function.
Each time round the loop check for two things:-
1) a new button press, if so increment the variable and reset the time out timer, if this exceeds three then set your inhibit flag and set a time out on the inhibit flag.
2) check if the to out timer has expired, if so clear the variable back to zero
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok thanks. i will try that. but i dont understand what you mean by
Quote
1) a new button press, if so increment the variable and reset the time out timer, if this exceeds three then set your inhibit flag and set a time out on the inhibit flag.
so i check for a new button press but dont understand all the stuff about flags and "time out timer"

Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 626
Posts: 34183
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but dont understand all the stuff about flags and "time out timer"
Look at the blink without delay example in your arduino IDE. That shows the use of millis() to decide when things happen. In the case of the example it is turning an LED on and off, for you it is the setting or clearing of flags that control the rest of your code.

That is the technique you need to use to achieve what you want.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 if(alarmState=1)  // alarm state is 1 by default
But, lets assign it a value of 1 just to be sure, and then test the result of that assignment.

Is that really what you want to do?

Check out the various button libraries available. At least one of them has a uniquePress method. It will return true if the switch was pressed, even if you aren't looking at the switch at the time.
« Last Edit: September 28, 2011, 05:32:41 am by PaulS » Logged

Pages: [1]   Go Up
Jump to: