button on/off complications

Hi,
I am having trouble setting this button up and could use some advice. I am definitely newer to this and am trying to figure out a way to use a singular button to turn on and off an led light strip. I the button doesn’t lock in place so i was trying to figure out how to keep reading the input for any changes while running the fade color sequence for the led lights. I have attached my current code for better understanding. would a while statement of some kind work best? or do… while. an example of a button being pressed while another function is running would be helpful. Thank you to any responders.

// color swirl! connect an RGB LED to the PWM pins as indicated
// in the #defines
// public domain, enjoy!
int Pot_value = 1;
#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3

#define FADESPEED Pot_value // make this higher to slow down
int Frequency_output = 0;
int buttonState = 0; // variable for reading the pushbutton status
const int buttonPin = A3; // the number of the pushbutton pin

int bttnPushCounter = 0; // counter for the number of button presses
int bttnState = 0; // current state of the button
int lastBttnState = 0; // previous state of the button

void setup() {
pinMode(buttonPin, INPUT);
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (bttnPushCounter >= 1) {
int bttnPushCounter = 0;
int r = 0;
int g = 0;
int b = 0;
}
if (buttonState == HIGH && (bttnPushCounter == 0 || bttnPushCounter >= 2)) {
int r = 0;
int g = 0;
int b = 0;

}
else {

if (bttnState == LOW || bttnPushCounter == 1) {
bttnPushCounter = (bttnPushCounter + 1);
int r, g, b;
Pot_value = analogRead(A0);
Pot_value = map(Pot_value, 0, 1023, 0, 20);
// fade from blue to violet
#define FADESPEED Pot_value
for (r = 0; r < 256; r++) {
analogWrite(REDPIN, r);
delay(FADESPEED);
}
#define FADESPEED Pot_value
// fade from violet to red
for (b = 255; b > 0; b–) {
analogWrite(BLUEPIN, b);
delay(FADESPEED);
}
#define FADESPEED Pot_value
// fade from red to yellow
for (g = 0; g < 256; g++) {
analogWrite(GREENPIN, g);
delay(FADESPEED);
}
#define FADESPEED Pot_value
// fade from yellow to green
for (r = 255; r > 0; r–) {
analogWrite(REDPIN, r);
delay(FADESPEED);
}
#define FADESPEED Pot_value
// fade from green to teal
for (b = 0; b < 256; b++) {
analogWrite(BLUEPIN, b);
delay(FADESPEED);
}
#define FADESPEED Pot_value
// fade from teal to blue
for (g = 255; g > 0; g–) {
analogWrite(GREENPIN, g);
delay(FADESPEED);
}
}
}

}

terrific.txt (2.22 KB)

#define FADESPEED Pot_value

What is that line doing all over your code? A #define sets up a text based find-and-replace. You usually put them at the top of the code if you want them, not sprinkled throughout and you definitely don't need the same one over and over.

Your problem is using the for loop to handle the fade. Adding more blocking, while or do-while, would be the opposite of the solution. You need non-blocking code. You need to rewrite this so that instead of getting stuck in a for loop fading, it lets the loop function keep repeating and on each run through adds one to the fade and writes it.

Look at the top of the Programming Board under the "Useful Links" thread and look at all the stuff under "General Design". Most of it is about how to write non-blocking code.

Also take a moment to look at the one "How to use this forum" and learn about posting your code correctly. Thumbing your nose at the forum rules isn't going to win you any friends here.

Apologies. Thank you, I will review the how to use this forum and double back on some research before posting again.

What you’re looking for is the state/change example

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How have you got your button wired?

Thanks.. Tom... :slight_smile:

Thanks guys, I will post my updated code below. I’ve changed the code quite a bit after researching non-blocking code. mostly code I have pieced together from other code I have found online.
I’ve got it almost where I want it to be. I think I am having voltage problems and I am trying to figure out how to use a logic mosfet to control the rgb led stips without overloading the pin. included is an image of almost how I have it set up. its a 12v rgb light strip and this button. I am able to turn off the lights with the interrupt kind of, it runs through the loop and ends with no color through the lights, not perfect but it works.

I am not as familiar with wiring and varying voltage, if there is another idea besides mosfet please give a suggestion and I will look it up.

Button

int redPin = 5;

int greenPin = 6;

int bluePin = 3;

int ButtonState = 0; 

int buttonPin = 2;

int ButtonPushCounter = 0;

int Pot_value = 0;





int r = 0;

int g = 0;

int b = 0;



