Need a hand loop within case by IR remote

Hi,

I'm building a laser pointer training device and I would like to run different training sets by pressing a IR remote button.

Got a 1-9 *# arrows and OK remote which I can let print it's name when pressed and a built-in led turning on and off.

That's basic remote and I understand most of it.

I wrote 3 other sketches that run on the uno like I would want to though now it's time to make 4 sketches into 1

I would like this.

Button 1 pressed, Loop my right to left training till pressed another button
Button 2 pressed, Loop my left to right training till pressed another button
..
Button 9 pressed, Loop my random training till pressed another button

current sketches 1 and 9
I made sure the header and setup are the same.

Sketch I want under button 1

// Setup Laser trainer mode 1 Right to Left
// Future remote function 1

// Start functions header                     // All new addons must be added in every header for future remote control
int laserPin = 13;                            // Laser pin 13 to be able to check builtin led when without laser attached
const int buzzer = 11;                        // Buzzer pin 11
#include <Servo.h>                            // Include servo library
#include <IRremote.h>                         // Include IR library

Servo PanServo;                               // Name for servo moving horizontal
Servo TiltServo;                              // Name for servo moving vertical

// int positions
int pos = 0;
int posPanL = 110; // Pos max Left
int posPanR = 150; // Pos max Right
int posTiltO = 60; // Pos max Bottom
int posTiltB = 100; // Pos max Top
int posLROffset = 11; // Pos Offset for L>R and R>L
int posLRRL = posTiltB - posLROffset; // Pos for L>R and R>L

// int buzzer
int buzztone = 100;
int buzzdur = 10;


// IR setup
int reciever = 2;
uint32_t Previous;
IRrecv irrecv(reciever);
decode_results results;
// #define buttons
#define button_1  0xFFA25D // Is button 1
#define button_2  0xFF629D // Is button 2
#define button_3  0xFFE21D // Is button 3
#define button_4  0xFF22DD // Is button 4
#define button_5  0xFF02FD // Is button 5
#define button_6  0xFFC23D // Is button 6
#define button_7  0xFFE01F // Is button 7
#define button_8  0xFFA857 // Is button 8
#define button_9  0xFF906F // Is button 9
#define button_0  0xFF9867 // Is button 0
#define button_X  0xFF6897 // Is button *
#define button_HASH  0xFFB04F // Is button #
#define button_UP  0xFF18E7 // Is button Omhoog
#define button_DOWN 0xFF4AB5 // Is button Omlaag
#define button_LEFT  0xFF10EF // Is button Links
#define button_RIGHT  0xFF5AA5 // Is button Rechts
#define button_OK  0xFF38C7 // Is button OK

void setup() {
  pinMode(laserPin, OUTPUT);                   // Set laser as output
  pinMode(buzzer, OUTPUT);                     // Set buzzer as output
  PanServo.attach(9);                          // Pan servo pin 9
  TiltServo.attach(10);                        // Tilt servo pin10

  // Ready to start
  PanServo.write(posPanR);                         // Pan start
  TiltServo.write(posTiltB);                       // Tilt start
  digitalWrite(laserPin, LOW);                // Laser OFF
  delay(15);                                  // Wait for servo to move to start pos

  //test IR
  Serial.begin(9600);
  irrecv.enableIRIn();
  // end test IR
}
// End of functions header

void loop()
// START Laser trainer function 1 Right to Left
{
  digitalWrite(laserPin, LOW);                // Turns laser OFF
  TiltServo.write(posLRRL);                        // Set tilt to 10
  delay(15);

  for (pos = posPanL; pos <= posPanR; pos += 10) {    // goes from posPanL to posPanR in steps of 5 degrees
    PanServo.write(pos);                      // tell servo to go to position in variable 'pos'
    delay(15);                                // waits 15ms for the servo to reach the position
  }

  delay(800);                             // waits 800ms

  //// Buzzer of during testing in livingroom
  //  tone(buzzer, buzztone);                          // Send buzztone Hz sound signal...
  //  delay(buzzdur);                                 // ...for buzzdur sec
  //  noTone(buzzer);                             // Stop sound...

  delay(100);                           // waits 100ms for the servo to reach the position
  digitalWrite(laserPin, HIGH);             // Turn laser ON
  for (pos = posPanR; pos >= posPanL; pos -= 1 ) {      // goes from posPanR to posPanL
    PanServo.write(pos);                      // tell servo to go to position in variable 'pos'
    delay(55);                                // waits 55ms for the servo to reach the position

  }

}
// END Laser trainer function 1 Right to Left

