Pages: [1]   Go Down
Author Topic: how can I add a second function to the same switch?  (Read 412 times)
0 Members and 1 Guest are viewing this topic.
Florida, Winter Springs
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your help with the other sketch ''from the beginning'', I got it working now.
what I want to do with this one is add a second function to the same switch
so the UPPIN can change cases and if I hold it change brightness too, same with DNPIN. 
In the sketch I added the
Code:
int brightness = 50;    // how bright the LED is, stores the brigthness value
and the
Code:
unsigned long startTime = 0;  // when did we begin pressing?
to use it with millis?
and the cases are set to brightness instead
Code:
analogWrite(REDPIN, brightness);   // set to brightness

so this is the sketch
Code:
#define REDPIN 3
#define GREENPIN 5
#define BLUEPIN 6

#define UPPIN 4
#define DNPIN 2

int btnPressCount = 0;

int btnStateUp = LOW;
int lastBtnStateUp = LOW;

int btnStateDn = LOW;
int lastBtnStateDn = LOW;

int brightness = 50;    // how bright the LED is, stores the brigthness value
unsigned long startTime = 0;  // when did we begin pressing?

void setup()
{
  pinMode(UPPIN, INPUT);     
  pinMode(DNPIN, INPUT);
 
  Serial.begin(9600);
}

void loop()
{
 btnStateUp = digitalRead(UPPIN);
if((btnStateUp == HIGH) && (lastBtnStateUp == LOW)) // Switch is pressed and was not
   btnPressCount++;       // Count this press
   lastBtnStateUp = btnStateUp;                     // Use btnPressCount
 
 btnStateDn = digitalRead(DNPIN);
 if((btnStateDn == HIGH) && (lastBtnStateDn == LOW)) // Switch is pressed and was not
   btnPressCount--;                                  // Count this press
   lastBtnStateDn = btnStateDn;                      // Use btnPressCount
     
     Serial.print("count = ");
     Serial.println(btnPressCount); 
  //this part mantains the counts equal to 0
  // so it doesn't make the btnPressCount unsigned
  if(btnPressCount < 0)  // <-- Add me
      btnPressCount = 0; // <-- And me
 
   btnPressCount = btnPressCount % 3;               // Keep count in the range 0 to 6
                 

  switch(btnPressCount)
{   
   case 0:
  analogWrite(REDPIN, brightness);   // set to brightness
  analogWrite(GREENPIN, 0);    // off
  analogWrite(BLUEPIN, 0);
     break;
   
    case 1:
  analogWrite(REDPIN, 0); 
  analogWrite(GREENPIN, brightness);   
  analogWrite(BLUEPIN, 0 );     
     break;
     
    case 2:
  analogWrite(REDPIN, 0);   
  analogWrite(GREENPIN, 0);   
  analogWrite(BLUEPIN, brightness);
     break;
}
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45962
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, you want to time how long the switch is pressed. In order to do that, you need to know when the switch transitions from not pressed to pressed and when it transitions from pressed to not pressed.

Record when each transition occurs. When the transition from pressed to not pressed occurs, base your action on how long the switch was pressed. Short press == one action. Long press == other action.

Add
unsigned long upPress, dnPress;
unsigned long upRelease, dnRelease;

Initialize these variables at the appropriate times.

In the (new) if block following the up switch release detection, determine how long the switch was pressed. Increment either the button press count variable or the mode, depending on how long the switch was pressed.
Logged

Florida, Winter Springs
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

question, do I have to start from scratch or keep working on this one?. Do I need to use millis?
I'm confused cause I'm looking at this example on how to fade but I guess is not even close on what you said
Code:
//checkwhether the button is being held down
 if((digitalRead(UPPIN) == HIGH) && (digitalRead(DNPIN) == LOW))
 {
   // if the button is held for more than 500ms.
   if (state == 1 && (millis() - startTime) > 500)
 
   brightness++;     //increment brightness by 1
« Last Edit: September 20, 2011, 07:27:12 pm by martin_80x » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 505
Posts: 31332
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Do I need to use millis?
Yes.

It is up to you if you start again.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could attach an interrupt to your function.  That way the function gets called automatically when the button is pushed / let go.

http://arduino.cc/en/Reference/AttachInterrupt

Simply use millis() to record when the button state changes (is pushed / let go).  Subtract the two times.  Then do a specific action based on the elapsed time.  Reset the timers whenever the button is let go... after an action is done.
Logged

Pages: [1]   Go Up
Jump to: