Exemple de programme de robot avec l’IDE Arduino :
#include <Servo.h>
#include "IRLibAll.h"
#define PIN_TRIG A4
#define PIN_ECHO A3
#define PIN_IR 2
IRrecvPCI myReceiver(PIN_IR);
IRdecode myDecoder;
#define PIN_MOT1 11
#define PIN_MOT2 5
#define CENTER_MOT1 88
#define CENTER_MOT2 89
#define FACTOR_MOT1 1
#define FACTOR_MOT2 1
Servo motor1;
Servo motor2;
int speed = 80;
int dir = -1;
long mesureDistance(void){
digitalWrite(PIN_TRIG,LOW);
delayMicroseconds(2);
digitalWrite(PIN_TRIG,HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG,LOW);
long duration_hc=pulseIn(PIN_ECHO,HIGH);
long distance_hc=duration_hc/58;
return distance_hc;
}
void setup(void){
pinMode(PIN_TRIG,OUTPUT);
pinMode(PIN_ECHO,INPUT);
myReceiver.enableIRIn();
motor1.attach(PIN_MOT1);
motor2.attach(PIN_MOT2);
motor1.write(CENTER_MOT1);
motor2.write(CENTER_MOT2);
}
void loop(void){
if(myReceiver.getResults()){
myDecoder.decode();
int value=myDecoder.value&0xff;
if(value==2 || value==4 || value==5 || value==6 || value==8) dir=value;
switch(value){
case 0x11 :
if(speed>0) speed -= 5;
break;
case 0x10 :
if(speed<90) speed += 5;
break;
}
delay(50);
myReceiver.enableIRIn();
}
long distance=mesureDistance();
if(distance<20 && dir==2) dir=5;
switch(dir){
case 2 :
motor1.write(CENTER_MOT1+speed*FACTOR_MOT1);
motor2.write(CENTER_MOT2-speed*FACTOR_MOT2);
break;
case 4 :
motor1.write(CENTER_MOT1-speed/6*FACTOR_MOT1);
motor2.write(CENTER_MOT2-speed/6*FACTOR_MOT2);
break;
case 5 :
motor1.write(CENTER_MOT1);
motor2.write(CENTER_MOT2);
break;
case 6 :
motor1.write(CENTER_MOT1+speed/6*FACTOR_MOT1);
motor2.write(CENTER_MOT2+speed/6*FACTOR_MOT2);
break;
case 8 :
motor1.write(CENTER_MOT1-speed);
motor2.write(CENTER_MOT2+speed);
break;
}
delay(10);
}