Code funtkioniert nicht richtig

Hallo,
ich habe hier einen Code mit integrierter Fernsteuerung. Die Verkabelung von Motoren und Sensoren ist richtig, denn eine Aktion wird immer ausgeführt, egal welche Taste man drückt, wenn man die andere Aktion ausführen will muss man aber den Resetkonpf drücken.
Wäre nett, wenn mir jemand helfen könnte
LG

#include "IRremote.h"
#include "Servo.h"
#include "Stepper.h"
#define SERVOPIN 10
#define SCHRITTEPROUMDREHUNG 2048
int RECV_PIN = 2; 
Servo Servomotor;
IRrecv Empfaenger(RECV_PIN);
decode_results Daten;
int directionPin_A = 12; // Definiere die Pins
int pwmPin_A = 3;
int brakePin_A = 9;
int directionPin_B = 13;
int pwmPin_B = 11;
int brakePin_B = 8;
bool directionState_A; 

Stepper Schrittmotor(SCHRITTEPROUMDREHUNG, 4, 6, 5, 7);

void setup()
{
  Serial.begin(9600);
  Empfaenger.enableIRIn(); 
  Servomotor.attach(SERVOPIN);
  Servomotor.write(90);
}

void loop() {
  if (Empfaenger.decode(&Daten)) {
    if(Daten.value == 0xFF30CF){
      Serial.println("Taste 1 erkannt");
      Servomotor.write(0);
      Schrittmotor.setSpeed(4);
      Schrittmotor.step(-SCHRITTEPROUMDREHUNG/4*3);
      delay(500);
      digitalWrite(directionPin_B, HIGH);
      digitalWrite(brakePin_B, LOW);
      analogWrite(pwmPin_B, 180);
      delay(2000);
      digitalWrite(directionPin_B, LOW);
      analogWrite(pwmPin_B, 0);
      delay(2000);
      digitalWrite(brakePin_B, LOW);
      analogWrite(pwmPin_B, 180);
      delay(200);
      analogWrite(pwmPin_B, 0);
      Schrittmotor.setSpeed(4);
      Schrittmotor.step(SCHRITTEPROUMDREHUNG/4*3);
      delay(500);
      digitalWrite(directionPin_A, HIGH);
      digitalWrite(brakePin_A, LOW);
      analogWrite(pwmPin_A, 255);
      delay(1000);
      analogWrite(pwmPin_A, 0);
      delay(1000);
      digitalWrite(directionPin_A, LOW);
      analogWrite(pwmPin_A, 255);
      delay(1000);
      analogWrite(pwmPin_A, 0);
      Empfaenger.resume();
    }
    else if(Daten.value == 0xFF18E7){
      Serial.println("taste 2 erkannt");
      Servomotor.write(180);
      Schrittmotor.setSpeed(4);
      Schrittmotor.step(-SCHRITTEPROUMDREHUNG/4*3);
      delay(500);
      digitalWrite(directionPin_B, HIGH);
      digitalWrite(brakePin_B, LOW);
      analogWrite(pwmPin_B, 180);
      delay(2000);
      digitalWrite(directionPin_B, LOW);
      analogWrite(pwmPin_B, 0);
      delay(2000);
      digitalWrite(brakePin_B, LOW);
      analogWrite(pwmPin_B, 180);
      delay(200);
      analogWrite(pwmPin_B, 0);
      Schrittmotor.setSpeed(4);
      Schrittmotor.step(SCHRITTEPROUMDREHUNG/4*3);
      delay(500);
      digitalWrite(directionPin_A, HIGH);
      digitalWrite(brakePin_A, LOW);
      analogWrite(pwmPin_A, 255);
      delay(1000);
      analogWrite(pwmPin_A, 0);
      delay(1000);
      digitalWrite(directionPin_A, LOW);
      analogWrite(pwmPin_A, 255);
      delay(1000);
      analogWrite(pwmPin_A, 0);
      Empfaenger.resume();
    }
    Empfaenger.resume();
  }
}

Welchen? Den am arduino?

ja genau den

int brakePin_A = 9;

Versuchen Sie, einen anderen DIO-Pin für diese Bremse zu verwenden (nicht-PWM). ?Timer1? ist an Pin 9 angeschlossen und kann Probleme mit IRremote verursachen.

Welche Bibliothek in welcher Version verwendest Du?

In der mir bekannten in der aktuellen Version steht #include <IRremote.hpp>.

keine Ahnung, aber es gab bisher noch keine Probleme damit

Ich bekomme eine Warnung mit meiner irRemote. In neueren dürfte das nicht anders sein:

warning: 'bool IRrecv::decode(decode_results*)' is deprecated: Please use IrReceiver.decode() without a parameter and IrReceiver.decodedIRData.<fieldname> . [-Wdeprecated-declarations]

Ich hab mal was umgebaut, ABER: Du bist nach dem auslösen einer aktion 10 Sekunden blind für einen Tastendruck!
Es muss das delay überall raus, sonst wird das nix.

#include "IRremote.h"
#include "Servo.h"
#include "Stepper.h"
#define SERVOPIN 10
#define SCHRITTEPROUMDREHUNG 2048
int RECV_PIN = 2;
Servo Servomotor;
IRrecv Empfaenger(RECV_PIN);
decode_results Daten;
int directionPin_A = 12; // Definiere die Pins
int pwmPin_A = 3;
int brakePin_A = 9;
int directionPin_B = 13;
int pwmPin_B = 11;
int brakePin_B = 8;
bool directionState_A;

Stepper Schrittmotor(SCHRITTEPROUMDREHUNG, 4, 6, 5, 7);
uint32_t oldData;

void setup()
{
  Serial.begin(9600);
  Empfaenger.enableIRIn();
  Servomotor.attach(SERVOPIN);
  Servomotor.write(90);
}

void loop()
{
  if (Empfaenger.decode(&Daten) != oldData)
  {
    oldData = Daten.value;
    switch (Daten.value)
    {
      case  0xFF30CF:
        {
          Serial.println("Taste 1 erkannt");
          Servomotor.write(0);
          Schrittmotor.setSpeed(4);
          Schrittmotor.step(-SCHRITTEPROUMDREHUNG / 4 * 3);
          delay(500);
          digitalWrite(directionPin_B, HIGH);
          digitalWrite(brakePin_B, LOW);
          analogWrite(pwmPin_B, 180);
          delay(2000);
          digitalWrite(directionPin_B, LOW);
          analogWrite(pwmPin_B, 0);
          delay(2000);
          digitalWrite(brakePin_B, LOW);
          analogWrite(pwmPin_B, 180);
          delay(200);
          analogWrite(pwmPin_B, 0);
          Schrittmotor.setSpeed(4);
          Schrittmotor.step(SCHRITTEPROUMDREHUNG / 4 * 3);
          delay(500);
          digitalWrite(directionPin_A, HIGH);
          digitalWrite(brakePin_A, LOW);
          analogWrite(pwmPin_A, 255);
          delay(1000);
          analogWrite(pwmPin_A, 0);
          delay(1000);
          digitalWrite(directionPin_A, LOW);
          analogWrite(pwmPin_A, 255);
          delay(1000);
          analogWrite(pwmPin_A, 0);
          break;
        case 0xFF18E7:
          Serial.println("taste 2 erkannt");
          Servomotor.write(180);
          Schrittmotor.setSpeed(4);
          Schrittmotor.step(-SCHRITTEPROUMDREHUNG / 4 * 3);
          delay(500);
          digitalWrite(directionPin_B, HIGH);
          digitalWrite(brakePin_B, LOW);
          analogWrite(pwmPin_B, 180);
          delay(2000);
          digitalWrite(directionPin_B, LOW);
          analogWrite(pwmPin_B, 0);
          delay(2000);
          digitalWrite(brakePin_B, LOW);
          analogWrite(pwmPin_B, 180);
          delay(200);
          analogWrite(pwmPin_B, 0);
          Schrittmotor.setSpeed(4);
          Schrittmotor.step(SCHRITTEPROUMDREHUNG / 4 * 3);
          delay(500);
          digitalWrite(directionPin_A, HIGH);
          digitalWrite(brakePin_A, LOW);
          analogWrite(pwmPin_A, 255);
          delay(1000);
          analogWrite(pwmPin_A, 0);
          delay(1000);
          digitalWrite(directionPin_A, LOW);
          analogWrite(pwmPin_A, 255);
          delay(1000);
          analogWrite(pwmPin_A, 0);
          break;
        }
    }
  }
  Empfaenger.resume();
}

Wie im vorigen Posting schon angeklungen, der Code passt nicht mehr zur aktuellen Version von IRRemote.

So sollte es aber funktionieren:

#define DECODE_NEC        // DECODE_NEC
#include <IRremote.hpp>   
#include "Servo.h"
#include "Stepper.h"

constexpr uint8_t RECV_PIN {2};  // IR Receive pin 2 ist die Standardeinstellung (Uno/Nano)
constexpr uint8_t SERVO_PIN {10};
constexpr uint8_t A_DIRECTION_PIN {12};
constexpr uint8_t B_DIRECTION_PIN {13};
constexpr uint8_t A_PWM_PIN {3};
constexpr uint8_t B_PWM_PIN {11};
constexpr uint8_t A_BRAKE_PIN {9};
constexpr uint8_t B_BRAKE_PIN {8};

constexpr int SCHRITTE_PRO_UMDREHUNG {2048};

bool A_DIRECTION_STATE;

Stepper Schrittmotor(SCHRITTE_PRO_UMDREHUNG, 4, 6, 5, 7);
Servo Servomotor;

uint16_t irReceive() {
  uint16_t received {0};

  if (IrReceiver.decode()) {
    IrReceiver.printIRResultShort(&Serial);
    if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
      // We have an unknown protocol here, print more info
      IrReceiver.printIRResultRawFormatted(&Serial, true);
    }
    if (IrReceiver.decodedIRData.protocol == NEC) {
      received = IrReceiver.decodedIRData.command;
      Serial.print("Command: 0x");
      Serial.println(received, HEX);
    }
    IrReceiver.resume();
  }
  return received;
}

void actions(uint16_t code) {
  switch (code) {
    case 0x30:   // Taste 1
      Serial.println("Taste 1 erkannt");
      Servomotor.write(0);
      Schrittmotor.setSpeed(4);
      Schrittmotor.step(-SCHRITTE_PRO_UMDREHUNG / 4 * 3);
      delay(500);
      digitalWrite(B_DIRECTION_PIN, HIGH);
      digitalWrite(B_BRAKE_PIN, LOW);
      analogWrite(B_PWM_PIN, 180);
      delay(2000);
      digitalWrite(B_DIRECTION_PIN, LOW);
      analogWrite(B_PWM_PIN, 0);
      delay(2000);
      digitalWrite(B_BRAKE_PIN, LOW);
      analogWrite(B_PWM_PIN, 180);
      delay(200);
      analogWrite(B_PWM_PIN, 0);
      Schrittmotor.setSpeed(4);
      Schrittmotor.step(SCHRITTE_PRO_UMDREHUNG / 4 * 3);
      delay(500);
      digitalWrite(A_DIRECTION_PIN, HIGH);
      digitalWrite(A_BRAKE_PIN, LOW);
      analogWrite(A_PWM_PIN, 255);
      delay(1000);
      analogWrite(A_PWM_PIN, 0);
      delay(1000);
      digitalWrite(A_DIRECTION_PIN, LOW);
      analogWrite(A_PWM_PIN, 255);
      delay(1000);
      analogWrite(A_PWM_PIN, 0);
      break;
    case 0x18:   // Taste 2
      Serial.println("taste 2 erkannt");
      Servomotor.write(180);
      Schrittmotor.setSpeed(4);
      Schrittmotor.step(-SCHRITTE_PRO_UMDREHUNG / 4 * 3);
      delay(500);
      digitalWrite(B_DIRECTION_PIN, HIGH);
      digitalWrite(B_BRAKE_PIN, LOW);
      analogWrite(B_PWM_PIN, 180);
      delay(2000);
      digitalWrite(B_DIRECTION_PIN, LOW);
      analogWrite(B_PWM_PIN, 0);
      delay(2000);
      digitalWrite(B_BRAKE_PIN, LOW);
      analogWrite(B_PWM_PIN, 180);
      delay(200);
      analogWrite(B_PWM_PIN, 0);
      Schrittmotor.setSpeed(4);
      Schrittmotor.step(SCHRITTE_PRO_UMDREHUNG / 4 * 3);
      delay(500);
      digitalWrite(A_DIRECTION_PIN, HIGH);
      digitalWrite(A_BRAKE_PIN, LOW);
      analogWrite(A_PWM_PIN, 255);
      delay(1000);
      analogWrite(A_PWM_PIN, 0);
      delay(1000);
      digitalWrite(A_DIRECTION_PIN, LOW);
      analogWrite(A_PWM_PIN, 255);
      delay(1000);
      analogWrite(A_PWM_PIN, 0);
      break;
  }
}

void setup() {
  Serial.begin(9600);

  IrReceiver.begin(RECV_PIN);
  Serial.print(F("Ready to receive IR signals at pin "));
  Serial.println(RECV_PIN);

  Servomotor.attach(SERVO_PIN);
  Servomotor.write(90);
}

void loop() { actions(irReceive()); }

Der Code im Ganzen ist nicht getestet. Die irReceive() Funktion aber schon....

1 Like

Dankeschön, jetzt funktioniert es

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