IR codes Senden funktieniert nicht

Hallo,

ich will für ein Projekt Motoren über infrarot steuern. Dazu habe ich einen Motor Treiber mit Motor und einen ir Receiver an einen arduino uno angeschlossen. Zusätzlich habe ich noch eine ir led und zwei knöpfe(Richtung regulieren) an einen Arduino nano angeschlossen. Das Problem ist das die ir led keine signale sendet, aber die ir led ist nicht kaputt. Kann jemand mal bitte den code überprüfen:


const int irLedPin = 10;   // IR LED Pin
const int forwardButton = 9; // Vorwärts-Button Pin
const int backwardButton = 8; // Rückwärts-Button Pin

IRsend irsend;

void setup() {
  pinMode(irLedPin, OUTPUT);
  pinMode(forwardButton, INPUT_PULLUP);
  pinMode(backwardButton, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(forwardButton) == LOW) {
    Serial.println("Forward button pressed");
    irsend.sendNEC(0x1FE48B7, 32); // Vorwärts
    delay(200); // Kurze Verzögerung zur Entprellung
  }
  if (digitalRead(backwardButton) == LOW) {
    Serial.println("Backward button pressed");
    irsend.sendNEC(0x1FE58A7, 32); // Rückwärts
    delay(200); 
  }
}

Warum postest du nur ein Fragment des Sketches ?
Zeig uns bitte den kompletten Sketch.
Und woher weißt du, das die IR-Led nichts sendet ?
Wie hast du das geprüft ?

Das kannst du z.B. mit einer Kamera des Handys kontrollieren.

ich habe es schon mit einer kamera getestet und mit einem ir reciver(hat nichts aufhegenommen)

#include <IRremote.h>

const int irReceiverPin = 8; // IR-Empfänger Pin
const int motor1Input1 = 3;
const int motor1Input2 = 4;
const int enable1 = 9;

IRrecv irrecv(irReceiverPin);
decode_results results;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

  pinMode(motor1Input1, OUTPUT);
  pinMode(motor1Input2, OUTPUT);
  pinMode(enable1, OUTPUT);
  
  digitalWrite(enable1, HIGH); // Enable Motor 1
}

void loop() {
  if (irrecv.decode(&results)) {
    unsigned long irCode = results.value;
    Serial.println(irCode, HEX);

    if (irCode == 0x1FE48B7) { // Vorwärts
      digitalWrite(motor1Input1, HIGH);
      digitalWrite(motor1Input2, LOW);
    } else if (irCode == 0x1FE58A7) { // Rückwärts
      digitalWrite(motor1Input1, LOW);
      digitalWrite(motor1Input2, HIGH);
    } else {
  
      digitalWrite(motor1Input1, LOW);
      digitalWrite(motor1Input2, LOW);
    }

    irrecv.resume(); // Receive the next value
  }
}

hier ist der andere teil des codes

Ok und was siehst du da ?

Und was heißt das genau ?
Sind beide zusammen auf einem Controller?

Ich wurde erst den IR zum laufen bringen danach den Rest.
Ich sehe noch was :wink:
Teste das mall

#include <IRremote.h>

const int irReceiverPin = 8; // IR-Empfänger Pin
const int motor1Input1 = 3;
const int motor1Input2 = 4;
const int enable1 = 9;

IRrecv irrecv(irReceiverPin);
decode_results results;
unsigned long irCode=0;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

  pinMode(motor1Input1, OUTPUT);
  pinMode(motor1Input2, OUTPUT);
  pinMode(enable1, OUTPUT);

  digitalWrite(enable1, HIGH); // Enable Motor 1
}

void loop() {
  if (irrecv.decode(&results)) {
    irCode = results.value;
    Serial.println(irCode, HEX);
    irrecv.resume();
  }
  if (irCode == 0x1FE48B7) { // Vorwärts
    digitalWrite(motor1Input1, HIGH);
    digitalWrite(motor1Input2, LOW);
  } else if (irCode == 0x1FE58A7) { // Rückwärts
    digitalWrite(motor1Input1, LOW);
    digitalWrite(motor1Input2, HIGH);
  } else {

    digitalWrite(motor1Input1, LOW);
    digitalWrite(motor1Input2, LOW);
  }

  // Receive the next value
}

