How to make the claw open and close + arm move upward and downward in remote

Our device is a LAFVIN Mechanical 4WD Robot Arm Car V2.2. This is our code so far, would greatly appreciate your comments/suggestions for changes. Thank you!:

#include "IR_remote.h"
#include "keymap.h"

IRremote ir(3);

#include <Servo.h>

String BLE_val;
 int base_degrees;
 int arm_degrees;
 int claw_degrees;
 boolean forward_flag;
 boolean backward_flag;
 boolean left_flag;
 boolean right_flag;
 boolean claw_close_flag;
 boolean claw_open_flag;
 boolean arm_forward_flag;
 boolean claw_recracted_flag;
 boolean base_anticlockwise_flag;
 boolean base_clockwise_flag;
 boolean menory_action_flag;
 boolean Avoidance_Function_flag;
   boolean Following_Function_flag;
 boolean Anti_drop_Function_flag;
 boolean Line_tracking_Function_flag;
 boolean Gravity_sensor_Function_flag;
 int Left_Tra_Value;
 int Center_Tra_Value;
 int Right_Tra_Value;
 int Black_Line;
 int distance;
 int actions_count;
 int auto_count;
 int low_speed;
 int medium_speed;
 int high_speed;
 int speed_car;
int claw_read_degress[20]={0,0,0};

int arm_read_degress[20]={0,0,0};

int base_read_degress[20]={0,0,0};

Servo myservo1;
Servo myservo2;
Servo myservo3;
void read_degress() {
  if (actions_count <= 19) {
    claw_read_degress[(int)((actions_count + 1) - 1)] = myservo1.read();
    delay(50);
    arm_read_degress[(int)((actions_count + 1) - 1)] = myservo2.read();
    delay(50);
    base_read_degress[(int)((actions_count + 1) - 1)] = myservo3.read();
    delay(50);
    actions_count = actions_count + 1;
    auto_count = actions_count;
    Serial.println(auto_count);

    }           
}

void IR_control_Function() {
  if (ir.getIrKey(ir.getCode(),1) == IR_KEYCODE_UP) {
    Move_Forward(100);
    delay(300);
    Stop();

  } else if (ir.getIrKey(ir.getCode(),1) == IR_KEYCODE_DOWN) {
    Move_Backward(100);
    delay(300);
    Stop();
  } else if (ir.getIrKey(ir.getCode(),1) == IR_KEYCODE_LEFT) {
    Rotate_Left(70);
    delay(300);
    Stop();
  } else if (ir.getIrKey(ir.getCode(),1) == IR_KEYCODE_RIGHT) {
    Rotate_Right(70);
    delay(300);
    Stop();
  } else if (ir.getIrKey(ir.getCode(),1) == IR_KEYCODE_OK) {
    Stop();
  } else if (false) {
  } else if (false) {
  } else if (ir.getIrKey(ir.getCode(),1) == IR_KEYCODE_7) {
    claw_degrees = claw_degrees + 5;
    if (claw_degrees >= 180) {
      claw_degrees = 180;

    }
    myservo1.write(claw_degrees);
    delay(2);
  } else if (ir.getIrKey(ir.getCode(),1) == IR_KEYCODE_9) {
    claw_degrees = claw_degrees - 5;
    if (claw_degrees <= 50) {
      claw_degrees = 50;

    }
    myservo1.write(claw_degrees);
    delay(2);
  } else if (ir.getIrKey(ir.getCode(),1) == IR_KEYCODE_2) {
    arm_degrees = arm_degrees + 5;
    if (arm_degrees >= 180) {
      arm_degrees = 180;

    }
    myservo2.write(arm_degrees);
    delay(2);
  } else if (ir.getIrKey(ir.getCode(),1) == IR_KEYCODE_8) {
    arm_degrees = arm_degrees - 5;
    if (arm_degrees <= 0) {
      arm_degrees = 0;

    }
    myservo2.write(arm_degrees);
    delay(2);
  } else if (ir.getIrKey(ir.getCode(),1) == IR_KEYCODE_4) {
    base_degrees = base_degrees + 5;
    if (base_degrees >= 180) {
      base_degrees = 180;

    }
    myservo3.write(base_degrees);
    delay(2);
  } else if (ir.getIrKey(ir.getCode(),1) == IR_KEYCODE_6) {
    base_degrees = base_degrees - 5;
    if (base_degrees <= 0) {
      base_degrees = 0;

    }
    myservo3.write(base_degrees);
    delay(2);
  }
}

void auto_do() {
  Serial.println(auto_count);
  if (0 != auto_count) {
    menory_action_flag = true;
//hiii
  }
  actions_count = 0;
  claw_degrees = myservo1.read();
  arm_degrees = myservo2.read();
  base_degrees = myservo3.read();
  while (menory_action_flag) {
    for (int i = (1); i <= (auto_count); i = i + (1)) {
      if (Serial.read() == 's') {
        menory_action_flag = false;
        break;

      }
      if (claw_degrees < claw_read_degress[(int)(i - 1)]) {
        while (claw_degrees < claw_read_degress[(int)(i - 1)]) {
          claw_degrees = claw_degrees + 1;
          myservo1.write(claw_degrees);
          delay(15);
        }

      } else {
        while (claw_degrees > claw_read_degress[(int)(i - 1)]) {
          claw_degrees = claw_degrees - 1;
          myservo1.write(claw_degrees);
          delay(15);
        }

      }
      if (Serial.read() == 's') {
        menory_action_flag = false;
        break;

      }
      if (arm_degrees < arm_read_degress[(int)(i - 1)]) {
        while (arm_degrees < arm_read_degress[(int)(i - 1)]) {
          arm_degrees = arm_degrees + 1;
          myservo2.write(arm_degrees);
          delay(15);
        }

      } else {
        while (arm_degrees > arm_read_degress[(int)(i - 1)]) {
          arm_degrees = arm_degrees - 1;
          myservo2.write(arm_degrees);
          delay(15);
        }

      }
      if (Serial.read() == 's') {
        menory_action_flag = false;
        break;

      }
      if (base_degrees < base_read_degress[(int)(i - 1)]) {
        while (base_degrees < base_read_degress[(int)(i - 1)]) {
          base_degrees = base_degrees + 1;
          myservo3.write(base_degrees);
          delay(15);
        }

      } else {
        while (base_degrees > base_read_degress[(int)(i - 1)]) {
          base_degrees = base_degrees - 1;
          myservo3.write(base_degrees);
          delay(15);
        }

      }
      if (Serial.read() == 's') {
        menory_action_flag = false;
        break;

      }
    }
  }
}