void setup() {

  pinMode(redPin, OUTPUT);

  pinMode(greenPin, OUTPUT);

  pinMode(bluePin, OUTPUT);
  
  pinMode(buttonPin, INPUT);
  
  digitalWrite(buttonPin, HIGH);
  
  attachInterrupt(digitalPinToInterrupt(2), press , CHANGE);
  

}



void loop() { 
  
  ButtonState = digitalRead(buttonPin);
  if (ButtonState == LOW || ButtonPushCounter == 1){
  ButtonPushCounter = 1;
  Pot_value = analogRead(A0);
  Pot_value = map(Pot_value, 0, 1023, 0, 50);
  
  setColor(255, 0, 0);    // red

  setColor(0, 255, 0);    // green

  setColor(0, 0, 255);    // blue

  setColor(255, 255, 0);  // yellow

  setColor(80, 0, 80);    // purple

  setColor(0, 255, 255);  // aqua
    
  setColor(0, 0, 0);
  }
}



void setColor(int red, int green, int blue) {

  while ( r != red || g != green || b != blue ) {

    if ( r < red ) r += 1;

    if ( r > red ) r -= 1;
    
    Pot_value = analogRead(A0);
  Pot_value = map(Pot_value, 0, 1023, 0, 50);



    if ( g < green ) g += 1;

    if ( g > green ) g -= 1;
    
    Pot_value = analogRead(A0);
  Pot_value = map(Pot_value, 0, 1023, 0, 50);



    if ( b < blue ) b += 1;

    if ( b > blue ) b -= 1;
    
    Pot_value = analogRead(A0);
  Pot_value = map(Pot_value, 0, 1023, 0, 20);



    _setColor();

    delay(Pot_value);

  }

}

void press()
{
  if (ButtonPushCounter == 1 && ButtonState == HIGH) 
  {
    ButtonPushCounter = 0;
        digitalWrite(buttonPin, HIGH);
        setColor(0, 0, 0);

  }
}



void _setColor() {

  analogWrite(redPin, r);

  analogWrite(greenPin, g);

  analogWrite(bluePin, b); 

}

You really don’t need to use an interrupt in this case.

OK, a few things.

Do not double space your code. It makes it very difficult to read. Use only single blanks between complete functional blocks.

Please go back and modify your original post (not re-post it) - using the "More -> Modify" option below the right hand corner of your post - to mark up your code as such.

Do not use interrupts when you do not comprehend what they are!


As a beginner, it is incredibly unlikely that interrupts will be useful to you.

A common "newbie" misunderstanding is that an interrupt is a mechanism for altering the flow of a program - to execute an alternate function. Nothing could be further from the truth! :astonished:

An interrupt is a mechanism for performing an action which can be executed in "no time at all" with an urgency that it must be performed immediately or else data - information - will be lost or some harm will occur. It then returns to the main task without disturbing that task in any way though the main task may well check at the appropriate point for a "flag" set by the interrupt.

Now these criteria are in a microprocessor time scale - microseconds. This must not be confused with a human time scale of tens or hundreds of milliseconds or indeed, a couple of seconds. A switch operation is in this latter category and a mechanical operation perhaps several milliseconds; the period of a 6000 RPM shaft rotation is ten milliseconds.

Unless it is a very complex procedure, you would expect the loop() to cycle many times per millisecond. If it does not, there is most likely an error in code planning; while the delay() function is provided for testing purposes, its action goes strictly against effective programming methods. The loop() will be successively testing a number of contingencies as to whether each requires action, only one of which may be whether a particular timing criteria has expired. Unless an action must be executed in the order of microseconds, it will be handled in the loop().

So what sort of actions do require such immediate attention? Well, generally those which result from the computer hardware itself, such as high speed transfer of data in UARTs(, USARTs) or disk controllers.

An alternate use of interrupts, for context switching in RTOSs, is rarely relevant to this category of microprocessors as it is more efficient to write cooperative code as described above.

well said!
This should go in the tutorial section for interrupts
thx

It should indeed, but the tutorial section seems instead to spruik interrupts as "something else exciting to try out"! :cold_sweat:

start another thread.... What are interrupts for...
a bit more specific than ‘how to use...’

But unless it counts as a sticky, it dies (suffocates)!

The Introductory Tutorials have stayed pretty clean.
Irrelevant threads get tossed fairly quickly, maybe when I’m back home, I’ll sign up to clean out or consolidate dubious posts.

Hi,
OPs "circuit"?


Can you please post a copy of your circuit as apicture of a hand drawn circuit in jpg, png?

How do you think the press button and LED are wired?

Thanks.. Tom.... :slight_smile:

Good question...!