If I shouldn't use loop, what will I use? / rgb strip led with IR remote

Hi, this is what I try to do:

BUTTON1 >> Turns my rgb strip led to white
BUTTON0 >> Activates disco mode (turns red and then green, blue etc.)

I'm trying to achieve this, but disco mode requires loop because it changes the color all the time. However, if there is a loop, it means I cannot change the color back to white (pressing BUTTON1).

As a result of days of researchment, I learnt that I shouldn't make any loop, void loop() must do the trick but I couldn't get what should I do instead of loops.

#include <IRremote.hpp>

#define K1 0X87
#define K0 0X92
#define left 0X9B
#define right 0X99

int full_brightness = 255;
int zero_brightness = 0;
int half_brightness = 128;

int R = 6;
int G = 3;
int B = 5;
int brightness = 255;

int i = 1;
int y = 1;

void setup()
{
  Serial.begin(9600);
  IrReceiver.begin(2, ENABLE_LED_FEEDBACK); // Start the receiver

  pinMode(R, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(B, OUTPUT);
}

void loop() {

  if (i + y < 10) {

  }

  if (IrReceiver.decode()) {
    IrReceiver.printIRResultShort(&Serial);
    if (IrReceiver.decodedIRData.command == K1) {
      white();
    }
    if (IrReceiver.decodedIRData.command == K0) {
      discoMode();
    }
    IrReceiver.resume();
  }
}

void discoMode() {
  // R colour
  analogWrite(R, full_brightness);
  analogWrite (G, zero_brightness);
  analogWrite(B, zero_brightness);
  delay(500);
  //G colour
  analogWrite(R, zero_brightness);
  analogWrite (G, full_brightness);
  analogWrite(B, zero_brightness);
  delay(500);
  //B colour
  analogWrite(R, zero_brightness);
  analogWrite (G, zero_brightness);
  analogWrite(B, full_brightness);
}

///////////////////-/>trash<\-\\\\\\\\\\\\\\\\\\\

void white() {

  analogWrite(R, 255);
  analogWrite(G, 255);
  analogWrite(B, 255);
  Serial.println("WHITE");
}

In this example disco mode runs for just one time but I want it to be infinite and when I press another button it should break the loop, do what the code says.

But as I said, people tell there shouldn't be any loop but I cannot make it infinite with if statement or something like that.

I hope you got me. Here is my last hope.

Everything is done in loop. Setup runs once and only once (to set things up). Loop is where everything occurs. That is where your program goes. You can write functions outside of the main loop function but you still need to call them here to use them.

You need to start to think in a loop. Your program runs once really fast then goes back to the beginning and runs again over and over.

Look up state machines.

What is your first ‘if’ doing?

1 Like
void discoMode()
{
  static unsigned long timer = 0;
  static byte color = 0;

  if (millis() - timer > 500)
  {
    timer = millis();
    color = (color + 1) % 3;

    switch (color)
    {
      case 0:
        // R colour
        analogWrite(R, full_brightness);
        analogWrite(G, zero_brightness);
        analogWrite(B, zero_brightness);
        break;

      case 1:
        //G colour
        analogWrite(R, zero_brightness);
        analogWrite(G, full_brightness);
        analogWrite(B, zero_brightness);
        break;

      case 2:
        //B colour
        analogWrite(R, zero_brightness);
        analogWrite(G, zero_brightness);
        analogWrite(B, full_brightness);
        break;
    }
  }
}

void loop()
{
  static int mode = 0;

  if (IrReceiver.decode())
  {
    IrReceiver.printIRResultShort(&Serial);
    if (IrReceiver.decodedIRData.command == K1)
    {
      mode = 0;
    }
    if (IrReceiver.decodedIRData.command == K0)
    {
      mode = 1;
    }
    IrReceiver.resume();
  }

  switch (mode)
  {
    case 0:
      white();
      break;

    case 1:
      discoMode();
      break;
  }
}
1 Like

Variables can hold a state in the loop indefinitely. You can then use this state to decide what the program is currently doing.

With a button think like this;

The loop function is read many times each second, again and again.
You need to check for a button press or in your care an IR equivalent
When it detects this you currently ask it so do ‘something’ but if the button is not pressed the next time through the cycle it won’t do it again.
Instead you want to remember that the button was pressed and you can do this in a variable eg
Bool wasButtonPressed = false;
If button is pressed set it to true
Now set your function of disco mode to run if wasButtonPressed true
Now it will keep running forever

1 Like

Thank you, you are the best !!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.