How to use Ultrasonic sensor with IR control

I'm on a project that need to use the ultrassonico sensor and IR sensor. When I turn my robot must operate using the ultrassonic sensor.
But I can also choose to use the remote, so it will work with IR. However, by submitting the code, the ultrasonic no longer works ...

Thank you.

Can you rephrase your question ?

What does this mean ?

and when selecting the function of active IR operate by moving the robot by remote control.

juniormoura:
However, by submitting the code, the ultrasonic no longer works ...

Show your sketch.

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

float num = 0;

Servo servo1;
Servo servo2;
Servo servo3;

int cm;
int st;
int trig = 6;
int echo = 7;
long microseconds;

/* =================== Declaração do IR ===================*/

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
float armazenavalor;

/* ========================== // ==========================*/

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  st=2;
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  servo1.attach(8 ) ;
  servo2.attach(9);
  servo3.attach(10);
  servo1.write(110);
  servo2.write(70); 
  servo3.write(100);
  delay(5000);

}
void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);

    armazenavalor = (results.value); 
    if (armazenavalor == NULL) 
    { 
      sensor();
    }
    if (armazenavalor == 0xFFA25D) 
    { 
      controle();

    }
  }
}

long microsecondsToCentimeters(long microseconds) { 
  return microseconds / 29 / 2;
}

void sensor(){
  long duration, cm;
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  duration = pulseIn(echo, HIGH);
  cm = microsecondsToCentimeters(duration);
  if (cm > 30 and st==1){
    alinha();
  }
  if (cm <= 30 and st==1){
    Sonar2();
  }
  if (cm <= 30 and st==2){
    pare();
  }
  if (cm > 30 and st==2){
    frente();
  }
  if (cm > 30 and st==3){
    frente();
  }
  if (cm <= 30 and st==3){
    alinha2();
  }
}

void controle(){
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);

    armazenavalor = (results.value); 
    if (armazenavalor == 0xFF22DD) //Verifica se a tecla 1 foi acionada 
    { 
      pare();
    }
    if (armazenavalor == 0xFF02FD) //Verifica se a tecla 1 foi acionada 
    { 
      alinha();
    }
    if (armazenavalor == 0xFFC23D) //Verifica se a tecla 1 foi acionada 
    { 
      frente();
    }
    if (armazenavalor == 0xFF9867) //Verifica se a tecla 1 foi acionada 
    { 
      sensor();
    }
    irrecv.resume(); // Receive the next value
  }
}

void frente() {
  servo1.write(110);
  servo2.write (70);
  servo3.write (100);
  delay(15); 
}
void pare() {
  servo2.write(94); //enviando um ângulo de 70 graus os servos param
  servo3.write(94);
  st = 1;
  delay(200);
  Sonar();
}
void Sonar() {
  servo1.write(0);
  delay(2000); 
}
void Sonar2() {
  servo1.write(180);
  st=3;
  delay(2000);
}
void alinha() { //alinhar o sonar com e motores
  servo1.write(110);
  for(int i=0;i<=10;i++) {
    servo2.writeMicroseconds (1000);
    servo3.writeMicroseconds (1000);
    st = 2;
    delay(30);
  }
}

void alinha2() {
  servo1.write(110);
  for(int i=0;i<=10;i++) {
    servo2.writeMicroseconds (2000);
    servo3.writeMicroseconds (2000);
    st = 2;
    delay(30);
  }
}

Thank you.

Post a link for the IR sensor. We need to see which one you are using.

I'm using this model:
http://www.aliexpress.com/store/product/1838t-infrared-universal-receiver-infrared-sensor-receiver-tube-22-25-meters/413154_1462684324.html

Your sketch does not compile:

sketch_aug10a.ino: In function 'void loop()':
sketch_aug10a:54: error: 'controleIR' was not declared in this scope

Sorry controleIR the function does not exist.
The correct name is the function control ().
I changed my sketch, but still does not work.

Thank you.