void claw_close() {
  claw_close_flag = true;
  while (claw_close_flag) {
    claw_degrees = claw_degrees + 1;
    myservo1.write(claw_degrees);
    Serial.println(claw_degrees);
    delay(10);
    if (claw_degrees >= 180) {
      claw_degrees = 180;

    }
    if (Serial.read() == 's') {
      claw_close_flag = false;

    }
  }
}

void claw_open() {
  claw_close_flag = true;
  while (claw_close_flag) {
    claw_degrees = claw_degrees - 1;
    myservo1.write(claw_degrees);
    Serial.println(claw_degrees);
    delay(10);
    if (claw_degrees <= 50) {
      claw_degrees = 50;

    }
    if (Serial.read() == 's') {
      claw_close_flag = false;

    }
  }
}

void arm_up() {
  arm_forward_flag = true;
  while (arm_forward_flag) {
    arm_degrees = arm_degrees + 1;
    myservo2.write(arm_degrees);
    delay(10);
    Serial.println(arm_degrees);
    if (arm_degrees >= 180) {
      arm_degrees = 180;

    }
    if (Serial.read() == 's') {
      arm_forward_flag = false;

    }
  }
}

void arm_down() {
  claw_recracted_flag = true;
  while (claw_recracted_flag) {
    arm_degrees = arm_degrees - 1;
    myservo2.write(arm_degrees);
    Serial.println(arm_degrees);
    delay(10);
    if (arm_degrees <= 0) {
      arm_degrees = 0;

    }
    if (Serial.read() == 's') {
      claw_recracted_flag = false;

    }
  }
}

void Stop() {
  digitalWrite(2,LOW);
  analogWrite(5,0);
  digitalWrite(4,HIGH);
  analogWrite(6,0);
}

void arm_base_anticlockwise() {
  base_anticlockwise_flag = true;
  while (base_anticlockwise_flag) {
    base_degrees = base_degrees + 1;
    myservo3.write(base_degrees);
    Serial.println(base_degrees);
    delay(10);
    if (base_degrees >= 180) {
      base_degrees = 180;

    }
    if (Serial.read() == 's') {
      base_anticlockwise_flag = false;

    }
  }
}

void arm_base_clockwise() {
  base_clockwise_flag = true;
  while (base_clockwise_flag) {
    base_degrees = base_degrees - 1;
    myservo3.write(base_degrees);
    Serial.println(base_degrees);
    delay(10);
    if (base_degrees <= 0) {
      base_degrees = 0;

    }
    if (Serial.read() == 's') {
      base_clockwise_flag = false;

    }
  }
}

void Line_tracking_Function() {
  Line_tracking_Function_flag = true;
  while (Line_tracking_Function_flag) {
    Left_Tra_Value = digitalRead(7);
    Center_Tra_Value = digitalRead(8);
    Right_Tra_Value = digitalRead(A1);
    if (Left_Tra_Value != Black_Line && (Center_Tra_Value == Black_Line && Right_Tra_Value != Black_Line)) {
      Move_Forward(120);

    } else if (Left_Tra_Value == Black_Line && (Center_Tra_Value == Black_Line && Right_Tra_Value != Black_Line)) {
      Rotate_Left(80);
    } else if (Left_Tra_Value == Black_Line && (Center_Tra_Value != Black_Line && Right_Tra_Value != Black_Line)) {
      Rotate_Left(120);
    } else if (Left_Tra_Value != Black_Line && (Center_Tra_Value != Black_Line && Right_Tra_Value == Black_Line)) {
      Rotate_Right(120);
    } else if (Left_Tra_Value != Black_Line && (Center_Tra_Value == Black_Line && Right_Tra_Value == Black_Line)) {
      Rotate_Right(80);
    } else if (Left_Tra_Value == Black_Line && (Center_Tra_Value == Black_Line && Right_Tra_Value == Black_Line)) {
      Stop();
    }
    if (Serial.read() == 'S') {
      Line_tracking_Function_flag = false;
      Stop();

    }
  }
}

void Move_Backward(int speed) {
  digitalWrite(2,LOW);
  analogWrite(5,speed);
  digitalWrite(4,HIGH);
  analogWrite(6,speed);
}

float checkdistance() {
  digitalWrite(12, LOW);
  delayMicroseconds(2);
  digitalWrite(12, HIGH);
  delayMicroseconds(10);
  digitalWrite(12, LOW);
  float distance = pulseIn(13, HIGH) / 58.00;
  delay(10);
  return distance;
}

void Following_Function() {
  int Following_distance = 0;
  Following_Function_flag = true;
  while (Following_Function_flag) {
    Following_distance = checkdistance();
    if (Following_distance < 15) {
      Move_Backward(80);

    } else if (15 <= Following_distance && Following_distance <= 20) {
      Stop();
    } else if (20 <= Following_distance && Following_distance <= 25) {
      Move_Forward(80);
    } else if (25 <= Following_distance && Following_distance <= 30) {
      Move_Forward(100);
    } else {
      Stop();

    }
    if (Serial.read() == 'S') {
      Following_Function_flag = false;
      Stop();

    }
  }
}

void Rotate_Left(int speed) {
  digitalWrite(2,LOW);
  analogWrite(5,speed);
  digitalWrite(4,LOW);
  analogWrite(6,speed);
}

void Rotate_Right(int speed) {
  digitalWrite(2,HIGH);
  analogWrite(5,speed);
  digitalWrite(4,HIGH);
  analogWrite(6,speed);
}

void Move_Forward(int speed) {
  digitalWrite(2,HIGH);
  analogWrite(5,speed);
  digitalWrite(4,LOW);
  analogWrite(6,speed);
}

void Anti_drop_Function() {
  Anti_drop_Function_flag = true;
  while (Anti_drop_Function_flag) {
    Left_Tra_Value = digitalRead(7);
    Center_Tra_Value = digitalRead(8);
    Right_Tra_Value = digitalRead(A1);
    if (Left_Tra_Value != Black_Line && (Center_Tra_Value != Black_Line && Right_Tra_Value != Black_Line)) {
      Move_Forward(60);

    } else {
      Move_Backward(60);
      delay(600);
      Rotate_Left(60);
      delay(500);

    }
    if (Serial.read() == 'S') {
      Anti_drop_Function_flag = false;
      Stop();

    }
  }
}

void Move_backward_Function() {
  backward_flag = true;
  while (backward_flag) {
    Move_Backward(speed_car);
    if (Serial.read() == 'S') {
      backward_flag = false;
      Stop();

    }
  }
}

void Move_forward_Function() {
  forward_flag = true;
  while (forward_flag) {
    Move_Forward(speed_car);
    if (Serial.read() == 'S') {
      forward_flag = false;
      Stop();

    }
  }
}

void Avoidance_Function() {
  int Avoidance_distance = 0;
  Avoidance_Function_flag = true;
  while (Avoidance_Function_flag) {
    Avoidance_distance = checkdistance();
    if (Avoidance_distance <= 25) {
      if (Avoidance_distance <= 15) {
        Stop();
        delay(100);
        Move_Backward(100);
        delay(600);

      } else {
        Stop();
        delay(100);
        Rotate_Left(100);
        delay(600);

      }

    } else {
      Move_Forward(70);

    }
    if (Serial.read() == 'S') {
      Avoidance_Function_flag = false;
      Stop();

    }
  }
}

void Turn_right_Function() {
  right_flag = true;
  while (right_flag) {
    Rotate_Right(speed_car);
    if (Serial.read() == 'S') {
      right_flag = false;
      Stop();

    }
  }
}

void Turn_left_Function() {
  left_flag = true;
  while (left_flag) {
    Rotate_Left(speed_car);
    if (Serial.read() == 'S') {
      left_flag = false;
      Stop();

    }
  }
}

char bluetooth_val;

void Gravity_sensor_Function() {
  Gravity_sensor_Function_flag = true;
  while (Gravity_sensor_Function_flag) {
    if (Serial.available())
    {
      bluetooth_val = Serial.read();
      Serial.println(bluetooth_val);
      if (bluetooth_val == 'F') {
        Move_Forward(speed_car);

      } else if (bluetooth_val == 'B') {
        Move_Backward(speed_car);
      } else if (bluetooth_val == 'L') {
        Rotate_Left(speed_car);
      } else if (bluetooth_val == 'R') {
        Rotate_Right(speed_car);
      } else if (bluetooth_val == 'p') {
        Stop();
      } else if (bluetooth_val == 'X') {
        speed_car = low_speed;
      } else if (bluetooth_val == 'Y') {
        speed_car = medium_speed;
      } else if (bluetooth_val == 'Z') {
        speed_car = high_speed;
      }
      if (bluetooth_val == 'S') {
        Gravity_sensor_Function_flag = false;
        Stop();

      }
    }
  }
}

void setup(){
  IRremote ir(3);

  Serial.begin(9600);
  BLE_val = "";
  base_degrees = 90;
  arm_degrees = 90;
  claw_degrees = 90;
  forward_flag = false;
  backward_flag = false;
  left_flag = false;
  right_flag = false;
  claw_close_flag = false;
  claw_open_flag = false;
  arm_forward_flag = false;
  claw_recracted_flag = false;
  base_anticlockwise_flag = false;
  base_clockwise_flag = false;
  menory_action_flag = false;
  Avoidance_Function_flag = false;
  Following_Function_flag = false;
  Anti_drop_Function_flag = false;
  Line_tracking_Function_flag = false;
  Gravity_sensor_Function_flag = false;
  Left_Tra_Value = 1;
  Center_Tra_Value = 1;
  Right_Tra_Value = 1;
  Black_Line = 1;
  distance = 0;
  actions_count = 0;
  auto_count = 0;
  low_speed = 60;
  medium_speed = 120;
  high_speed = 160;
  speed_car = 60;
  myservo1.attach(9);
  myservo2.attach(10);
  myservo3.attach(11);
  myservo1.write(claw_degrees);
  delay(500);
  myservo2.write(arm_degrees);
  delay(500);
  myservo3.write(base_degrees);
  delay(500);
  Stop();
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  pinMode(A1, INPUT);
  pinMode(12, OUTPUT);
  pinMode(13, INPUT);
  Serial.begin(9600);
}

