RGB torch code _ Help to add a long press function

Thread Starter

Moxxie

Joined Mar 2, 2022
4
So I've been working on an RGB torch for a little while but I'm struggling to get over the last hurdle. The code I have written is working but I'd like to add a long press function to buttons 1 and 2 to change the colour quickly rather than having to cycle through all the brightness levels, I have tried many different things to get it to work but no such luck, any help would be appreciated.

///////////////////////////////////////////////////////////////////////
Mod: placed Code into Code Tags

C-like:
#include <Pushbutton.h>


int count = 0;


#define button1 2

#define button2 3

#define button3 4


Pushbutton pbutton1(button1);

Pushbutton pbutton2(button2);

Pushbutton pbutton3(button3);


void setup() {

  pinMode (5, OUTPUT);

  pinMode (6, OUTPUT);

  pinMode (9, OUTPUT);

}


void loop() {

  digitalWrite (5,LOW);

  digitalWrite (6,LOW);

  digitalWrite (9,LOW);



    if (pbutton1.isPressed()){

      count = count + 1;                // Button 1 increments the program forwards

      delay(100);

    }

    if (pbutton2.isPressed()){

      count = count - 1;                // Button 2 increments the program backwards

      delay(100);

    }

    if (pbutton3.isPressed()){

      count = count = 0;                // Button 3 resets the program back to the start

      delay(100);

    }


if (count == -1){

  count = 13;

  delay (100);

}



if (count == 0) {

  digitalWrite (5,LOW);

  digitalWrite (6,LOW);

  digitalWrite (9,LOW);

  delay (100);

}


//Red:

   

if (count == 1){

analogWrite (5, 63);

digitalWrite (6,LOW);

digitalWrite (9,LOW);

delay (100);

  }



if (count == 2){

analogWrite (5, 126);

digitalWrite (6,LOW);

digitalWrite (9,LOW);

delay (100);

}


if (count == 3){

analogWrite (5, 189);

digitalWrite (6, LOW);

digitalWrite (9, LOW);

delay (100);

}


if (count == 4){

analogWrite (5, 255);

digitalWrite (6, LOW);

digitalWrite (9, LOW);

delay (100);

}


//Green:


if (count == 5){

digitalWrite (5,LOW);

analogWrite (6, 63);

digitalWrite (9,LOW);

delay (100);

  }



if (count == 6){

digitalWrite (5,LOW);

analogWrite (6, 126);

digitalWrite (9,LOW);

delay (100);

}


if (count == 7){

digitalWrite (5,LOW);

analogWrite (6, 189);

digitalWrite (9,LOW);

delay (100);

}


if (count == 8){

digitalWrite (5,LOW);

analogWrite (6, 255);

digitalWrite (9,LOW);

delay (100);

}


//Blue:


if (count == 9){

digitalWrite (5,LOW);

digitalWrite (6,LOW);

analogWrite (9, 63);

delay (100);

  }



if (count == 10){

digitalWrite (5,LOW);

digitalWrite (6,LOW);

analogWrite (9, 126);

delay (100);

}


if (count == 11){

digitalWrite (5,LOW);

digitalWrite (6,LOW);

analogWrite (9, 189);

delay (100);

}


if (count == 12){

digitalWrite (5,LOW);

digitalWrite (6,LOW);

analogWrite (9, 255);

delay (100);

}


//White:


if (count == 13){

digitalWrite (5, HIGH);

digitalWrite (6, HIGH);

digitalWrite (9, HIGH);

delay (100);

}


if (count == 14){

  count = 0;

  delay (100);

}

    }
///////////////////////////////////////////////////////////////////////



Thanks, Mox.
 
Last edited by a moderator:

Marc Sugrue

Joined Jan 19, 2018
222
So I've been working on an RGB torch for a little while but I'm struggling to get over the last hurdle. The code I have written is working but I'd like to add a long press function to buttons 1 and 2 to change the colour quickly rather than having to cycle through all the brightness levels, I have tried many different things to get it to work but no such luck, any help would be appreciated.

