I want use a remote control

but my code doesnt accept a new button Press and he go trough the code
i cant help me with google and Chatgpt.
iam a littlebit lost.

#include <IRremote.h>
#include <IRremoteInt.h>

#include "IRremote.h"
#include "IR.h"

IRrecv irrecv(RECEIVER);
decode_results results;
const int LED_PIN_1 = 6;  // Beispiel-Pin für die Rote LED
const int LED_PIN_2 = 5;  // Beispiel-Pin für die zweite LED
const int LED_PIN_3 = 4;  // Beispiel-Pin für die zweite LED
bool foundKey12 = false;
bool foundKey13 = false;
bool foundKey14 = false;
bool foundKey15 = false;
void setup() {

  bool foundKey12 = false;
  pinMode(LED_PIN_1, OUTPUT);
  pinMode(LED_PIN_2, OUTPUT);
  pinMode(LED_PIN_3, OUTPUT);
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");
  irrecv.enableIRIn();
}

void loop() {
  int tmpValue;
  // Überprüfe, ob ein Infrarotsignal empfangen wurde
  if (irrecv.decode(&results)) {
    // Überprüfe alle Key Values im Array
    foundKey12 = false;
    foundKey13 = false;
    foundKey14 = false;
    for (int i = 0; i < KEY_NUM; i++) {
      // Wenn der KeyValue 12 ist, steuere den Port 6 an (Licht einschalten)
      if (keyValue[i] = 12 && !foundKey12) {
        digitalWrite(6, HIGH);
        foundKey12 = true;
        //foundKey13 = false;
        //foundKey14 = false;
        Serial.println("Debuggggggg");
      }

      else if (keyValue[i] = 13 && !foundKey13) {
        // Wenn der KeyValue 13 ist, steuere den Port 6 an (Licht ein ausschalten)
        digitalWrite(6, HIGH);
        delay(2000);
        digitalWrite(6, LOW);
        delay(1000);
        foundKey13 = true;
        Serial.println("Debuggggggg zwoooo");

      }

      else if (keyValue[i] = 14 && !foundKey14) {
        // Wenn der KeyValue 14 ist, steuere den Port 6 an (Licht ein ausschalten)
        digitalWrite(4, HIGH);
        digitalWrite(5, HIGH);
        foundKey14 = true;

        Serial.println("Debuggggggg drüüü");

      }

      else if (keyValue[i] = 15 && !foundKey15) {
        // Wenn der KeyValue 14 ist, steuere den Port 6 an (Licht ein ausschalten)
        digitalWrite(4, HIGH);
        digitalWrite(5, LOW);
        delay(1000);
        digitalWrite(4, LOW);
        digitalWrite(5, HIGH);
        delay(1000);
        digitalWrite(4, LOW);
        digitalWrite(5, LOW);
        delay(500);
        foundKey15 = true;
        Serial.println("Debuggggggg foooor");
      }
      // Überprüfe, ob der aktuelle KeyValue mit dem empfangenen Wert übereinstimmt und der Index innerhalb des gültigen Bereichs liegt
      else if (keyValue[i] == results.value && i < KEY_NUM) {
        // Speichere den empfangenen Wert in tmpValue
        tmpValue = results.value;
      }
      // Wenn REPEAT erreicht ist, setze den empfangenen Wert erneut
      else if (REPEAT == i) {
        results.value = tmpValue;
      }
    }
    // Warte auf einen neuen Tastendruck
    while (irrecv.decode(&results)) {
    }
    // Empfangenes Signal verarbeiten, um sicherzustellen, dass keine Wiederholungen auftreten
    irrecv.resume();
  }
}

HI @geist64 ,

in your sketch there is an include "IR.h" ...

Does this hold the declaration for the array keyValue[] and also a define for KEY_NUM?

If yes could you please post it also so that it's possible to compile and test your sketch?

In general it looks as if you are using the functions of an outdated IR remote library ...

ec2021

what's all this?

the IRremote library went through an extensive rewrite recently, for a new project you should probably adopt the new way things work. They are providing examples.

HI @J-M-L ,

nice to meet you again :slight_smile:

In addition to the outdated library declarations and defines are missing:

  • RECEIVER
  • KEY_NUM
  • REPEAT
  • keyValues[]

If they are not in "IR.h" I doubt that this sketch did compile without error messages ...

The task (as far as I assume to understand it) calls for arrays for leds and foundKeyXX as well as for a switch case to handle the keys and also a "millis()" function for the led signals ...

Are lines like this on purpose?

image

@geist64 ,
Maybe you could explain us the intended purpose of the sketch? That may help us ...

same here :slight_smile:


@geist64

here is a minimal example with the new .hpp API that you can test with wokwi

click to see the code
/* ============================================
  code is placed under the MIT license
  Copyright (c) 2024 J-M-L
  For the Arduino Forum : https://forum.arduino.cc/u/j-m-l

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
  ===============================================
*/



#include <IRremote.hpp>

/*
   Pin mapping table for different platforms

   Platform     IR input    IR output   Tone      Core/Pin schema
   --------------------------------------------------------------
   DEFAULT/AVR  2           3           4         Arduino
   ATtinyX5     0|PB0       4|PB4       3|PB3     ATTinyCore
   ATtiny167    3|PA3       2|PA2       7|PA7     ATTinyCore
   ATtiny167    9|PA3       8|PA2       5|PA7     Digispark original core
   ATtiny84      |PB2        |PA4        |PA3     ATTinyCore
   ATtiny88     3|PD3       4|PD4       9|PB1     ATTinyCore
   ATtiny3217  18|PA1      19|PA2      20|PA3     MegaTinyCore
   ATtiny1604   2           3|PA5       %
   ATtiny816   14|PA1      16|PA3       1|PA5     MegaTinyCore
   ATtiny1614   8|PA1      10|PA3       1|PA5     MegaTinyCore
   SAMD21       3           4           5
   ESP8266      14|D5       12|D6       %
   ESP32        15          4           27
   ESP32-C3     6           7
   BluePill     PA6         PA7         PA3
   APOLLO3      11          12          5
   RP2040       3|GPIO15    4|GPIO16    5|GPIO17
*/

#define IR_RECEIVE_PIN      2

void setup() {
  Serial.begin(115200); Serial.println();
  IrReceiver.begin(IR_RECEIVE_PIN);
}

void loop() {
  if (IrReceiver.decode()) {
    // got a new signal
    // IrReceiver.decodedIRData is a structure you can access to read what got received
    // you can see the structure at https://github.com/Arduino-IRremote/Arduino-IRremote/blob/c6c7f17f587eabab0833fe0d048dba85251a9735/src/IRProtocol.h#L107-L130
    // it looks something like this (https://github.com/Arduino-IRremote/Arduino-IRremote?tab=readme-ov-file#receiving-ir-codes)
    /*
       struct IRData {
      decode_type_t protocol;     // UNKNOWN, NEC, SONY, RC5, PULSE_DISTANCE, ...
      uint16_t address;           // Decoded address
      uint16_t command;           // Decoded command
      uint16_t extra;             // Used for Kaseikyo unknown vendor ID. Ticks used for decoding Distance protocol.
      uint16_t numberOfBits;      // Number of bits received for data (address + command + parity) - to determine protocol length if different length are possible.
      uint8_t flags;              // IRDATA_FLAGS_IS_REPEAT, IRDATA_FLAGS_WAS_OVERFLOW etc. See IRDATA_FLAGS_* definitions
      IRRawDataType decodedRawData;    // Up to 32 (64 bit for 32 bit CPU architectures) bit decoded raw data, used for sendRaw functions.
      uint32_t decodedRawDataArray[RAW_DATA_ARRAY_SIZE]; // 32 bit decoded raw data, to be used for send function.
      irparams_struct *rawDataPtr; // Pointer of the raw timing data to be decoded. Mainly the data buffer filled by receiving ISR.
      };
    */
    // the most useful attribute of the structure is the Decoded command, it's an uint16_t you access with IrReceiver.decodedIRData.command

    // for the wokwi remote control, commands looks like this
    switch (IrReceiver.decodedIRData.command) {
      case 162: Serial.println("POWER"); break;
      case 226: Serial.println("MENU");  break;
      case 34:  Serial.println("TEST"); break;
      case 2:   Serial.println("PLUS"); break;
      case 194: Serial.println("BACK"); break;
      case 224: Serial.println("PREV."); break;
      case 168: Serial.println("PLAY"); break;
      case 144: Serial.println("NEXT"); break;
      case 104: Serial.println("num: 0"); break;
      case 152: Serial.println("MINUS"); break;
      case 176: Serial.println("key: C"); break;
      case 48:  Serial.println("num: 1"); break;
      case 24:  Serial.println("num: 2"); break;
      case 122: Serial.println("num: 3"); break;
      case 16:  Serial.println("num: 4"); break;
      case 56:  Serial.println("num: 5"); break;
      case 90:  Serial.println("num: 6"); break;
      case 66:  Serial.println("num: 7"); break;
      case 74:  Serial.println("num: 8"); break;
      case 82:  Serial.println("num: 9"); break;
      default:
        Serial.print(IrReceiver.decodedIRData.command);
        Serial.println("=> unknown button");
    }

    // Prepare for the next IR frame
    IrReceiver.resume();
  }
}