Click the MODIFY button on the upper right of the window,
Highlight only the code,
Click the "#" CODE TAGS button,
Click the SAVE button on the lower left ,

In the future please use the "#" CODE TAGS toolbutton when you post code.

I think your main problem is in loop():

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);

    armazenavalor = (results.value); 
    if (armazenavalor == NULL) 
    { 
      sensor();
    }
    if (armazenavalor == 0xFFA25D) 
    { 
      controleIR();
    }
  }
}

The way it is written, it will do NOTHING unless an IR message has been received. This will prevent the use of "sensor()".

What you probably want to do is use an IR Remote button to switch to IR mode and a different one to switch to Sonar mode:

void loop() {
  static boolean IRMode = true;  // Start in IR Remote mode

  if (irrecv.decode(&results)) {
    armazenavalor = results.value;
    Serial.println(armazenavalor, HEX);
    if (armazenavalor == SONAR_BUTTON)
        IRMode = false;
    else
    if (armazenavalor == IR_BUTTON)
        IRMode = true;
    else
    if (IRMode)
      controleIR();
   }

   if (!IRMode) { 
      sensor();
    }
}

Hello, the ultrassonic sensor worked, but is getting lost, stopping all the time.
And the infrared sensor does not trigger.
I followed his example, and tried changing, but still does not work as expected.
What might be happening?
Does the control() function is wrong?
Follows the code with the changes.

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

float num = 0;

Servo servo1;
Servo servo2;
Servo servo3;

int cm;
int st;
int trig = 6;
int echo = 7;
long microseconds;

/* =================== Declaração do IR ===================*/

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
float armazenavalor;

/* ========================== // ==========================*/

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  st=2;
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  servo1.attach(8);
  servo2.attach(9);
  servo3.attach(10);
  servo1.write(110);
  servo2.write(70); 
  servo3.write(100);
  delay(5000);

}

void loop() {

  static boolean IRMode = true;  // Start in IR Remote mode

    if (irrecv.decode(&results)) {
    armazenavalor = results.value;
    Serial.println(armazenavalor, HEX);
    if (armazenavalor == 0xFF9867){
      IRMode = false;
    }
    else if (armazenavalor == 0xFFA25D){
      IRMode = true;
    }
    else if (IRMode == true){
      controle();
    }
  }

  if (!IRMode) { 
    sensor();
  }
}


long microsecondsToCentimeters(long microseconds) { 
  return microseconds / 29 / 2;
}

void sensor(){
  long duration, cm;
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  duration = pulseIn(echo, HIGH);
  cm = microsecondsToCentimeters(duration);
  if (cm > 10 and st==1){
    alinha();
  }
  if (cm <= 10 and st==1){
    Sonar2();
  }
  if (cm <= 10 and st==2){
    pare();
  }
  if (cm > 10 and st==2){
    frente();
  }
  if (cm > 10 and st==3){
    frente();
  }
  if (cm <= 10 and st==3){
    alinha2();
  }
}

void controle(){
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);

    armazenavalor = (results.value); 
    if (armazenavalor == 0xFF22DD) //Verifica se a tecla 1 foi acionada 
    { 
      pare();
    }
    if (armazenavalor == 0xFF02FD) //Verifica se a tecla 1 foi acionada 
    { 
      alinha();
    }
    if (armazenavalor == 0xFFC23D) //Verifica se a tecla 1 foi acionada 
    { 
      frente();
    }
    if (armazenavalor == 0xFF9867) //Verifica se a tecla 1 foi acionada 
    { 
      sensor();
    }
    irrecv.resume(); // Receive the next value
  }
}

