Go Down

Topic: rgb code with rgb anode momentary switch (Read 309 times) previous topic - next topic

casualgamer24

Alright, I am getting nowhere with programming the arduino how I would like it built. Could someone program it?

So i have an arduino nano ATmega328,one rgb anode momentary switch and one rgb cathode led. I am using the digital side.
How I want it to work is when the arduino is on, the rgb anode momentary switch blinks green and when you hold down the momentary switch for 3 seconds it turns the led on. When you hold the momentary button down for 3 seconds again it turns the led off. Secondly, when the rgb cathode led is on you can cycle through the colors by pressing the button twice in 3 seconds and the color will stay lit until you press the button to cycle the next color. Lastly, when you cycle through the colors and turn off the rgb cathode led it will turn back on at the color you last had it. I would Also like the rgb anode momentary switch the same color as the rgb cathode led at the same time, 10 colors: white,red,orange,yellow,green,agua,blue,purple,pink,gold.

Ok so  heres a short version:
arduino on = rgb anode momentary switch blink green
hold rgb anode momentary switch 3 seconds = power on rgb cathode led to color you last left it(example yellow or orange). hold rgb anode momentary switch 3 seconds = power off led
double press rgb anode momentary switch = cycle through rgb cathode led and rgb anode switch colors. The rgb anode momentary switch changes to the same color as the rgb led at the same time.Example: red double press green double press purple double press yellow.

thank you.

PaulRB

You are simply asking for someone to write this sketch for you?

Grumpy_Mike

Such arbitrary behaviour requirements sounds like this is a class assignment, are you wanting to cheat?

casualgamer24

yes and no its not a class assignment. I graduated collage 3 years ago

Grumpy_Mike

Not sure what to make of that. A straight forward reading says
it is not a class assignment
but I do want to cheat

Implying it is some sort of assignment.

Basically what we do here is help. That means we will not write code for you, but we will help with the code structure, where you are having trouble understanding concepts and even when you get stuck with syntax of the code.

So what you are asking has no practical use other than to show your ability to break the problem down and code.

Therefore rather than hit us with "How I want it to work is when ....." you need to break things down. Their is a lot of complication in what you want so you need to break it down.

You start with the schematic. That is you show how things are wired so that you can tell what state to set the output pins and how to interpreter what you read from the input pins. A link to these wired sounding switch / LEDs would be a good start.

Then you write code to just do one thing in that list. You test it, get it working and understand the code that is making it work.

Then you add the next requirement and get that working and so on until your project is complete.

It looks like you have operations and conditions going on at what will look like the "same time". Therefore you will need to look at the millis call and how to use it. You can start by looking at my http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0

When you get stuck, post the code you have and say what it does.
Read How to use this forum because it will tell you how to post code correctly. If you don't post code correctly after being told how to people get a bit cross with you for taking the piss. That link will tell you how to respond to any answers you get back.

casualgamer24

Hello,
I have a code that is need help that changes the rgb color on momentary button double click.

I would like 10 colors
white
double click
red
double click
orange
double click
yellow
double click
ect...

also the toggle on/off button hold I would like when power on the rgb led it goes to the last color that it was on.
please help, here is the code:

Code: [Select]

#define buttonPin 8        // analog input pin to use as a digital input
#define ledPin1 2          // digital output pin for LED 1
#define ledPin2 4          // digital output pin for LED 2
#define ledPin3 6          // digital output pin for LED 3


// LED variables
boolean ledVal1 = true;    // state of LED 1
boolean ledVal2 = true;    // state of LED 2
boolean ledVal3 = true;    // state of LED 3


//=================================================

void setup() {
   // Set button input pin
   pinMode(buttonPin, INPUT);
   digitalWrite(buttonPin, HIGH );
   // Set LED output pins
   pinMode(ledPin1, OUTPUT);
   digitalWrite(ledPin1, ledVal1);
   pinMode(ledPin2, OUTPUT);
   digitalWrite(ledPin2, ledVal2);
   pinMode(ledPin3, OUTPUT);   
   digitalWrite(ledPin3, ledVal3);
}

void loop() {
   // Get button event and act accordingly
   int b = checkButton();
   if (b == 1) doubleClickEvent();
   if (b == 2) holdEvent();
}

//=================================================
// Events to trigger

void doubleClickEvent() {
   ledVal2 = !ledVal2;
   digitalWrite(ledPin2, ledVal2);
}
void setColor(int red,int green,int blue){
#ifdef COMMON_ANODE
    red =255-red;
   green =255-green;
   blue =255-blue;

#endif
analogWrite(ledPin1,red);
  analogWrite(ledPin2,green);
  analogWrite(ledPin3, blue);
}
void holdEvent() {
   ledVal3 = !ledVal3;
   digitalWrite(ledPin3, ledVal3);
}
//=================================================
//  MULTI-CLICK:  One Button, Multiple Events

// Button timing variables
int debounce = 20;          // ms debounce period to prevent flickering when pressing or releasing the button
int DCgap = 250;            // max ms between clicks for a double click event
int holdTime = 2000;        // ms hold period: how long to wait for press+hold event
//int longHoldTime = 3000;    // ms long hold period: how long to wait for press+hold event

// Button variables
boolean buttonVal = HIGH;   // value read from button
boolean buttonLast = HIGH;  // buffered value of the button's previous state
boolean DCwaiting = false;  // whether we're waiting for a double click (down)
boolean DConUp = false;     // whether to register a double click on next release, or whether to wait and click
boolean singleOK = true;    // whether it's OK to do a single click
long downTime = -1;         // time the button was pressed down
long upTime = -1;           // time the button was released
boolean ignoreUp = false;   // whether to ignore the button release because the click+hold was triggered
boolean waitForUp = false;        // when held, whether to wait for the up event
boolean holdEventPast = false;    // whether or not the hold event happened already
//boolean longHoldEventPast = false;// whether or not the long hold event happened already

int checkButton() {   
   int event = 0;
   buttonVal = digitalRead(buttonPin);
   // Button pressed down
   if (buttonVal == LOW && buttonLast == HIGH && (millis() - upTime) > debounce)
   {
       downTime = millis();
       ignoreUp = false;
       waitForUp = false;
       singleOK = true;
       holdEventPast = false;
       //longHoldEventPast = false;
       if ((millis()-upTime) < DCgap && DConUp == false && DCwaiting == true)  DConUp = true;
       else  DConUp = false;
       DCwaiting = false;
   }
   // Button released
   else if (buttonVal == HIGH && buttonLast == LOW && (millis() - downTime) > debounce)
   {       
       if (not ignoreUp)
       {
           upTime = millis();
           if (DConUp == false) DCwaiting = true;
           else
           {
               event = 2;
               DConUp = false;
               DCwaiting = false;
               singleOK = false;
           }
       }
   }
   // Test for normal click event: DCgap expired
   if ( buttonVal == HIGH && (millis()-upTime) >= DCgap && DCwaiting == true && DConUp == false && singleOK == true && event != 2)
   {
       event = 1;
       DCwaiting = false;
   }
   // Test for hold
   if (buttonVal == LOW && (millis() - downTime) >= holdTime) {
       // Trigger "normal" hold
       if (not holdEventPast)
       {
           event = 3;
           waitForUp = true;
           ignoreUp = true;
           DConUp = false;
           DCwaiting = false;
           //downTime = millis();
           holdEventPast = true;
       }
       
       // Trigger "long" hold
      // if ((millis() - downTime) >= longHoldTime)
     //  {
         //  if (not longHoldEventPast)
         //  {
             //  event = 4;
             //  longHoldEventPast = true;
          // }
       //}
   }
   buttonLast = buttonVal;
   return event;
}

casualgamer24

okay currently I have a code that if i double press a momentary button twice the color green is on and if i double press it again it turns off. Also if i hold the button for around 3 seconds the led red turns on and  if i hold it again it turns off.

I im still not sure how to make it to where it i double press the button it will change to a different color
example
white
double press
blue
double press
yellow
double press
purple
ect..

the hold button would be to turn on led based on last color left on and turn off led.

Here is the code:
Code: [Select]

#define buttonPin 8        // analog input pin to use as a digital input
#define ledPin1 2          // digital output pin for LED 1
#define ledPin2 4          // digital output pin for LED 2
#define ledPin3 6          // digital output pin for LED 3


// LED variables
boolean ledVal1 = true;    // state of LED 1
boolean ledVal2 = true;    // state of LED 2
boolean ledVal3 = true;    // state of LED 3


//=================================================

void setup() {
   // Set button input pin
   pinMode(buttonPin, INPUT);
   digitalWrite(buttonPin, HIGH );
   // Set LED output pins
   pinMode(ledPin1, OUTPUT);
   digitalWrite(ledPin1, ledVal1);
   pinMode(ledPin2, OUTPUT);
   digitalWrite(ledPin2, ledVal2);
   pinMode(ledPin3, OUTPUT);   
   digitalWrite(ledPin3, ledVal3);
}