void loop(){
  while (Serial.available() > 0) {
    BLE_val = BLE_val + ((char)(Serial.read()));
    delay(2);
  }
  if (0 < String(BLE_val).length() && 2 >= String(BLE_val).length()) {
    Serial.println(String(BLE_val).length());
    Serial.println(BLE_val);
    switch (String(BLE_val).charAt(0)) {
     case 'o':
      claw_open();
      break;
     case 'c':
      claw_close();
      break;
     case 'u':
      arm_up();
      break;
     case 'd':
      arm_down();
      break;
     case 'l':
      arm_base_anticlockwise();
      break;
     case 'r':
      arm_base_clockwise();
      break;
     case 'F':
      Move_forward_Function();
      break;
     case 'B':
      Move_backward_Function();
      break;
     case 'L':
      Turn_left_Function();
      break;
     case 'R':
      Turn_right_Function();
      break;
     case 'S':
      Stop();
      break;
     case 'm':
      Serial.println(actions_count);
      read_degress();
      break;
     case 'a':
      auto_do();
      break;
     case 'X':
      speed_car = low_speed;
      break;
     case 'Y':
      speed_car = medium_speed;
      break;
     case 'Z':
      speed_car = high_speed;
      break;
     case 'A':
      Avoidance_Function();
      break;
     case 'D':
      Anti_drop_Function();
      break;
     case 'W':
      Following_Function();
      break;
     case 'T':
      Line_tracking_Function();
      break;
     case 'G':
      Gravity_sensor_Function();
      break;
    }
    BLE_val = "";

  } else {
    BLE_val = "";

  }
  IR_control_Function();

}


Here are the sketches


#include "IR_remote.h"
#include "Keymap.h"
// Provides ISR
#ifndef __AVR_ATmega32U4__
#include <avr/interrupt.h>

volatile irparams_t irparams;
bool MATCH(uint8_t measured_ticks, uint8_t desired_us)
{
  // Serial.print(measured_ticks);Serial.print(",");Serial.println(desired_us);
  return(measured_ticks >= desired_us - (desired_us>>2)-1 && measured_ticks <= desired_us + (desired_us>>2)+1);
}

ISR(TIMER_INTR_NAME)
{
  // Serial.println("ISR");
   //Serial.println(millis());
  uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin);
  // uint32_t new_time = micros();
  // uint8_t timer = (new_time - irparams.lastTime)>>6;
  irparams.timer++; // One more 50us tick
  if (irparams.rawlen >= RAWBUF)
  {
    // Buffer overflow
    irparams.rcvstate = STATE_STOP;
  }
  switch(irparams.rcvstate)
  {
    case STATE_IDLE: // In the middle of a gap
      if (irdata == MARK)
      {
        irparams.rawlen = 0;
        irparams.timer = 0;
        irparams.rcvstate = STATE_MARK;
      }
      break;
    case STATE_MARK: // timing MARK
      if (irdata == SPACE)
      {
        // MARK ended, record time
        irparams.rawbuf[irparams.rawlen++] = irparams.timer;
        irparams.timer = 0;
        irparams.rcvstate = STATE_SPACE;
      }
      break;
    case STATE_SPACE: // timing SPACE
      if (irdata == MARK)
      {
        // SPACE just ended, record it
        irparams.rawbuf[irparams.rawlen++] = irparams.timer;
        irparams.timer = 0;
        irparams.rcvstate = STATE_MARK;
      }
      else
      { // SPACE
        if (irparams.timer > GAP_TICKS)
        {
          // big SPACE, indicates gap between codes
          // Mark current code as ready for processing
          // Switch to STOP
          // Don't reset timer; keep counting space width
          irparams.rcvstate = STATE_STOP;
          irparams.lastTime = millis();
        } 
      }
      break;
    case STATE_STOP: // waiting, measuring gap
      if(millis() - irparams.lastTime > 120)
      {
        irparams.rawlen = 0;
        irparams.timer = 0;
        irparams.rcvstate = STATE_IDLE;
      }
      else if (irdata == MARK)
      { 
        // reset gap timer
        irparams.timer = 0;
      }
      break;
  }
  // irparams.lastTime = new_time;
}

/**
 * Alternate Constructor which can call your own function to map the IR to arduino port,
 * no pins are used or initialized here.
 * \param[in]
 *   None
 */
IRremote::IRremote(int pin)
{
  pinMode(pin,INPUT);
  irparams.recvpin = pin;
  // attachInterrupt(INT0, irISR, CHANGE);
  
  irDelayTime = 0;
  irIndex = 0;
  irRead = 0;
  irReady = false;
  irBuffer = "";
  irPressed = false;
  begin();
}

/**
 * \par Function
 *    begin
 * \par Description
 *    Initialize interrupt.
 * \param[in]
 *    None
 * \par Output
 *    None
 * \par Return
 *    None
 * \par Others
 *    None
 */
void IRremote::begin()
{
  cli();
  // setup pulse clock timer interrupt
  //Prescale /8 (16M/8 = 0.5 microseconds per tick)
  // Therefore, the timer interval can range from 0.5 to 128 microseconds
  // depending on the reset value (255 to 0)
  TIMER_CONFIG_NORMAL();

  //Timer2 Overflow Interrupt Enable
  TIMER_ENABLE_INTR;

  // TIMER_RESET;

  sei();  // enable interrupts

  // initialize state machine variables
  irparams.rawlen = 0;
  irparams.rcvstate = STATE_IDLE;
}

/**
 * \par Function
 *    end
 * \par Description
 *    Close the interrupt.
 * \param[in]
 *    None
 * \par Output
 *    None
 * \par Return
 *    None
 * \par Others
 *    None
 */
void IRremote::end()
{
  EIMSK &= ~(1 << INT0);
}

