Rotate the turret to a given angle

Friends, I’m trying to create a turret here, but I ran into a problem: I can’t figure out how to do everything, so that when I sequentially press three numeric keys on the remote control, the turret turns to a given angle. I will be glad for any help. Here is the code itself:

#include <Servo.h>
#include <IRremote.h>

int RECV_PIN = 4;
IRrecv irrecv(RECV_PIN);
decode_results results;
Servo servZ;
Servo servX;

int Zpos = 60;
int Xpos = 60;
int x = 0;

void setup() {
   servZ.attach(8); 
   servX.attach(10); 
   irrecv.enableIRIn();
   servZ.write(Zpos);
   servX.write(Xpos);
}

void loop() { 

  if (irrecv.decode(&results)) {
    if (results.value == 16718055) {
      up();
    } else if (results.value == 16730805) {
      down();   
    } else if (results.value == 16716015) {
      left();   
    } else if (results.value == 16734885) {
      right();   
    } else if (results.value == 16738455) {
      axisX();   
    }

    irrecv.resume();
  }
  delay(100);
}

void up() {
  Zpos=Zpos+10;
  servZ.write(Zpos);
  delay(100);
}
 
void down() {
  Zpos=Zpos-10;
  servZ.write(Zpos);
  delay(100);
}
 
void left() {
  Xpos=Xpos-10;
  servX.write(Xpos);
  delay(100);
}

void right() {
  Xpos=Xpos+10;
  servX.write(Xpos);
  delay(100);
}
 
void axisX() {
  x = 0;
  if (irrecv.decode(&results)){
    while (results.value != 16726215){
      for (int i = 3; i > 1; i--){
        if (results.value == 16750695){
            int x = x + pow(0, i);
          }
          else if (results.value == 16753245){
            int x = x + pow(1, i);
          }
          else if (results.value == 16736925){
            int x = x + pow(2, i);
          }
            else if (results.value == 16769565){
            int x = x + pow(3, i);
          }
            else if (results.value == 16720605){
            int x = x + pow(4, i);
          }
            else if (results.value == 16712445){
            int x = x + pow(5, i);
          }
            else if (results.value == 16748655){
            int x = x + pow(6, i);
          }
            else if (results.value == 16769055){
            int x = x + pow(7, i);
          }
            else if (results.value == 16754775){
            int x = x + pow(8, i);
          }
            else if (results.value == 16748655){
            int x = x + pow(9, i);
          }
        }
    }
    if(results.value == 16726215){
      servX.write(x);
    }
  }
}

When you use "int x" inside each of the if statements, you are declaring a new local variable x that ceases to exist at the end of the if statement. Leave off the "int" and it will use the global variable x.

void axisX() {
  x = 0;
  if (irrecv.decode(&results)) {
    while (results.value != 16726215) {
      for (int i = 3; i > 1; i--) {
        if (results.value == 16750695) {
          int x = x + pow(0, i);
        }
        else if (results.value == 16753245) {
          int x = x + pow(1, i);
        }
        else if (results.value == 16736925) {
          int x = x + pow(2, i);
        }
        else if (results.value == 16769565) {
          int x = x + pow(3, i);
        }
        else if (results.value == 16720605) {
          int x = x + pow(4, i);
        }
        else if (results.value == 16712445) {
          int x = x + pow(5, i);
        }
        else if (results.value == 16748655) {
          int x = x + pow(6, i);
        }
        else if (results.value == 16769055) {
          int x = x + pow(7, i);
        }
        else if (results.value == 16754775) {
          int x = x + pow(8, i);
        }
        else if (results.value == 16748655) {
          int x = x + pow(9, i);
        }
      }
    }
    if (results.value == 16726215) {
      servX.write(x);
    }
  }
}

The use of pow() is a bit unusual, a lot more common to multiple the current x by 10 then add the number.

not sure about the codes
consider this approach for axis()

void axisX () {
    x = 0;

    while (true)  {
        if (! irrecv.decode (&results))
            continue;

        ...

        if (Two == results.value)  {
            x = 10*x + 2;

        ...

        else if (Go results.value)  {
            servX.write (x);
            return;
        }
    }
}

you can probably use a switch statement instead of "ifs" if you truncated the code values -- code % 0x10000

of course it would be helpful to define symbols (e.g. Up, Down, Two, Four, ...) for the codes

enum {
    Up     = 0x18e7,  // 16718055  0x00ff18e7
    Down   = 0x4ab5,  // 16730805  0x00ff4ab5
    Left   = 0x10ef,  // 16716015  0x00ff10ef
    Right  = 0x5aa5,  // 16734885  0x00ff5aa5
    Axis   = 0x6897,  // 16738455  0x00ff6897
    One    = 0x38c7,  // 16726215  0x00ff38c7
    Two    = 0x9867,  // 16750695  0x00ff9867
    Three  = 0xa25d,  // 16753245  0x00ffa25d
    Four   = 0x629d,  // 16736925  0x00ff629d
    Five   = 0xe21d,  // 16769565  0x00ffe21d
    Six    = 0x22dd,  // 16720605  0x00ff22dd
    Seven  = 0x02fd,  // 16712445  0x00ff02fd
    Eight  = 0x906f,  // 16748655  0x00ff906f
    Nine   = 0xe01f,  // 16769055  0x00ffe01f
    Go     = 0xa857,  // 16754775  0x00ffa857
};

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