///////////////////////////////////////////////////////////////////////
Mod: placed Code into Code Tags

C-like:
#include <Pushbutton.h>


int count = 0;


#define button1 2

#define button2 3

#define button3 4


Pushbutton pbutton1(button1);

Pushbutton pbutton2(button2);

Pushbutton pbutton3(button3);


void setup() {

  pinMode (5, OUTPUT);

  pinMode (6, OUTPUT);

  pinMode (9, OUTPUT);

}


void loop() {

  digitalWrite (5,LOW);

  digitalWrite (6,LOW);

  digitalWrite (9,LOW);



    if (pbutton1.isPressed()){

      count = count + 1;                // Button 1 increments the program forwards

      delay(100);

    }

    if (pbutton2.isPressed()){

      count = count - 1;                // Button 2 increments the program backwards

      delay(100);

    }

    if (pbutton3.isPressed()){

      count = count = 0;                // Button 3 resets the program back to the start

      delay(100);

    }


if (count == -1){

  count = 13;

  delay (100);

}



if (count == 0) {

  digitalWrite (5,LOW);

  digitalWrite (6,LOW);

  digitalWrite (9,LOW);

  delay (100);

}


//Red:

 

if (count == 1){

analogWrite (5, 63);

digitalWrite (6,LOW);

digitalWrite (9,LOW);

delay (100);

  }



if (count == 2){

analogWrite (5, 126);

digitalWrite (6,LOW);

digitalWrite (9,LOW);

delay (100);

}


if (count == 3){

analogWrite (5, 189);

digitalWrite (6, LOW);

digitalWrite (9, LOW);

delay (100);

}


if (count == 4){

analogWrite (5, 255);

digitalWrite (6, LOW);

digitalWrite (9, LOW);

delay (100);

}


//Green:


if (count == 5){

digitalWrite (5,LOW);

analogWrite (6, 63);

digitalWrite (9,LOW);

delay (100);

  }



if (count == 6){

digitalWrite (5,LOW);

analogWrite (6, 126);

digitalWrite (9,LOW);

delay (100);

}


if (count == 7){

digitalWrite (5,LOW);

analogWrite (6, 189);

digitalWrite (9,LOW);

delay (100);

}


if (count == 8){

digitalWrite (5,LOW);

analogWrite (6, 255);

digitalWrite (9,LOW);

delay (100);

}


//Blue:


if (count == 9){

digitalWrite (5,LOW);

digitalWrite (6,LOW);

analogWrite (9, 63);

delay (100);

  }



if (count == 10){

digitalWrite (5,LOW);

digitalWrite (6,LOW);

analogWrite (9, 126);

delay (100);

}


if (count == 11){

digitalWrite (5,LOW);

digitalWrite (6,LOW);

analogWrite (9, 189);

delay (100);

}


if (count == 12){

digitalWrite (5,LOW);

digitalWrite (6,LOW);

analogWrite (9, 255);

delay (100);

}


//White:


if (count == 13){

digitalWrite (5, HIGH);

digitalWrite (6, HIGH);

digitalWrite (9, HIGH);

delay (100);

}


if (count == 14){

  count = 0;

  delay (100);

}

    }
///////////////////////////////////////////////////////////////////////



Thanks, Mox.
Try implementing a MOVING AVERAGE FILTER where a speed variable will increase the longer you hold the button. Think of it as a shift register where you populate the data at regular intervals and calulate the average of the variables. Old values will drop off the filter and new ones get added. Start with something like 10 variables which update every 0.5 seconds as a start, speed will then increment every 0.5s until it reaches your max rate. As soon as the button is released then clear the filter elements for next time.
 
Last edited:

click_here

Joined Sep 22, 2020
548
+1 for the finite state machine

But keeping with your code you can do something like this..
Code:
if(b1 && b2)
  ...
else if(b1)
  ...
else if(b2)
  ...
else if(b3)
  ...
 
Top