/**
 * \par Function
 *    decode
 * \par Description
 *    Decodes the received IR message.
 * \param[in]
 *    None
 * \par Output
 *    None
 * \par Return
 *    Returns 0 if no data ready, 1 if data ready.
 * \par Others
 *    Results of decoding are stored in results.
 */
ErrorStatus IRremote::decode()
{
  rawbuf = irparams.rawbuf;
  rawlen = irparams.rawlen;
  if (irparams.rcvstate != STATE_STOP)
  {
     return ERROR;
  }

  if (decodeNEC())
  {
    begin();
    return SUCCESS;
  }
  begin();
  return ERROR;
}

/**
 * \par Function
 *    decodeNEC
 * \par Description
 *    Decodes NEC the received IR message.
 * \param[in]
 *    None
 * \par Output
 *    None
 * \par Return
 *    Returns ERROR if decode NEC no done, SUCCESS if decode NEC done.
 * \par Others
 *    Results of decode NEC.
 */
// NECs have a repeat only 4 items long
ErrorStatus IRremote::decodeNEC()
{
  static unsigned long repeat_value = 0xFFFFFFFF;
  static byte repeta_time = 0;
  uint32_t data = 0;
  int offset = 0; // Skip first space
  // Initial mark
  if (!MATCH(rawbuf[offset], NEC_HDR_MARK/50)) 
  {
    return ERROR;
  }
  offset++;
  // Check for repeat
  if (rawlen == 3 &&
    MATCH(rawbuf[offset], NEC_RPT_SPACE/50) &&
    MATCH(rawbuf[offset+1], NEC_BIT_MARK/50)) 
  {  
     rawbuf[offset] = 0;
     rawbuf[offset+1] = 0;
     repeta_time++;
    // if(repeta_time > 1) {
        repeta_time = 0;
        bits = 0;
        value = repeat_value;
       // Serial.println("REPEAT");
        decode_type = NEC;
        return SUCCESS;
   //  }
  }
  if (rawlen < (2 * NEC_BITS + 3)) 
  {
    return ERROR;
  }
  // Initial space  
  if (!MATCH(rawbuf[offset], NEC_HDR_SPACE/50)) 
  {
    return ERROR;
  }
  rawbuf[offset] = 0;
  offset++;
  for (int i = 0; i < NEC_BITS; i++)
  {
    if (!MATCH(rawbuf[offset], NEC_BIT_MARK/50)) 
    {
      return ERROR;
    }
    rawbuf[offset] = 0;
    offset++;
    if (MATCH(rawbuf[offset], NEC_ONE_SPACE/50))
    {
      //data = (data << 1) | 1;
      data = (data >> 1) | 0x80000000;
    } 
    else if (MATCH(rawbuf[offset], NEC_ZERO_SPACE/50))
    {
      //data <<= 1;
      data >>= 1;
    } 
    else 
    {
      return ERROR;
    }
    offset++;
  }
  // Success
  bits = NEC_BITS;
  value = data;
  repeat_value = data;
  decode_type = NEC;
  repeta_time = 0;
  return SUCCESS;
}

/**
 * \par Function
 *    mark
 * \par Description
 *    Sends an IR mark for the specified number of microseconds.
 * \param[in]
 *    us - THe time of a PWM.
 * \par Output
 *    None
 * \par Return
 *    None
 * \par Others
 *    None
 */
void IRremote::mark(uint16_t us)
{
  // Sends an IR mark for the specified number of microseconds.
  // The mark output is modulated at the PWM frequency.
  TIMER_ENABLE_PWM; // Enable pin 3 PWM output
  delayMicroseconds(us);
}

/**
 * \par Function
 *    space
 * \par Description
 *    Sends an IR mark for the specified number of microseconds.
 * \param[in]
 *    us - THe time of a PWM.
 * \par Output
 *    None
 * \par Return
 *    None
 * \par Others
 *    None
 */
/* Leave pin off for time (given in microseconds) */
void IRremote::space(uint16_t us)
{
  // Sends an IR space for the specified number of microseconds.
  // A space is no output, so the PWM output is disabled.
  TIMER_DISABLE_PWM; // Disable pin 3 PWM output
  delayMicroseconds(us);
}

/**
 * \par Function
 *    enableIROut
 * \par Description
 *    Enable an IR for the specified number of khz.
 * \param[in]
 *    us - THe time of a INTR.
 * \par Output
 *    None
 * \par Return
 *    None
 * \par Others
 *    None
 */
void IRremote::enableIROut(uint8_t khz)
{
  TIMER_DISABLE_INTR; //Timer2 disable Interrupt
  TIMER_CONFIG_KHZ(khz);
}

/**
 * \par Function
 *    enableIRIn
 * \par Description
 *    Enable an IR to write in.
 * \param[in]
 *    None
 * \par Output
 *    None
 * \par Return
 *    None
 * \par Others
 *    None
 */
// initialization
void IRremote::enableIRIn() {
  cli();
  // setup pulse clock timer interrupt
  //Prescale /8 (16M/8 = 0.5 microseconds per tick)
  // Therefore, the timer interval can range from 0.5 to 128 microseconds
  // depending on the reset value (255 to 0)
  TIMER_CONFIG_NORMAL();

  //Timer2 Overflow Interrupt Enable
  TIMER_ENABLE_INTR;

  //TIMER_RESET;

  sei();  // enable interrupts

  // initialize state machine variables
  irparams.rcvstate = STATE_IDLE;
  irparams.rawlen = 0;

  // set pin modes
  pinMode(irparams.recvpin, INPUT);
}