Based on Wokwi (see the example from post #5) I have modified your sketch so that you may check if it does what you want.

I did not optimize it just made some changes to make it run and added a blue led that lights as long as one of the functions controlled by the keys 2,3,4 or 5 takes:

To be tested (and further improved!) here https://wokwi.com/projects/392422103389593601

/*

  Forum: https://forum.arduino.cc/t/i-want-use-a-remote-control/1235880/2
  Wokwi: https://wokwi.com/projects/392422103389593601

*/

#include <IRremote.hpp>

#define IR_RECEIVE_PIN 2

const int LED_PIN_1 = 6;  // Beispiel-Pin für die Rote LED
const int LED_PIN_2 = 5;  // Beispiel-Pin für die zweite LED
const int LED_PIN_3 = 4;  // Beispiel-Pin für die zweite LED
const int LED_PIN_4 = 9;  // Beispiel-Pin für eine Anzeige, solange die handleKey-Funktion läuft
void setup() {
  pinMode(LED_PIN_1, OUTPUT);
  pinMode(LED_PIN_2, OUTPUT);
  pinMode(LED_PIN_3, OUTPUT);
  pinMode(LED_PIN_4, OUTPUT);
  Serial.begin(115200);
  Serial.println("IR Receiver Button Decode");
  IrReceiver.begin(IR_RECEIVE_PIN);
}

void loop() {
  // Überprüfe, ob ein Infrarotsignal empfangen wurde
  if (IrReceiver.decode()) {
    // Überprüfe alle Key Values (ohne Arrays!)
    switch (IrReceiver.decodedIRData.command) {
      case 162: Serial.println("POWER"); break;
      case 226: Serial.println("MENU");  break;
      case 34:  Serial.println("TEST"); break;
      case 2:   Serial.println("PLUS"); break;
      case 194: Serial.println("BACK"); break;
      case 224: Serial.println("PREV."); break;
      case 168: Serial.println("PLAY"); break;
      case 144: Serial.println("NEXT"); break;
      case 104: Serial.println("num: 0"); break;
      case 152: Serial.println("MINUS"); break;
      case 176: Serial.println("key: C"); break;
      case 48:  Serial.println("num: 1");  break;
      case 24:  Serial.println("num: 2"); handleKey12();break;
      case 122: Serial.println("num: 3"); handleKey13(); break;
      case 16:  Serial.println("num: 4"); handleKey14();break;
      case 56:  Serial.println("num: 5"); handleKey15();break;
      case 90:  Serial.println("num: 6"); break;
      case 66:  Serial.println("num: 7"); break;
      case 74:  Serial.println("num: 8"); break;
      case 82:  Serial.println("num: 9"); break;
      default:
        Serial.print(IrReceiver.decodedIRData.command);
        Serial.println("=> unknown button");
    }
    IrReceiver.resume();
  }
}

void handleKey12() {
  digitalWrite(9, HIGH);
  digitalWrite(6, HIGH);
  Serial.println("Debuggggggg");
  digitalWrite(9, LOW);
}


void handleKey13() {
  digitalWrite(9, HIGH);
  digitalWrite(6, HIGH);
  delay(2000);
  digitalWrite(6, LOW);
  delay(1000);
  digitalWrite(9, LOW);
  Serial.println("Debuggggggg zwoooo");
}


void handleKey14() {
  digitalWrite(9, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(9, LOW);
  Serial.println("Debuggggggg drüüü");
}


void handleKey15() {
  digitalWrite(9, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);
  delay(1000);
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);
  delay(1000);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  delay(500);
  digitalWrite(9, LOW);
  Serial.println("Debuggggggg foooor");
}

Good luck!
(Are you living in Switzerland? I recognized the "drüüü" in your counting :wink:

ec2021

He i am from Germany Not Switzerland. This Counting comes from The Movie Cool Runnings.
Actually im in a hury i News time to Read all the helping answers. Thx

HEY

I don't remember what I wanted to achieve with it, but I know it was intentional.

Maybe I'll start again from the beginning because I had to learn so many other things that I don't even remember what I did here.

Thanks for the answer though.

Maybe testing the sketch on Wokwi I supplied with post #6 can support your memory :slightly_smiling_face:

I assume you are aware of the German part in this forum ("Kategorie Deutsch") where you can get support in your (and my :wink: ) native language?!?

Good luck (und viel Spaß mit Arduino)
ec2021

Thx for the adwisewith the German forum it seems the Code with the Cases works so gar.