void frente() {
  servo1.write(110);
  servo2.write (70);
  servo3.write (100);
  delay(15); 
}
void pare() {
  servo2.write(94); //enviando um ângulo de 70 graus os servos param
  servo3.write(94);
  st = 1;
  delay(200);
  Sonar();
}
void Sonar() {
  servo1.write(0);
  delay(2000); 
}
void Sonar2() {
  servo1.write(180);
  st=3;
  delay(2000);
}
void alinha() { //alinhar o sonar com e motores
  servo1.write(110);
  for(int i=0;i<=10;i++) {
    servo2.writeMicroseconds (1000);
    servo3.writeMicroseconds (1000);
    st = 2;
    delay(30);
  }
}

void alinha2() {
  servo1.write(110);
  for(int i=0;i<=10;i++) {
    servo2.writeMicroseconds (2000);
    servo3.writeMicroseconds (2000);
    st = 2;
    delay(30);
  }
}

Thank you.

controle() is incorrect. It checks to see if ANOTHER IR message has arrived and it only gets called when a message has arrived.

Remove this part:

  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);

    armazenavalor = (results.value);

and remove the matching close-bracket. Use the value that loop() saved in armazenavalor.

You can also take out this part because it is already done in loop():

    if (armazenavalor == 0xFF9867) //Verifica se a tecla 1 foi acionada 
    { 
      sensor();
    }

I made the changes you suggested, but the infrared did not work.

See the code:

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

float num = 0;

Servo servo1;
Servo servo2;
Servo servo3;

int cm;
int st;
int trig = 6;
int echo = 7;
long microseconds;

/* =================== Declaração do IR ===================*/

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
long armazenavalor;

/* ========================== // ==========================*/

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  st=2;
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  servo1.attach(8);
  servo2.attach(9);
  servo3.attach(10);
  servo1.write(110);
  servo2.write(70); 
  servo3.write(100);
  delay(5000);

}

void loop() {

  static boolean IRMode = true;  // Start in IR Remote mode

    sensor();

  if (irrecv.decode(&results)) {
    armazenavalor = results.value;
    Serial.println(armazenavalor, HEX);
    if (armazenavalor == 0xFF9867){
      IRMode = false;
      //sensor();
    }
    if (armazenavalor == 0xFFA25D){
      IRMode = true;
      //controle();
    }
    if (IRMode == true){
      controle();
    }
    if (!IRMode) { 
      sensor();
    }
  }
}


long microsecondsToCentimeters(long microseconds) { 
  return microseconds / 29 / 2;
}

void sensor(){
  long duration, cm;
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  duration = pulseIn(echo, HIGH);
  cm = microsecondsToCentimeters(duration);
  if (cm > 10 and st==1){
    alinha();
  }
  if (cm <= 10 and st==1){
    Sonar2();
  }
  if (cm <= 10 and st==2){
    pare();
  }
  if (cm > 10 and st==2){
    frente();
  }
  if (cm > 10 and st==3){
    frente();
  }
  if (cm <= 10 and st==3){
    alinha2();
  }
}

void controle(){

  if (armazenavalor == 0xFF22DD) //Verifica se a tecla 1 foi acionada 
  { 
    pare2();
  }
  if (armazenavalor == 0xFF02FD) //Verifica se a tecla 1 foi acionada 
  { 
    alinha();
  }
  if (armazenavalor == 0xFFC23D) //Verifica se a tecla 1 foi acionada 
  { 
    frente();
  }

}

void frente() {
  servo1.write(110);
  servo2.write (70);
  servo3.write (100);
  delay(15); 
}

void pare2() {
  servo2.write(94); //enviando um ângulo de 70 graus os servos param
  servo3.write(94);
  st = 1;
  delay(200);
  //Sonar();
}

void pare() {
  servo2.write(94); //enviando um ângulo de 70 graus os servos param
  servo3.write(94);
  st = 1;
  delay(200);
  Sonar();
}
void Sonar() {
  servo1.write(0);
  delay(2000); 
}
void Sonar2() {
  servo1.write(180);
  st=3;
  delay(2000);
}
void alinha() { //alinhar o sonar com e motores
  servo1.write(110);
  for(int i=0;i<=10;i++) {
    servo2.writeMicroseconds (1000);
    servo3.writeMicroseconds (1000);
    st = 2;
    delay(30);
  }
}