/**
 * \par Function
 *    sendRaw
 * \par Description
 *    Send the length of data with hz.
 * \param[in]
 *    buf[] - The data's buffer.
  * \param[in]
 *    len - The data's length.
  * \param[in]
 *    hz - The hz for sending data.
 * \par Output
 *    None
 * \par Return
 *    None
 * \par Others
 *    None
 */
void IRremote::sendRaw(unsigned int buf[], int len, uint8_t hz)
{
  enableIROut(hz);
  for (int i = 0; i < len; i++)
  {
    if (i & 1)
    {
      space(buf[i]);
    } 
    else
    {
      mark(buf[i]);
    }
  }
  space(0); // Just to be sure
}

/**
 * \par Function
 *    getString
 * \par Description
 *    Get string in a INTR.
 * \param[in]
 *    None
 * \par Output
 *    None
 * \par Return
 *    Return the result in a IRQ.
 * \par Others
 *    None
 */
String IRremote::getString()
{
  if(decode())
  {
    irRead = ((value >> 8) >> 8) & 0xff;
    if(irRead == 0xa || irRead == 0xd)
    {
      irIndex = 0;
      irReady = true;
    }
    else
    {
      irBuffer += irRead; 
      irIndex++;
    }
    irDelayTime = millis();
  }
  else
  {
    if(irRead > 0)
    {
      if(millis() - irDelayTime > 100)
      {
        irPressed = false;
        irRead = 0;
        irDelayTime = millis();
        Pre_Str = "";
      }
    }
  }
  if(irReady)
  {
    irReady = false;
    String s = String(irBuffer);
    Pre_Str = s;
    irBuffer = "";
    return s;
  }
  return Pre_Str;
}

/**
 * \par Function
 *    getCode
 * \par Description
 *    Get the reading code.
 * \param[in]
 *    None
 * \par Output
 *    None
 * \par Return
 *    Return the result of reading.
 * \par Others
 *    None
 */
unsigned char IRremote::getCode()
{
  irIndex = 0;
  loop();
  return irRead;
}
String IRremote::getKeyMap(byte keycode, byte ir_type)
{
   byte i;
   ST_KEY_MAP *irkeymap = normal_ir_keymap;
   if (ir_type == IR_TYPE_EM) irkeymap = em_ir_keymap;
   for (i = 0; i < KEY_MAX; i++) {
        if (irkeymap[i].keycode == keycode)
        return irkeymap[i].keyname;
   }
   return "";
}

byte IRremote::getIrKey(byte keycode, byte ir_type)
{
    byte i;
    ST_KEY_MAP *irkeymap = normal_ir_keymap;
    if (ir_type == IR_TYPE_EM) irkeymap = em_ir_keymap;
    for (i = 0; i < KEY_MAX; i++) {
        if (irkeymap[i].keycode == keycode)
        return i;
    }
   return 0xFF;
}
/**
 * \par Function
 *    sendString
 * \par Description
 *    Send data.
 * \param[in]
 *    s - The string you want to send.
 * \par Output
 *    None
 * \par Return
 *    None
 * \par Others
 *    None
 */
void IRremote::sendString(String s)
{
  unsigned long l;
  uint8_t data;
  s.concat('\n');
  for(int i = 0;i < s.length();i++)
  {
    data = s.charAt(i);
    l = 0x0000ffff & (uint8_t)(~data);
    l = l << 8;
    l = l + ((uint8_t)data);
    l = l << 16;
    l = l | 0x000000ff;
    sendNEC(l,32);
    delay(20);
  }
  enableIRIn();
}

/**
 * \par Function
 *    sendString
 * \par Description
 *    Send data.
 * \param[in]
 *    v - The string you want to send.
 * \par Output
 *    None
 * \par Return
 *    None
 * \par Others
 *    None
 */
void IRremote::sendString(float v)
{
  dtostrf(v,5, 8, floatString);
  sendString(floatString);
}

/**
 * \par Function
 *    sendNEC
 * \par Description
 *    Send NEC.
 * \param[in]
 *    data - The data you want to send.
  * \param[in]
 *    nbits - The data bit you want to send.
 * \par Output
 *    None
 * \par Return
 *    None
 * \par Others
 *    None
 */
void IRremote::sendNEC(unsigned long data, int nbits)
{
  
  enableIROut(38);
  mark(NEC_HDR_MARK);
  space(NEC_HDR_SPACE);
  for (int i = 0; i < nbits; i++)
  {
    if (data & 1) 
    {
      mark(NEC_BIT_MARK);
      space(NEC_ONE_SPACE);
    } 
    else 
    {
      mark(NEC_BIT_MARK);
      space(NEC_ZERO_SPACE);
    }
    data >>= 1;
  }
  mark(NEC_BIT_MARK);
  space(0);
}

/**
 * \par Function
 *    loop
 * \par Description
 *    A circle of operation.
 * \param[in]
 *    None
 * \par Output0
 *    None
 * \par Return
 *    None
 * \par Others
 *    None
 */
void IRremote::loop()
{
  if(decode())
  {
    irRead = ((value >> 8) >> 8) & 0xff;
    irPressed = true;
    if(irRead == 0xa || irRead == 0xd)
    {
      irIndex = 0;
      irReady = true;
    }
    else
    {
      irBuffer += irRead; 
      irIndex++;
      if(irIndex > 64)
      {
        irIndex = 0;
        irBuffer = "";
      }
    }
    irDelayTime =  millis();
  }
  else
  {
    if(irRead > 0)
    { 
     // Serial.println(millis() - irDelayTime);
      if(millis() - irDelayTime > 0)
      {
        irPressed = false;
        irRead = 0;
        irDelayTime = millis();
      }
    }
  }
 // Serial.println(irRead, HEX);
}