Sketch i want under button 9

// Setup Laser trainer mode 9 Random aiming
// Future remote function 9

// Start functions header                     // All new addons must be added in every header for future remote control
int laserPin = 13;                            // Laser pin 13 to be able to check builtin led when without laser attached
const int buzzer = 11;                        // Buzzer pin 11
#include <Servo.h>                            // Include servo library
#include <IRremote.h>                         // Include IR library

Servo PanServo;                               // Name for servo moving horizontal
Servo TiltServo;                              // Name for servo moving vertical

// int positions
int pos = 0;
int posPanL = 110; // Pos max Left
int posPanR = 150; // Pos max Right
int posTiltO = 60; // Pos max Bottom
int posTiltB = 100; // Pos max Top
int posLROffset = 11; // Pos Offset for L>R and R>L
int posLRRL = posTiltB - posLROffset; // Pos for L>R and R>L

// int buzzer
int buzztone = 100;
int buzzdur = 10;


// IR setup
int reciever = 2;
uint32_t Previous;
IRrecv irrecv(reciever);
decode_results results;
// #define buttons
#define button_1  0xFFA25D // Is button 1
#define button_2  0xFF629D // Is button 2
#define button_3  0xFFE21D // Is button 3
#define button_4  0xFF22DD // Is button 4
#define button_5  0xFF02FD // Is button 5
#define button_6  0xFFC23D // Is button 6
#define button_7  0xFFE01F // Is button 7
#define button_8  0xFFA857 // Is button 8
#define button_9  0xFF906F // Is button 9
#define button_0  0xFF9867 // Is button 0
#define button_X  0xFF6897 // Is button *
#define button_HASH  0xFFB04F // Is button #
#define button_UP  0xFF18E7 // Is button Omhoog
#define button_DOWN 0xFF4AB5 // Is button Omlaag
#define button_LEFT  0xFF10EF // Is button Links
#define button_RIGHT  0xFF5AA5 // Is button Rechts
#define button_OK  0xFF38C7 // Is button OK

void setup() {
  pinMode(laserPin, OUTPUT);                   // Set laser as output
  pinMode(buzzer, OUTPUT);                     // Set buzzer as output
  PanServo.attach(9);                          // Pan servo pin 9
  TiltServo.attach(10);                        // Tilt servo pin10

  // Ready to start
  PanServo.write(posPanR);                         // Pan start
  TiltServo.write(posTiltB);                       // Tilt start
  digitalWrite(laserPin, LOW);                // Laser OFF
  delay(15);                                  // Wait for servo to move to start pos

  //test IR
  Serial.begin(9600);
  irrecv.enableIRIn();
  // end test IR
}
// End of functions header

void loop()
// START Laser trainer function 9 Random
{

  pos = random(posPanL, posPanR);             // Random pos between pos pan L and R
  PanServo.write(pos);            // tell servo to go to position in variable 'pos'
  delay(25);                      // waits 15ms for the servo to reach the position
  pos = random(posTiltO, posTiltB);              // Random pos posTiltO and B
  TiltServo.write(pos);           // tell servo to go to position in variable 'pos'
  delay(25);                      // waits 15ms for the servo to reach the position
  delay(1500);                      // waits 1.5s

  tone(buzzer, buzztone);                // Send 200Hz sound signal...
  delay(buzzdur);                       // ...for 0.1 sec
  noTone(buzzer);                   // Stop sound...

  digitalWrite(laserPin, HIGH);     // Laser ON

  delay(2500);                      // waits 2.5s for the servo to reach the position

  digitalWrite(laserPin, LOW);      // Laser OFF


}