void alinha2() {
  servo1.write(110);
  for(int i=0;i<=10;i++) {
    servo2.writeMicroseconds (2000);
    servo3.writeMicroseconds (2000);
    st = 2;
    delay(30);
  }
}

Thank you.

I think this line has to go back in somewhere:

    irrecv.resume(); // Receive the next value

The principle seems to be working properly, but I can not keep the command.
For example if I run a pause for a few seconds.
If I have a greater delay, the front() function does not work.

I tried the command while and do while and not execute another button.
Is there anything else I can do in this case?

Thank you.

juniormoura:
The principle seems to be working properly, but I can not keep the command.
For example if I run a pause for a few seconds.
If I have a greater delay, the front() function does not work.

I tried the command while and do while and not execute another button.
Is there anything else I can do in this case?

I'm sorry, I don't understand the problem. There is no function "front()". What, exactly, is not working the way you expected it to work and in what way is it different from what you expected. Since your variable names and comments are in a language that I can't understand and many of your functions have few or no comments I can't be of much help. You might want to see if there is a part of the forum for your native language and ask for further help there.

There are boards for: Deutsch, Español, Français, India, Italiano, Nederlands, Portugues, and Scandinavia. If none of those will work for you, you might consider changing your variable names and comments to English and adding comments that explain magic constants like 110, 70, and 94.

That's spanish. You can copy and paste it into Google translate and then copy and paste the translation back into the code.

Sorry, I'm from Brazil.
I will change the name of the variables as you suggested and add comments.
I'll translate with google and what you do not understand you let me know.
This makes it easy for you to help me and understand what I'm thinking of doing in my project.

Thank you.

Hello, in my project I want when I turn on the robot, it moves through the ultrasonic sensor and when you press the power button on the remote control is triggered by movement control, allowing me to pause, advance or rewind.
And when you want to return the operation by ultrasonic sensor, press a button and returns to the sensor function.

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

float num = 0; 

Servo servo1; 
Servo servo2;
Servo servo3;

int cm; 
int st; 
int trig = 6; 
int echo = 7; 
long microseconds; 

/* Declaration of the IR =================== =================== */ 

RECV_PIN int = 11; 
IRrecv irrecv (RECV_PIN); 
decode_results results; 
infraredvalue long; 


/* ========================== // ==================== ====== */ 

void setup() {
   Serial.begin (9600); 
   irrecv.enableIRIn (); // Start the receiver 
   st = 2; 
   pinMode (trig, OUTPUT); 
   pinMode (echo, INPUT); 
   servo1.attach(8); 
   servo2.attach(9); 
   servo3.attach(10); 
   servo1.write(110); 
   servo2.write(70); 
   servo3.write(100); 
   delay (1000); 

} 

void loop() {

    static boolean IRMode = true; // In IR Remote Start mode 
     sensor(); 

   if (irrecv.decode (& results)) {
     infraredvalue = results.value; // Receives the value read by the IR 
     Serial.println (infraredvalue, HEX); 
     if (infraredvalue == 0x1FE708F) {
       IRMode = false; 
       // sensor (); 
     } 
     if (infraredvalue == 0x1FE48B7) {
       IRMode = true; 
       // control (); 
     } 
     if (IRMode == true) {
       control(); 
     } 
     if (!IRMode) {
       sensor(); 
     } 
     irrecv.resume (); // Receive the next value 
   } 
} 


microsecondsToCentimeters long (long microseconds) {
   return microseconds / 29/2; 
} 