/**
 * \par Function
 *    keyPressed
 * \par Description
 *    Press key.
 * \param[in]
 *    None
 * \par Output
 *    None
 * \par Return
 *    Return you the pressed key or not.
 * \par Others
 *    None
 */
boolean IRremote::keyPressed(unsigned char r)
{
  
  irIndex = 0;
  loop();
  return irRead == r;
}
#endif // !defined(__AVR_ATmega32U4__)

#ifndef IRremote_h
#define IRremote_h

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>
#include <Arduino.h>

#ifdef ME_PORT_DEFINED
#endif // ME_PORT_DEFINED
#ifndef __AVR_ATmega32U4__
#define MARK  0
#define SPACE 1
#define NEC_BITS 32
#define USECPERTICK 50  // microseconds per clock interrupt tick
#define RAWBUF 80 // Length of raw duration buffer

typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus;

#define NEC_HDR_MARK	9000
#define NEC_HDR_SPACE	4500
#define NEC_BIT_MARK	560
#define NEC_ONE_SPACE	1600
#define NEC_ZERO_SPACE	560
#define NEC_RPT_SPACE	2250
#define NEC_RPT_PERIOD	110000


#define _GAP 5000 // Minimum map between transmissions

// receiver states
#define STATE_IDLE     2
#define STATE_MARK     3
#define STATE_SPACE    4
#define STATE_STOP     5


// Values for decode_type
#define NEC 1
#define SONY 2
#define RC5 3
#define RC6 4
#define DISH 5
#define SHARP 6
#define PANASONIC 7
#define JVC 8
#define SANYO 9
#define MITSUBISHI 10
#define SAMSUNG 11
#define LG 12
#define UNKNOWN -1

#define TOPBIT 0x80000000


#ifdef F_CPU
#define SYSCLOCK F_CPU     // main Arduino clock
#else
#define SYSCLOCK 16000000  // main Arduino clock
#endif


#define _GAP 5000 // Minimum map between transmissions
#define GAP_TICKS (_GAP/USECPERTICK)


#define TIMER_DISABLE_INTR   (TIMSK2 = 0)
#define TIMER_ENABLE_PWM     (TCCR2A |= _BV(COM2B1))
#define TIMER_DISABLE_PWM    (TCCR2A &= ~(_BV(COM2B1)))
#define TIMER_ENABLE_INTR    (TIMSK2 = _BV(OCIE2A))
#define TIMER_DISABLE_INTR   (TIMSK2 = 0)
#define TIMER_INTR_NAME      TIMER2_COMPA_vect
#define TIMER_CONFIG_KHZ(val) ({ \
  const uint8_t pwmval = F_CPU / 2000 / (val); \
  TCCR2A = _BV(WGM20); \
  TCCR2B = _BV(WGM22) | _BV(CS20); \
  OCR2A = pwmval; \
  OCR2B = pwmval / 3; \
})

#define TIMER_COUNT_TOP      (SYSCLOCK * USECPERTICK / 1000000)
#if (TIMER_COUNT_TOP < 256)
#define TIMER_CONFIG_NORMAL() ({ \
  TCCR2A = _BV(WGM21); \
  TCCR2B = _BV(CS20); \
  OCR2A = TIMER_COUNT_TOP; \
  TCNT2 = 0; \
})
#else
#define TIMER_CONFIG_NORMAL() ({ \
  TCCR2A = _BV(WGM21); \
  TCCR2B = _BV(CS21); \
  OCR2A = TIMER_COUNT_TOP / 8; \
  TCNT2 = 0; \
})
#endif

// information for the interrupt handler
typedef struct {
  uint8_t recvpin;           // pin for IR data from detector
  volatile uint8_t rcvstate;          // state machine
  volatile uint32_t lastTime;
  unsigned int timer;     // 
  volatile uint8_t rawbuf[RAWBUF]; // raw data
  volatile uint8_t rawlen;         // counter of entries in rawbuf
} irparams_t;


class IRremote
{
public:

  IRremote(int pin);
  ErrorStatus decode();
  void begin();
  void end();
  void loop();
  boolean keyPressed(unsigned char r);
  // void resume();
   
  int8_t decode_type; // NEC, SONY, RC5, UNKNOWN
  unsigned long value; // Decoded value
  uint8_t bits; // Number of bits in decoded value
  volatile uint8_t *rawbuf; // Raw intervals in .5 us ticks
  int rawlen; // Number of records in rawbuf.
  String getString();
  unsigned char getCode();
  String getKeyMap(byte keycode, byte ir_type = 1);
  byte getIrKey(byte keycode,    byte ir_type = 1);
  void sendString(String s);
  void sendString(float v);
  void sendNEC(unsigned long data, int nbits);
  void sendRaw(unsigned int buf[], int len, uint8_t hz);
  void enableIROut(uint8_t khz);
  void enableIRIn();
  void mark(uint16_t us);
  void space(uint16_t us);
private:
  ErrorStatus decodeNEC();
  int16_t irIndex;	
  char irRead;
  char floatString[5];
  boolean irReady;
  boolean irPressed;
  String irBuffer;
  String Pre_Str;
  double irDelayTime;
};
#endif // !__AVR_ATmega32U4__
#endif
#include "Keymap.h"

// ST_KEY_MAP normal_ir_keymap[18] = {
    // {"1", 0x45},
    // {"2", 0x46},
    // {"3", 0x47},
    // {"4", 0x44},
    // {"5", 0x40},
    // {"6", 0x43},
    // {"7", 0x07},
    // {"8", 0x15},
    // {"9", 0x09},
    // {"0", 0x19},
    // {"*", 0x16},
    // {"#", 0x0D},
    // {"up", 0x18},
    // {"down", 0x52},
    // {"ok", 0x1C},
    // {"left", 0x08},
    // {"right", 0x5A}
