1

I am making an IR controlled helicopter and I have the very basic part of the code done, but I don't know how to make the motor gradually increase speed. I want the motor to gradually increase speed when you hold the button on the IR remote and hold its speed when you stop pushing the button. And the same for when the helicopter goes down. Here is the code:

   #include <IRremote.h>
   #include <Servo.h>
   #define enA 9
   #define enB 11
   #define in1 6
   #define in2 7
   #define in3 4
   #define in4 5

   Servo ball1;

   int IR_RECEIVE_PIN = 2;
   IRrecv IrReceiver(IR_RECEIVE_PIN);
   decode_results results;

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

     ball1.attach(10);

     pinMode(in1, OUTPUT);
     pinMode(in2, OUTPUT);
     pinMode(in3, OUTPUT);
     pinMode(in4, OUTPUT);
   }
   void loop() {
     if (IrReceiver.decode(&results)) {
       switch (results.value) {

         case 0xFFA857:
           Serial.println("case: UP");
           digitalWrite(enA, 255);
           digitalWrite(in1, HIGH);
           digitalWrite(in2, LOW);
           break;

         case 0xFFE01F:
           Serial.println("case: DOWN");
           digitalWrite(enA, 120);
           digitalWrite(in1, LOW);
           digitalWrite(in2, LOW);
           break;

         case 0xFF18E7:
           Serial.println("case: FORWARD");
           ball1.write(90);
           break;

         case 0xFF10EF:
           Serial.println("case: LEFT");
           digitalWrite(in3, LOW);
           digitalWrite(in4, LOW);
           break;

         case 0xFF5AA5:
           Serial.println("case: RIGHT");
           digitalWrite(in3, HIGH);
           digitalWrite(in4, LOW);
           break;

         case 0xFF48B5:
           Serial.println("case: BACKWARD");
           ball1.write( ? ); // I need help on the direction of the linear servo
           break;
       }

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

It is the same code I used to control a RGB LED (But modified). Would it be possible to upload this code to an IC? I already programmed my UNO as an ISP, but there seems to be a problem. Also, I am using a linear servo for the pitch of the helicopter, can someone please show me how to change the direction of a linear servo? I have one that is similar to the Pico linear servo.

7
  • 1
    Should each case statement have a matching break statement?
    – tim
    Jul 18 at 4:43
  • 1
    Yes, I removed them when I was trying something, but I added them back now... Thanks!
    – Renier Jr.
    Jul 18 at 8:00
  • 1
    Properly indenting your code would make it much easier to follow.
    – StarCat
    Jul 18 at 8:28
  • 1
    Okay, I indented it...
    – Renier Jr.
    Jul 18 at 8:55
  • 2
    Every time you receive an "up" code increase the speed. Every time you receive a "down" code decrease the speed. IR doesn't (usually) have a concept of "press" and "release", only "press" and it sends the code repeatedly while it's pressed.
    – Majenko
    Jul 18 at 9:54
0

You could separate reading the controls and taking actions and there are several ways of doing that, one example shown below where the switch() can be used to read all commands and according to the value call one or more atomic functions. For instance, one function to change speed and another one to change direction, and perhaps calling several functions in a row for more complex tasks like take off and land (I have no idea how those helicopter projects works).

On each case you do the validation, min and max allowed values, dependencies on other factors and alike.

void loop() {

  if (IrReceiver.decode(&results)) {

    switch (results.value) {

      case 0xFFA857: // up
        verticalSpeed = verticalSpeed + verticalSpeedStep;
        if (verticalSpeed > maxVerticalSpeed) verticalSpeed = maxVerticalSpeed;
        setVericalMotor();
        break;

      case 0xFFE01F: //down
        verticalSpeed = verticalSpeed - verticalSpeedStep;
        if (verticalSpeed < 0) verticalSpeed = 0;
        setVericalMotor();
        break;

    }
  }
}

void setVericalMotor() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(enA, verticalSpeed);
}

As for your question regarding the servo, they usually run from 0 to 180 degrees, so tey anything in that range as the "reverse" for 90 degrees for Forward (normally that should be 0 or 180).

4
  • Thank you for the help!!
    – Renier Jr.
    Jul 18 at 14:25
  • I have not worked with verticalSpeed before, and the compiler says it is not declared in a scope along with verticalSpeedStep, and maxVerticalSpeed... Could you please help me?
    – Renier Jr.
    Jul 18 at 14:45
  • The code I showed you is not a complete program, just an idea how to design your program and as part of it you have to declare the variables. As you are new to programming I suggest you follow one of the many getting started with Arduino tutorials you can find on arduino.cc and many other websites.
    – Nino
    Jul 18 at 15:02
  • Okay, thank you!
    – Renier Jr.
    Jul 19 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.