normalerweise würde ich ja ein lila blinken sehen, aber in meinem fall nichts. den code zum senden hab ich auf einem arduino nano und der zum aufnehmen und motoren steuern auf einem uno.

Und um den Sketch zum Senden geht es.
Der ist nicht vollständig. So kann der nicht funktionieren.

was fehlt da?

Die Einbindung der Library.
Da müsste normalerweise ein Fehler kommen.

Wo ist
IrSender.begin();

oh sorry, ich hab das in meinem code aber habs hier vergessen

nicht da. wo muss das hin

Dafür gibt es doch Beispiele. Einfach nachbauen.
Und immer richtig lesen, was wir schreiben.

In Setup, jedoch dein Sende Sketch ist nicht so aufgebaut wie in den Beispielen.

/*
 * SendAndReceive.cpp
 *
 * Demonstrates sending IR codes and receiving it simultaneously
 *
 *  This file is part of Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote.
 *
 ************************************************************************************
 * MIT License
 *
 * Copyright (c) 2021 Armin Joachimsmeyer
 *
 * 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 <Arduino.h>

// select only NEC and the universal decoder for pulse distance protocols
#define DECODE_NEC          // Includes Apple and Onkyo
#define DECODE_DISTANCE     // in case NEC is not received correctly

//#define EXCLUDE_UNIVERSAL_PROTOCOLS // Saves up to 1000 bytes program memory.
//#define EXCLUDE_EXOTIC_PROTOCOLS
//#define SEND_PWM_BY_TIMER
//#define USE_NO_SEND_PWM
//#define NO_LED_FEEDBACK_CODE // saves 500 bytes program memory
//#define DEBUG // Activate this for lots of lovely debug output from the decoders.

#include "PinDefinitionsAndMore.h" // Define macros for input and output pin etc.
#include <IRremote.hpp>

#define DELAY_AFTER_SEND 2000
#define DELAY_AFTER_LOOP 5000

void setup() {
    Serial.begin(115200);
#if defined(__AVR_ATmega32U4__) || defined(SERIAL_PORT_USBVIRTUAL) || defined(SERIAL_USB) /*stm32duino*/|| defined(USBCON) /*STM32_stm32*/|| defined(SERIALUSB_PID) || defined(ARDUINO_attiny3217)
    delay(4000); // To be able to connect Serial monitor after reset or power up and before first print out. Do not wait for an attached Serial Monitor!
#endif
    // Just to know which program is running on my Arduino
    Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));

    // Start the receiver and if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED
    IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);

#if defined(IR_SEND_PIN)
    IrSender.begin(); // Start with IR_SEND_PIN as send pin and enable feedback LED at default feedback LED pin
#else
    IrSender.begin(3, ENABLE_LED_FEEDBACK); // Specify send pin and enable feedback LED at default feedback LED pin
#endif

    Serial.print(F("Ready to receive IR signals of protocols: "));
    printActiveIRProtocols(&Serial);
    Serial.println(F("at pin " STR(IR_RECEIVE_PIN)));

    Serial.println(F("Ready to send IR signals at pin " STR(IR_SEND_PIN)));


#if FLASHEND >= 0x3FFF  // For 16k flash or more, like ATtiny1604
// For esp32 we use PWM generation by ledcWrite() for each pin.
#  if !defined(SEND_PWM_BY_TIMER) && !defined(USE_NO_SEND_PWM) && !defined(ESP32)
    /*
     * Print internal software PWM generation info
     */
    IrSender.enableIROut(38); // Call it with 38 kHz to initialize the values printed below
    Serial.print(F("Send signal mark duration is "));
    Serial.print(IrSender.periodOnTimeMicros);
    Serial.print(F(" us, pulse correction is "));
    Serial.print(IrSender.getPulseCorrectionNanos());
    Serial.print(F(" ns, total period is "));
    Serial.print(IrSender.periodTimeMicros);
    Serial.println(F(" us"));