// };//不带logh方向盘在下方



// ST_KEY_MAP normal_ir_keymap[18] = {
    // {"up", 0x46},
    // {"left", 0x44},
    // {"ok", 0x40},
    // {"right", 0x43},
    // {"down", 0x15},
    // {"1", 0x16},
    // {"2", 0x19},
    // {"3", 0x0d},
    // {"4", 0x0c},
    // {"5", 0x18},
    // {"6", 0x5e},
    // {"7", 0x08},
    // {"8", 0x1c},
    // {"9", 0x5A},
    // {"*", 0x42},
    // {"0", 0x52},
    // {"#", 0x4A}
// };

ST_KEY_MAP normal_ir_keymap[18] = {
	{"1", 0x16},
    {"2", 0x19},
    {"3", 0x0d},
    {"4", 0x0c},
    {"5", 0x18},
    {"6", 0x5e},
    {"7", 0x08},
    {"8", 0x1c},
    {"9", 0x5A},
	{"0", 0x52},
    {"*", 0x42},
    {"#", 0x4A},
	{"up", 0x46},
	{"down", 0x15},
	{"ok", 0x40},
    {"left", 0x44},
	{"right", 0x43},
};//带logo LA

// ST_KEY_MAP em_ir_keymap[21] = {
    // {"A", 0x45},
    // {"B", 0x46},
    // {"C", 0x47},
    // {"D", 0x44},
    // {"up", 0x40},
    // {"+", 0x43},
    // {"left", 0x07},
    // {"ok", 0x15},
    // {"right", 0x09},
    // {"0", 0x16},
    // {"down", 0x19},
    // {"-", 0x0d},
    // {"1", 0x0c},
    // {"2", 0x18},
    // {"3", 0x5e},
    // {"4", 0x08},
    // {"5", 0x1c},
    // {"6", 0x5A},
    // {"7", 0x42},
    // {"8", 0x52},
    // {"9", 0x4A}
// };
#ifndef _KEYMAY_H_
#define _KEYMAY_H_
#include <Arduino.h>
#define KEY_MAX 21
typedef struct
{
    String keyname;
    byte keycode;
}ST_KEY_MAP;

#define IR_TYPE_NORMAL 1
#define IR_TYPE_EM 2

typedef enum {
    IR_KEYCODE_1 = 0,
    IR_KEYCODE_2,
    IR_KEYCODE_3,
    IR_KEYCODE_4,
    IR_KEYCODE_5,
    IR_KEYCODE_6,
    IR_KEYCODE_7,
    IR_KEYCODE_8,
    IR_KEYCODE_9,
    IR_KEYCODE_0,
    IR_KEYCODE_STAR,      // *
    IR_KEYCODE_POUND,     // # 
    IR_KEYCODE_UP,
    IR_KEYCODE_DOWN,
    IR_KEYCODE_OK,
    IR_KEYCODE_LEFT,
    IR_KEYCODE_RIGHT,
}E_NORMAL_IR_KEYCODE;

typedef enum {
    EM_IR_KEYCODE_A = 0,
    EM_IR_KEYCODE_B,
    EM_IR_KEYCODE_C,
    EM_IR_KEYCODE_D,
    EM_IR_KEYCODE_UP,
    EM_IR_KEYCODE_PLUS,
    EM_IR_KEYCODE_LEFT,
    EM_IR_KEYCODE_OK,
    EM_IR_KEYCODE_RIGHT,
    EM_IR_KEYCODE_0,
    EM_IR_KEYCODE_DOWN,      
    EM_IR_KEYCODE_REDUCE,     
    EM_IR_KEYCODE_1,
    EM_IR_KEYCODE_2,
    EM_IR_KEYCODE_3,
    EM_IR_KEYCODE_4,
    EM_IR_KEYCODE_5,
    EM_IR_KEYCODE_6,
    EM_IR_KEYCODE_7,
    EM_IR_KEYCODE_8,
	EM_IR_KEYCODE_9,
}E_EM_IR_KEYCODE;

extern ST_KEY_MAP normal_ir_keymap[];
extern ST_KEY_MAP em_ir_keymap[];
#endif  /* _KEYMAY_H_ */

Add a description wich code-section is what.
Are these four to six different Code-versions or something else

Add a link to a description of this robot car

1.Sorry, but we can't add descriptions, we just copied the lessons from the link below, and our deadline for our project is near. We are assigned for a research congress, yet we are only students which we have no knowledge of coding.

  1. Yes, but compiled into one code.

  2. Dropbox - LAFVIN 4WD Robot Arm Smart Car V2.2 - Simplify your life

That is a real bad combination.
So in my opinion your have this choices:

experiencing beeing unable to answer questions about the code at the research congress
and
if something does not work beeing unable to repair it

or

having a very intensive time in learning programming until this congress
and after having learned beeing able to explain and to repair.

Saying "no" to travelling to this congress this year

Having an intensive year of learning coding and a successfull perfomance on such a congress next year.

Me personal I will not contribute to such an attitude as you presented it.

Yes, it is.

@diceyof I think it's time for a chat with your supervisor/professor about how to proceed. This problem is not a technical one, but primarily a didactic one. If you're going to a congress to present a project you're evidently not familiar with, something's not going quite as it should.

1 Like

@diceyof - break your programs into smaller tasks to test each device. Similar to this:

Did you follow exactly the company tutorial for this kit? Did you correctly assemble the kit?

To me it is not clear what the problem is...
Does the code work?
If so: do not improve it... especially if you need to perform in two weeks and are not familiar with the code...
Hope it is a student conference with lots of other students that hacked things together.

what's the congress about?

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