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
///////////////////////////////////////////////////////////////////////
Thanks, Mox.
///////////////////////////////////////////////////////////////////////
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: