Need Help IR switch case (servo does not move contantly)

Hey, i want a servo turret´s movement to be toggled on / off by a remote control. I can receive a signal and can move the turret when i press the on button. But the Turret only moves a very short time and only moves again, if i press “On” again. How can i change that?
Edit: i looked in the seriall monitor and the movement works… as long as the latest IR code i reveived the “Start” one was. But i constantly receive some random codes.

#include <Servo.h> //Eine Bibliothek wird benötigt, um die Servos anzusprechen.
#include <IRremote.h>
boolean Schalter = false;
const int RECV_PIN = 4;

// Define IR Receiver and Results Objects
IRrecv irrecv(RECV_PIN);
decode_results results;

Servo Servo1;
Servo Servo2;

int ControllerX = A0;    //Pin 0 (Analog) für den Wert X des Joysticks
int ControllerY = A1;    //Pin 1 (Analog) für den Wert Y des Joysticks
int servo1 = 3;         //Pin 3 für Servo 1
int servo2 = 5;         //Pin 5 für Servo 2


void setup() {
  Servo1.attach(servo1); //Servos werden als Servos deklariert
  Servo2.attach(servo2);


  Serial.begin(9600);
  // Enable the IR Receiver
  irrecv.enableIRIn();

  pinMode(13, OUTPUT);
}

void loop() {
  IR();

  switch (results.value) {
    case 0xFFA25D:
      Bewegung();
      break;

    case 0xFF629D:

      break;

  }


}



void Bewegung() {

  int WertX = analogRead(ControllerX); //Nimmt den Wert X des Joysticks und setzt ihn als WertX
  int WertY = analogRead(ControllerY); //Nimmt den Wert Y des Joysticks und setzt ihn als WertY

  WertY = map(WertY, 0, 1023, 5, 180); //Zeichnet den Y-Wert des Controllers auf, um ihn auf den Servo anzuwenden.

  WertX = map(WertX, 0, 1023, 5, 180); //Zeichnet den X-Wert des Controllers auf, um ihn auf den Servo anzuwenden.

  Servo1.write(WertX);  //Aktualisiert den Wert des Servos mit der Position des Controllers. (X-Wert)

  Servo2.write(WertY);  //Aktualisiert den Wert des Servos mit der Position des Controllers. (Y-Wert)

  delay(7); //Damit der Arduino nicht jeden "Tick" den Wert aktualisiert

}

void IR() {
  if (irrecv.decode(&results)) {
    // Print Code in HEX
    Serial.println(results.value, HEX);
    irrecv.resume();
  }
}

I’ll guess that the results variable gets cleared when you try to read it again. Have a boolean that records whether the joysticks should be active or not. Set it true for 0xFFA25D, false for 0xFF629D.

Use the boolean to decide whether to call Bewegung.

1 Like

Hello
your code needs a control variable for the execution of the Bewegungs function. This control variable has to be modified by the ir interface.

1 Like

Edit: I looked in the seriall monitor and the movement works… as long as the latest IR code i reveived the FFA25D (Start) one was. But i constantly receive some random codes and those stop the movement again.

For example, this is the seriall monitor. When i press the on-button, a few seconds after some random code comes up and stops the movement.
FFA25D
73D189CF
FFA25D
4CB0FADC
FEAC02E7
FFA25D
25AE7EE1
FFA25D
6B9471D8
FFA25D
4AB0F7B7
FFA25D
FEAC02E5
FCABFFBF
FFA25D
8EA9365C

I’d like to know where those random codes come from, but they don’t really matter. The solution I suggested in #2 still works.

Thanks a lot, your right. It works, yeah!!!

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