#  endif

    // infos for receive
    Serial.print(RECORD_GAP_MICROS);
    Serial.println(F(" us is the (minimum) gap, after which the start of a new IR packet is assumed"));
    Serial.print(MARK_EXCESS_MICROS);
    Serial.println(F(" us are subtracted from all marks and added to all spaces for decoding"));
#endif
}

uint16_t sAddress = 0x0102;
uint8_t sCommand = 0x34;
uint8_t sRepeats = 1;

/*
 * Send NEC IR protocol
 */
void send_ir_data() {
    Serial.print(F("Sending: 0x"));
    Serial.print(sAddress, HEX);
    Serial.print(sCommand, HEX);
    Serial.println(sRepeats, HEX);

    // clip repeats at 4
    if (sRepeats > 4) {
        sRepeats = 4;
    }
    // Results for the first loop to: Protocol=NEC Address=0x102 Command=0x34 Raw-Data=0xCB340102 (32 bits)
    IrSender.sendNEC(sAddress, sCommand, sRepeats);
}

void receive_ir_data() {
    if (IrReceiver.decode()) {
        Serial.print(F("Decoded protocol: "));
        Serial.print(getProtocolString(IrReceiver.decodedIRData.protocol));
        Serial.print(F("Decoded raw data: "));
        Serial.print(IrReceiver.decodedIRData.decodedRawData, HEX);
        Serial.print(F(", decoded address: "));
        Serial.print(IrReceiver.decodedIRData.address, HEX);
        Serial.print(F(", decoded command: "));
        Serial.println(IrReceiver.decodedIRData.command, HEX);
        IrReceiver.resume();
    }
}

void loop() {
    /*
     * Print loop values
     */
    Serial.println();
    Serial.print(F("address=0x"));
    Serial.print(sAddress, HEX);
    Serial.print(F(" command=0x"));
    Serial.print(sCommand, HEX);
    Serial.print(F(" repeats="));
    Serial.println(sRepeats);
    Serial.flush();

    send_ir_data();
    // wait for the receiver state machine to detect the end of a protocol
    delay((RECORD_GAP_MICROS / 1000) + 5);
    receive_ir_data();

    // Prepare data for next loop
    sAddress += 0x0101;
    sCommand += 0x11;
    sRepeats++;

    delay(100); // Loop delay
}

#include <IRremote.h>

const int irLedPin = 10;   // IR LED Pin
const int forwardButton = 9; // Vorwärts-Button Pin
const int backwardButton = 8; // Rückwärts-Button Pin

IRsend irsend; // IR-Sender Objekt erstellen

void setup() {
  pinMode(irLedPin, OUTPUT);
  pinMode(forwardButton, INPUT_PULLUP);
  pinMode(backwardButton, INPUT_PULLUP);
  Serial.begin(9600);
  IrSender.begin(); // Initialisiere den IR-Sender
}

void loop() {
  if (digitalRead(forwardButton) == LOW) {
    Serial.println("Forward button pressed");
    irsend.sendNEC(0x1FE48B7, 32); // Vorwärts
    delay(200); // Kurze Verzögerung zur Entprellung
  }
  if (digitalRead(backwardButton) == LOW) {
    Serial.println("Backward button pressed");
    irsend.sendNEC(0x1FE58A7, 32); // Rückwärts
    delay(200); // Kurze Verzögerung zur Entprellung
  }
}

der code ist jetzt richtig oder? Aber ich bekomm diesen Fehler:

exit status 1

Compilation error: no matching function for call to 'IRsend::begin()'

Warum?

Tippfehler !

Hast du dir das mal im Beispiel der Lib angesehen ?

Du verwendest eine andere Library. Das passt so nicht.

Ist komplett falsch aufgebaut.
Der TO will NEC Code senden aber deklariert das erst in Loop, man muss sich schon halten was und die Lib das will,

hä ich verstehe das nicht

IrSender.begin(10) // Sende Pin
dann kompiliert ob tuts weis nicht und die 32 ändern auf 38

irsend.sendNEC(0x1FE58A7, 32);