// END Laser trainer function 9 Random

Hope you understand what I would like to achieve here because I find it hard to translate my thoughts to English and only a beginner with arduino.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

There is a Dutch section of the forum if that would be an easier place for you to get help

Thanks for both messages, I edited my first post and will wait for someone to read it. If needed I will start over in Dutch :+1:

Start with something that reads the remote and use a switch statement based on what you got to serial.print the button name.

Once that's working, take the loop function from sketch one and paste it into your remote program. Rename it and then let the compiler help you find all the missing declarations.

Thanks,

The remote and print works fine, Even the built-in LED does what I wrote there

void loop()
// START Laser trainer function #NUMBER #NAME

//test IR
{
  if (irrecv.decode(&results)) {
    if (results.value == 0xFFFFFFFF) {
      results.value = Previous;
    }
    switch (results.value) {
      case button_UP : Serial.println ("button_UP");

        digitalWrite(laserPin, HIGH);   // turn the LED on (HIGH is the voltage level)
        delay(2000);                       // wait for a second
        digitalWrite(laserPin, LOW);    // turn the LED off by making the voltage LOW
        delay(1000);                       // wait for a second

        break;
      case button_OK : Serial.println ("button_OK");

        digitalWrite(laserPin, HIGH);   // turn the LED on (HIGH is the voltage level)
        delay(1000);                       // wait for a second
        digitalWrite(laserPin, LOW);    // turn the LED off by making the voltage LOW
        delay(2000);                       // wait for a second


        break;
    }
    Serial.println (results.value, HEX);


    irrecv.resume();

  }
}
//end test IR

// END Laser trainer function #NUMBER #NAME

I can paste my program inside the case but then it will not loop because the LED and print also does not loop

Get it looping first. I expect the remote returns something that says no button pressed. Use another variable to drive the switch and only set it when an actual button was pressed.

I suggest that you put the code for each training set in its own function. Otherwise your main function will be huge and hard to follow.

It does not have a no button pressed as far as I can tell

The function decode(&results)) is deprecated and may not work as expected! Just use decode() without a parameter and IrReceiver.decodedIRData.<fieldname> .
button_1
FFA25D
FF629D
0
button_9
FF906F
button_1
FFA25D
0
button_9
FF906F
button_9
FF906F
button_9
FF906F
button_9
FF906F

The code posted earlier shows this on serial.

In that case, you could move the switch out of the if, so it runs whether a new button is pressed or not.

Thanks, that was the thing I needed.

Now it works, Well one weird thing. When I connect the other Uno through USB the IR code keeps changing so it's not recognized but when powering through jack it works the same as my Uno without servo, laser and buzzer connected.

And with 5V 3A adapter the laser is less visible than through USB which wouldn't make sense because then it has less power (0.5A from laptop I think)

// Setup Laser trainer mode #NUMBER #NAME
// Future remote function #NUMBER

// Start functions header                     // All new addons must be added in every header for future remote control
int laserPin = 13;                            // Laser pin 13 to be able to check builtin led when without laser attached
const int buzzer = 11;                        // Buzzer pin 11
#include <Servo.h>                            // Include servo library
#include <IRremote.h>                         // Include IR library

Servo PanServo;                               // Name for servo moving horizontal
Servo TiltServo;                              // Name for servo moving vertical

// int positions
int pos = 0;
int posPanL = 110; // Pos max Left
int posPanR = 150; // Pos max Right
int posTiltO = 60; // Pos max Bottom
int posTiltB = 100; // Pos max Top
int posLROffset = 11; // Pos Offset for L>R and R>L
int posLRRL = posTiltB - posLROffset; // Pos for L>R and R>L

// int buzzer
int buzztone = 100;
int buzzdur = 10;