void sensor() {
   long duration, cm; 
   digitalWrite (trig, LOW); 
   delayMicroseconds (2); 
   digitalWrite (trig, HIGH); 
   delayMicroseconds (10); 
   digitalWrite (trig, LOW); 
   delayMicroseconds (2); 
   duration = pulseIn (echo, HIGH); 
   cm = microsecondsToCentimeters (duration); 
   if (cm> 10 and st == 1) {// If the distance to the obstacle is greater than 10 cm align the sensor with the servomotors 
     align(); 
   } 
   if (cm <= 10 and st == 1) {// If the distance to meet the condition for servomotor to 180 degrees 
     sonar2(); 
   } 
   if (cm <= 10 and st == 2) {// If the distance to meet the condition servomotors stop 
     stop1(); 
   } 
   if (cm > 10 and st == 2) {// If the distance to meet the condition sermotores advance 
     front(); 
   } 
   if (cm >10 and st == 3) {// If the distance to meet the condition sermotores advance 
     front(); 
   } 
   if (cm <= 10 and st == 3) {// If the distance to the obstacle is greater than 10 cm align the sensor with the servomotors 
     align(); 
   } 
} 

void control () {
   switch (infraredvalue) {
   case 0x1FEA05F: // If the button is pressed 1FEA05F code servomotors stop 
     stop2 (); 
     // break; 
   case 0x1FE609F: // If the button is pressed 1FE609F code sensor with servo motors 
     align (); 
     // break; 
   case 0x1FE20DF: // If the button is pressed the code 1FE20DF sermotores advance 
     front (); 
     // break; 
   case 0x1FE8877: // If the button is pressed 1FE8877 code leaves the control function () and returns the sensor (function) 
     Sensor (); 
     // break; 
   } 
} 

/* BLOCK OF ALTERNATIVE TEST 
void control () {
 
  if (infraredvalue == 0x1FEA05F) // 1FEA05F Checks whether the key has been pressed 
  {
  pare2 (); 
  } 
  if (infraredvalue == 0x1FE609F) // 1FE609F Checks whether the key has been pressed 
  {
  align (); 
  } 
  if (infraredvalue == 0x1FE20DF) // 1FE20DF Checks whether the key has been pressed 
  {
  front (); 
  } 
  // Back to the sensor 
  if (infraredvalue == 0x1FE8877) // 1FE8877 Checks whether the key has been pressed 
  {
  sensor (); 
  } 
  } 
 
  */ 

// OS SERVOMOTORS BEEN MODIFIED TO ROTATE 360 DEGREES 
void forward () {
   servo1.write(110); // sending an angle of 110 degrees to align the sensor with the servomotors 
   servo2.write(70); // sending an angle of 70 degrees which is the point where the forward servo1 
   servo3.write(100); // sending an angle of 100 degrees which is the point where the forward servo2 
   delay (15); 
} 

 void stop2() {
   servo2.write(94); // sending an angle of 94 degrees which is the point where the stop servomotors 
   servo3.write(94); // sending an angle of 94 degrees which is the point where the stop servomotors 
   st = 1; 
   delay (200); 
   // sonar(); 
} 

 void stop1() {
   servo2.write(94); // sending an angle of 94 degrees which is the point where the stop servomotors 
   servo3.write(94); // sending an angle of 94 degrees which is the point where the stop servomotors 
   st = 1; 
   delay (200); 
   Sonar (); 
} 
void sonar() {
   servo1.write (0); // sending an angle of 0 degrees to see if there is any obstacle to the right 
   delay (2000); 
} 

void sonar2 () {
   servo1.write (180); // sending an angle of 0 degrees to see if there is any obstacle to the left 
   st = 3; 
   delay (2000); 
} 

void align() {align // sonar with servomotors 
   servo1.write(110); 
   for (int i = 0; i <= 10; ++ i) {
     servo2.writeMicroseconds (1000); 
     servo3.writeMicroseconds (1000); 
     st = 2; 
     delay (30); 
   } 
} 

/* 
void alinha2() {align // sonar with servomotors 
   servo1.write (110); 
   for (int i = 0; i <= 10; ++ i) {
     servo2.writeMicroseconds (2000); 
     servo3.writeMicroseconds (2000); 
     st = 2; 
     delay (30); 
   } 
} 
*/

I hope you have been better to understand.

Thank you.