Trying to implement various looping functions via remote

OK, so i’m slowly moving on with my project and I am trying to understand how I can adapt a code which currently uses a remote control to control x3 LEDs

The code in its original form switched on and off x2 LEDs with x4 buttons and another single button to make another LED “strobe” or flash.

I am trying to adapt it to allow 2 of the LEDs to set off on a flashing sequence by using x2 buttons and keep the third LED as an ON or OFF with x2 buttons, I did what I thought would be correct by basically adding another section of code in copying the strobe section and renaming the other strobeb etc but its doesn’t work and when compiling gives the errors:

remote_code_1:83: error: expected unqualified-id before ‘if’
remote_code_1:87: error: expected unqualified-id before ‘else’
remote_code_1:89: error: expected declaration before ‘}’ token

Can anyone help me through this code and what I am doing wrong?

#include <IRremote.h>

int irPin = 11; //ir sensor pin
int LEDa = 10; //First LED in row
const int strobeb = 9; //2nd LED in row - trying to flash this one too
const int strobe = 5; //LED for flashing
int all[] = {5, 9, 10}; //all lights
int strobeState = LOW;
int strobebState = LOW;
long prevMillis = 0;
long interval = 300; //time between flashes
bool StrobeIsFlashing=false;
bool StrobeIsFlashingb=false;

IRrecv irrecv(irPin);
decode_results results;

void setup(){
  Serial.begin(9600); //starts serial monitor *for debuging*
  irrecv.enableIRIn(); //starts IR reciever
  pinMode(LEDa, OUTPUT);
  pinMode(strobeb, OUTPUT);
  pinMode(strobe, OUTPUT);
}

void loop(){
  if (irrecv.decode(&results)){
    long int decCode = results.value;
    switch (results.value){
      case 546983:
      digitalWrite(LEDa, HIGH);
      break;
      
      case 571463:
      digitalWrite(LEDa, LOW);
      break;
      
      case 538823:
      StrobeIsFlashingb=!StrobeIsFlashingb;
      break;
      
      
      case 524543:
      StrobeIsFlashing=!StrobeIsFlashing;
     
      break;
      
      default:
      Serial.println("WAITING");
    }
    irrecv.resume();
  }
if(StrobeIsFlashing)
  {
  FlashStrobe();  
  }
  else
 digitalWrite(strobe, 0);
  }


void FlashStrobe()
{
unsigned long currentMillis = millis();
if(currentMillis - prevMillis > interval) 
  {
  prevMillis = currentMillis;
  if (strobeState == 0)
    strobeState = 1;
  else
    strobeState = 0;
  digitalWrite(strobe, strobeState);
 
  }
}






if(StrobeIsFlashingb)  //This part of the code seems not to work, it wont compile with errors 
  {
  FlashStrobeb();  
  }
  else
 digitalWrite(strobeb, 0);
  }


void FlashStrobeb()
{
unsigned long currentMillis = millis();
if(currentMillis - prevMillis > interval) 
  {
  prevMillis = currentMillis;
  if (strobebState == 0)
    strobebState = 1;
  else
    strobebState = 0;
  digitalWrite(strobeb, strobebState);
 
  }
}

OK so it would seem that there is nothing wrong with the last section of code referring to FlashStrobeb since removing the FlashStrobe section it compiles and the LED will flash, so i'm guessing I cannot have these 2 similar sections of codes together like that? What can I do to achieve the desired effect?

In your original post you had an if statement that was neither within the setup, loop or any other function, which is why you were getting errors on compile.

As you may have fixed that already, I suggest you undertake some debugging steps to help identify your problem. So put in lots of Serial.prinln("......") throughout your code to confirm it is behaving the way you expect.

(Obviously replace the dots above with more meaningful text and values.)

Debugging your own code is a normal step in the development process of any/all projects.

Thanks so much for the input, I have altered the code so that both “strobes” can flash and be turned on/off with a single button each.

Now I have the next task which I don’t know how to program, I have a static non strobing LED which I can turn on with one button and off with another button, I added some code so that if it is ON it will be turned off when either of the strobe buttons are pressed, this works fine.

I would also like the strobes to turn off if the static LED ON button is pressed i.e. in my code within case 546983.

I added code that I thought would have this effect, but it doesn’t. I am very new to programming and would appreciate any help/pointers as to what code I need to achieve this?

Thanks

#include <IRremote.h>

int irPin = 11; //ir sensor pin
int LEDa = 10; //First LED in row
const int strobeb = 9; //2nd LED in row - trying to flash this one too
const int strobe = 5; //LED for flashing
int all[] = {5, 9, 10}; //all lights
int strobeState = LOW;
int strobebState = LOW;
long prevMillis = 0;
long prevMillisa = 0;
long interval = 300; //time between flashes
bool StrobeIsFlashing=false;
bool StrobeIsFlashingb=false;

IRrecv irrecv(irPin);
decode_results results;

void setup(){
  Serial.begin(9600); //starts serial monitor *for debuging*
  irrecv.enableIRIn(); //starts IR reciever
  pinMode(LEDa, OUTPUT);
  pinMode(strobeb, OUTPUT);
  pinMode(strobe, OUTPUT);
}

void loop(){
  if (irrecv.decode(&results)){
    long int decCode = results.value;
    switch (results.value){
      
      case 546983: //yellow button - light up LED
      digitalWrite(strobe, 0); //this will not turn off the strobe
      digitalWrite(strobeb, 0); // this will not turn off the other strobe
      digitalWrite(LEDa, HIGH); 
     
      
      
      break;
      
      case 571463:
      digitalWrite(LEDa, LOW); // green button - turn off the LED
      break;
      
      case 538823: // start the strobe
      digitalWrite(LEDa, LOW);  // this turns off the static LED before starting the strobe
      StrobeIsFlashingb=!StrobeIsFlashingb;
      break;
      
      
      case 524543:  // start the 2nd strobe
      digitalWrite(LEDa, LOW);// this turns off the static LED before starting the strobe
      StrobeIsFlashing=!StrobeIsFlashing;
     
      break;
      
      default:
      Serial.println("WAITING");
    }
    irrecv.resume();
  }


if(StrobeIsFlashing)
  {
  FlashStrobe();  
  }
  else
 digitalWrite(strobe, 0);
  

if(StrobeIsFlashingb)  
  {
  FlashStrobeb();  
  }
  else
 digitalWrite(strobeb, 0);
  }



void FlashStrobe()
{
unsigned long currentMillisa = millis();
if(currentMillisa - prevMillisa > interval) 
  {
  prevMillisa = currentMillisa;
  if (strobeState == 0)
    strobeState = 1;
  else
    strobeState = 0;
  digitalWrite(strobe, strobeState);
 
  }
}









void FlashStrobeb()
{
unsigned long currentMillis = millis();
if(currentMillis - prevMillis > interval) 
  {
  prevMillis = currentMillis;
  if (strobebState == 0)
    strobebState = 1;
  else
    strobebState = 0;
  digitalWrite(strobeb, strobebState);
 
  }
}

I'm always too quick to ask before my experiments finish!

So I replaced:

digitalWrite(strobe, 0); //this will not turn off the strobe digitalWrite(strobeb, 0); // this will not turn off the other strobe

with:

StrobeIsFlashingb=!StrobeIsFlashingb; StrobeIsFlashing=!StrobeIsFlashing;

Obvious when you think about it, but coding is a challenge for me!

And that was wrong too!

so I replaced it with:

StrobeIsFlashingb=false; StrobeIsFlashing=false;

which actually stops the strobe flashing rather than just putting it in the opposite state which turns it on when it's off!