// IR setup
int reciever = 2;
uint32_t Previous;
IRrecv irrecv(reciever);
decode_results results;
// #define buttons
#define button_1  0xFFA25D // Is button 1
#define button_2  0xFF629D // Is button 2
#define button_3  0xFFE21D // Is button 3
#define button_4  0xFF22DD // Is button 4
#define button_5  0xFF02FD // Is button 5
#define button_6  0xFFC23D // Is button 6
#define button_7  0xFFE01F // Is button 7
#define button_8  0xFFA857 // Is button 8
#define button_9  0xFF906F // Is button 9
#define button_0  0xFF9867 // Is button 0
#define button_X  0xFF6897 // Is button *
#define button_HASH  0xFFB04F // Is button #
#define button_UP  0xFF18E7 // Is button Omhoog
#define button_DOWN 0xFF4AB5 // Is button Omlaag
#define button_LEFT  0xFF10EF // Is button Links
#define button_RIGHT  0xFF5AA5 // Is button Rechts
#define button_OK  0xFF38C7 // Is button OK

void setup() {
  pinMode(laserPin, OUTPUT);                   // Set laser as output
  pinMode(buzzer, OUTPUT);                     // Set buzzer as output
  PanServo.attach(9);                          // Pan servo pin 9
  TiltServo.attach(10);                        // Tilt servo pin10

  // Ready to start
  PanServo.write(posPanR);                         // Pan start
  TiltServo.write(posTiltB);                       // Tilt start
  digitalWrite(laserPin, LOW);                // Laser OFF
  delay(15);                                  // Wait for servo to move to start pos
  pinMode(LED_BUILTIN, OUTPUT);
  //test IR
  Serial.begin(9600);
  irrecv.enableIRIn();
  // end test IR
}
// End of functions header

void loop()
// START Laser trainer function #NUMBER #NAME

//test IR
{
  if (irrecv.decode(&results)) {
    if (results.value == 0xFFFFFFFF) {
      results.value = Previous;
    }
    switch (results.value) {




        break;
      case button_9 : Serial.println ("button_9");

        digitalWrite(laserPin, HIGH);   // turn the LED on (HIGH is the voltage level)
        delay(1000);                       // wait for a second
        digitalWrite(laserPin, LOW);    // turn the LED off by making the voltage LOW
        delay(2000);                       // wait for a second


        break;
    }
    Serial.println (results.value, HEX);
    irrecv.resume();

  }
  switch (results.value) {
    case button_1: Serial.println ("button_1");
      // START Laser trainer function 1 Right to Left
      {
        digitalWrite(laserPin, LOW);                // Turns laser OFF
        TiltServo.write(posLRRL);                        // Set tilt to 10
        delay(15);

        for (pos = posPanL; pos <= posPanR; pos += 10) {    // goes from posPanL to posPanR in steps of 5 degrees
          PanServo.write(pos);                      // tell servo to go to position in variable 'pos'
          delay(15);                                // waits 15ms for the servo to reach the position
        }

        delay(800);                             // waits 800ms

        //// Buzzer of during testing in livingroom
        //  tone(buzzer, buzztone);                          // Send buzztone Hz sound signal...
        //  delay(buzzdur);                                 // ...for buzzdur sec
        //  noTone(buzzer);                             // Stop sound...

        delay(100);                           // waits 100ms for the servo to reach the position
        digitalWrite(laserPin, HIGH);             // Turn laser ON
        for (pos = posPanR; pos >= posPanL; pos -= 1 ) {      // goes from posPanR to posPanL
          PanServo.write(pos);                      // tell servo to go to position in variable 'pos'
          delay(55);                                // waits 55ms for the servo to reach the position

        }

      }
      // END Laser trainer function 1 Right to Left
      break;
    case button_9: Serial.println ("button_9");
      // START Laser trainer function 9 Random
      {

        pos = random(posPanL, posPanR);             // Random pos between pos pan L and R
        PanServo.write(pos);            // tell servo to go to position in variable 'pos'
        delay(25);                      // waits 15ms for the servo to reach the position
        pos = random(posTiltO, posTiltB);              // Random pos posTiltO and B
        TiltServo.write(pos);           // tell servo to go to position in variable 'pos'
        delay(25);                      // waits 15ms for the servo to reach the position
        delay(1500);                      // waits 1.5s

        tone(buzzer, buzztone);                // Send 200Hz sound signal...
        delay(buzzdur);                       // ...for 0.1 sec
        noTone(buzzer);                   // Stop sound...

        digitalWrite(laserPin, HIGH);     // Laser ON

        delay(2500);                      // waits 2.5s for the servo to reach the position

        digitalWrite(laserPin, LOW);      // Laser OFF


      }

      // END Laser trainer function 9 Random
      break;
    default:
      {
        digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
        delay(500);                       // wait for a second
        digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
        delay(500);                       // wait for a second
      }
      break;
  }
}

