Oui, en effet.
Voici mon code :
#include "SoftwareSerial.h"
#include <CRC16.h>
#define TX_laser 9 //la broche reliée au TX du laser
#define RX_laser 8 //la broche reliée au RX du laser
#define EN_laser 10
SoftwareSerial laser(TX_laser, RX_laser); //on initialise la communication série qui sera appelée laser
SoftwareSerial bt(11, 12); // broches TX, RX du modile bluetooth
CRC16 crc(CRC16_MODBUS_POLYNOME,
CRC16_MODBUS_INITIAL,
CRC16_MODBUS_XOR_OUT,
CRC16_MODBUS_REV_IN,
CRC16_MODBUS_REV_OUT);
uint8_t byte6_1, byte5_1, byte6_2, byte5_2, byte4_1, byte4_2, byte3_1, byte3_2;
uint8_t commandContinue[8] = {0x01, 0x03, 0x00, 0x01, 0x00, 0x02, 0x95, 0xCB};
uint8_t commandUnique[8] = {0x01, 0x03, 0x00, 0x0F, 0x00, 0x02, 0xF4, 0x08};
uint8_t marche[11] = {0x01, 0x10, 0x00, 0x03, 0x00, 0x01, 0x02, 0x00, 0x01, 0x67, 0xA3};
uint8_t data[9];
uint32_t distance;
int STEP = 5;
int DIR = 4;
int EN = 6;
int button = 11;
int interupt_A = 2;
int interupt_B = 3;
int sens = 1;
int posTour = 0;
int nbrPas = 200;
int ecartMes = 1;//pas
long lastDist = 0;
int nbSame =0;
// constante temps
const int MEASURE_TIMEOUT = 1; // 1ms = ~30km à 3*10^8m/s
void setup(){
pinMode(STEP, OUTPUT);
pinMode(DIR, OUTPUT);
pinMode(EN, OUTPUT);
pinMode(button, INPUT_PULLUP);
pinMode( interupt_A, INPUT_PULLUP);
pinMode( interupt_B, INPUT_PULLUP);
pinMode(EN_laser, OUTPUT);
Serial.begin(9600);
bt.begin(9600);
laser.begin(9600);
//digitalWrite(EN_laser, HIGH);
//delay(200);
//laser.write(marche, 11);
//laser.write(commandContinue, 8);
//digitalWrite(EN,LOW);
}
void loop(){
//Serial.println(digitalRead(button));
if(digitalRead(button)==LOW) {
bt.println(" reset");
Serial.println(" reset");
digitalWrite(EN_laser,HIGH);
delay(200);
laser.write(marche,11);
remise_a_0();
//sens 1
digitalWrite(DIR,LOW);
for(int i = 0; i<nbrPas; i+=ecartMes) {
long measure = getDist();
delay(200);
//Serial.println(measure);
while(!(measure >0 && measure < 40000) || measure==lastDist) {
//delay(100);
if(measure==lastDist) nbSame++;
if(nbSame ==4) break;
measure = getDist();
delay(200);
}
lastDist= measure;
nbSame=0;
Serial.println(" "+String(i)+":"+String(measure));
//delay(200);
for(int n =0;n<ecartMes;n++) {
digitalWrite(STEP, HIGH);
delay(3);
digitalWrite(STEP, LOW);
delay(3);
}
delay(200);
}
//Sens 2
digitalWrite(DIR,HIGH);
for(int i = nbrPas; i>0; i-=ecartMes) {
long measure = getDist();
//Serial.println(measure);
while(!(measure >0 && measure < 40000) || measure==lastDist) {
delay(100);
nbSame++;
if(nbSame ==4) break;
measure = getDist();
}
lastDist= measure;
nbSame=0;
Serial.println(" "+String(i)+":"+String(measure));
delay(100);
for(int n =0;n<ecartMes;n++) {
digitalWrite(STEP, HIGH);
delay(3);
digitalWrite(STEP, LOW);
delay(3);
}
//delay(50);
}
digitalWrite(EN,HIGH);
digitalWrite(EN_laser, LOW);
}
}
long getDist() {
crc.restart();
laser.write(commandUnique, 8);
if( laser.available() >0){
laser.readBytes(data, 9);
}
for(int i = 0; i < sizeof(data); i++)
{
//Serial.print(data[i], HEX);
//Serial.print(" ");
crc.add(data[i]);
}
uint8_t CRC = crc.calc();
//Serial.println(CRC, HEX);
delay(200);
byte3_1 = data[3] / 16;
byte3_2 = data[3] - byte3_1 * 16;
byte4_1 = data[4] / 16;
byte4_2 = data[4] - byte4_1 * 16;
byte5_1 = data[5] / 16;
byte5_2 = data[5] - byte5_1 * 16;
byte6_1 = data[6] / 16;
byte6_2 = data[6] - byte6_1 * 16;
uint8_t bytes[8] = {byte6_2,byte6_1,byte5_2,byte5_1,byte4_2,byte4_1,byte3_2,byte3_1}; //bit de poids faible en premier
float dist = 0;
for(int i =0; i< 8;i++) {
//Serial.println(bytes[i]);
dist+= bytes[i]*pow(16,i);
}
//return dist;
if (CRC == 0) {
//Serial.println(dist);
return dist;
} else return -1;
}
void remise_a_0(){
digitalWrite(EN, LOW);
if( digitalRead(interupt_A) == 0 ){
Serial.println("reprise en sens inverse");
digitalWrite(DIR, LOW);
for( int i=0; i<50; i++){
digitalWrite(STEP, HIGH);
delay(2);
digitalWrite(STEP, LOW);
delay(2);
if( digitalRead(interupt_B) == 0 ){
i = 60;
}
}
}
while( digitalRead(interupt_A) == 1){
digitalWrite(DIR, HIGH);
digitalWrite(STEP, HIGH);
delay(2);
digitalWrite(STEP, LOW);
delay(2);
}
}
Le code est une modification de celuis là.