Go Down

Topic: how can I add a second function to the same switch? (Read 500 times) previous topic - next topic

martin_80x

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: [Select]
int brightness = 50;    // how bright the LED is, stores the brigthness value
and the
Code: [Select]

unsigned long startTime = 0;  // when did we begin pressing?
to use it with millis?
and the cases are set to brightness instead
Code: [Select]
analogWrite(REDPIN, brightness);   // set to brightness

so this is the sketch
Code: [Select]
#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;
}
}

PaulS

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.

martin_80x

#2
Sep 21, 2011, 01:49 am Last Edit: Sep 21, 2011, 02:27 am by martin_80x Reason: 1
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: [Select]
//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


Grumpy_Mike

Quote
Do I need to use millis?

Yes.

It is up to you if you start again.

BKnight760

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.

Go Up