//end test IR

// END Laser trainer function #NUMBER #NAME

I do need some more help,

Code above, I can run 1 and 9 back and forth but when I press a unknown button it gets trapped in default and can't get out of it with 1 or 9

How do I get out of default case?

You have a second's worth of delay in default. Better to use millis for timing so you can get back to reading the remote more frequently.

Thanks, I'll look into that though that's not why it gets stuck in default I think because switching from one to nine and back works flawless (I know it does take some seconds)

It's just the default for now.

Just comment out the delays in default and test it.

I did what you suggested and it stays the same.
I can go back and forth between 1 and 9 as much as I want but when using a unassigned button it goes to default and you can't get out of it until reboot.

Then I pasted blink without delay in the default section and it's also the same result.

Maybe it's not the case default casing the problem.
I'm still new to coding but I opened the serial monitor to see what the IR sensor does.

15:48:12.959 -> The function decode(&results)) is deprecated and may not work as expected! Just use decode() without a parameter and IrReceiver.decodedIRData.<fieldname> .
15:48:13.100 -> FFA25D
15:48:13.100 -> button_1
15:48:16.300 -> button_1
15:48:19.564 -> button_1
15:48:22.819 -> button_1
15:48:26.081 -> FF629D
15:48:36.491 -> FFA25D
15:48:36.491 -> button_1
15:48:39.695 -> 0
15:48:45.299 -> FFA25D
15:48:45.299 -> button_1
15:48:48.555 -> 0
15:48:52.156 -> FF906F
15:48:52.156 -> button_9
15:48:56.252 -> 0
15:49:02.509 -> E0627FE9
15:49:04.065 -> 9DAB2913
15:49:05.683 -> 4805B99D
15:49:06.895 -> D1A336C4
15:49:07.831 -> 922E0A38
15:49:08.675 -> EDD5EB55
15:49:10.027 -> F182FCD0

After a unknown it stops decoding and won't give FF#### but it reads other values. So probably that's letting me stay in default because there is no matching case.

Could it be that deprecated function you're getting a warning about?

Does it always work for a bit and then go wrong?

Yes it always works fine till I use a unknow button. I could assign all buttons to a case but then I still could catch a failed signal and get stuck again.

I'm going through some example sketches for IR and just can't figure out how to edit one to use for my remote. This one came from the seller.

I'm trying to get the irremote version to 3.x instead of the deprecated 2.x now but there's another small problem I can't figure out.

Before there was "results.value" which is used in the switch case and now results is gone so I don't have the knowledge to use any received HEX to switch.

This example sketch works for me (I did define the pins, just shut down the laptop so I can't paste the complete code therefore use the example from github)

#include <IRremote.hpp>

void setup()
{

  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver
}

void loop() {
  if (IrReceiver.decode()) {
      Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);
      IrReceiver.printIRResultShort(&Serial); // optional use new print version
      
      IrReceiver.resume(); // Enable receiving of the next value
  }
  
}

Can you add a switch at the very end of loop to tell you what button is current?