void loop() {
   // Get button event and act accordingly
   int b = checkButton();
   if (b == 1) doubleClickEvent();
   if (b == 2) holdEvent();
}

//=================================================
// Events to trigger

void doubleClickEvent() {
   ledVal2 = !ledVal2;
   digitalWrite(ledPin2, ledVal2);
}
void setColor(int red,int green,int blue){
#ifdef COMMON_ANODE
    red =255-red;
   green =255-green;
   blue =255-blue;

#endif
analogWrite(ledPin1,red);
  analogWrite(ledPin2,green);
  analogWrite(ledPin3, blue);
}
void holdEvent() {
   ledVal3 = !ledVal3;
   digitalWrite(ledPin3, ledVal3);
}
//=================================================
//  MULTI-CLICK:  One Button, Multiple Events

// Button timing variables
int debounce = 20;          // ms debounce period to prevent flickering when pressing or releasing the button
int DCgap = 250;            // max ms between clicks for a double click event
int holdTime = 2000;        // ms hold period: how long to wait for press+hold event
//int longHoldTime = 3000;    // ms long hold period: how long to wait for press+hold event

// Button variables
boolean buttonVal = HIGH;   // value read from button
boolean buttonLast = HIGH;  // buffered value of the button's previous state
boolean DCwaiting = false;  // whether we're waiting for a double click (down)
boolean DConUp = false;     // whether to register a double click on next release, or whether to wait and click
boolean singleOK = true;    // whether it's OK to do a single click
long downTime = -1;         // time the button was pressed down
long upTime = -1;           // time the button was released
boolean ignoreUp = false;   // whether to ignore the button release because the click+hold was triggered
boolean waitForUp = false;        // when held, whether to wait for the up event
boolean holdEventPast = false;    // whether or not the hold event happened already
//boolean longHoldEventPast = false;// whether or not the long hold event happened already

int checkButton() {   
   int event = 0;
   buttonVal = digitalRead(buttonPin);
   // Button pressed down
   if (buttonVal == LOW && buttonLast == HIGH && (millis() - upTime) > debounce)
   {
       downTime = millis();
       ignoreUp = false;
       waitForUp = false;
       singleOK = true;
       holdEventPast = false;
       //longHoldEventPast = false;
       if ((millis()-upTime) < DCgap && DConUp == false && DCwaiting == true)  DConUp = true;
       else  DConUp = false;
       DCwaiting = false;
   }
   // Button released
   else if (buttonVal == HIGH && buttonLast == LOW && (millis() - downTime) > debounce)
   {       
       if (not ignoreUp)
       {
           upTime = millis();
           if (DConUp == false) DCwaiting = true;
           else
           {
               event = 2;
               DConUp = false;
               DCwaiting = false;
               singleOK = false;
           }
       }
   }
   // Test for normal click event: DCgap expired
   if ( buttonVal == HIGH && (millis()-upTime) >= DCgap && DCwaiting == true && DConUp == false && singleOK == true && event != 2)
   {
       event = 1;
       DCwaiting = false;
   }
   // Test for hold
   if (buttonVal == LOW && (millis() - downTime) >= holdTime) {
       // Trigger "normal" hold
       if (not holdEventPast)
       {
           event = 3;
           waitForUp = true;
           ignoreUp = true;
           DConUp = false;
           DCwaiting = false;
           //downTime = millis();
           holdEventPast = true;
       }
       
       // Trigger "long" hold
      // if ((millis() - downTime) >= longHoldTime)
     //  {
         //  if (not longHoldEventPast)
         //  {
             //  event = 4;
             //  longHoldEventPast = true;
          // }
       //}
   }
   buttonLast = buttonVal;
   return event;
}

Grumpy_Mike

Quote
I im still not sure how to make it to where it i double press the button it will change to a different color
On each detection of a double press you increment a variable. You make the variable wrap round, that is when it reaches the maximum you reset it back to zero.
Then in the function that sets the colour you just pass it that variable and use it to look up the red, green, and blue components of your colour n.
Each component is in a predefined red, green or blue array, containing the colour you want to produce at each value of your double click count.

Grumpy_Mike

Look when you were young if you didn't like the answer from mom did you ask dad?

Do not cross post here.
Discussions here https://forum.arduino.cc/index.php?topic=476779.msg3262461#msg3262461

Coding Badly


@casualgamer24, please do not cross-post.  Threads merged.


Go Up