Помогите разобраться с лазерным дальномером и дисплеем
- Войдите на сайт для отправки комментариев
Чт, 13/06/2019 - 12:21
Добрый день, прошу помощи, 3й день пытаюсь обьеденить стандартный скетч лазерного модуля измерения расстояния и дисплея, удалось добиться чтобы информация выводилась на дисплей после посыла команд на Serial но не могу понять как сделать чтобы при включении эта команда отправлялась автоматически и на дисплее появлялась информация об измерении
// Please connect your LRF module with arduino like that // The LCD display is just an option because the resylt will be shown also in your arduino terminal! You can remove it if you want ;) // Please note that your LRF module should be POWERED with 3.3v VCC // * LRF RX pin to D10 // * LRF TX pin to D0 // * Push button (Trigger) pins on D8 & GND (D8 is already configured to internal pullup Mode) #include <SoftwareSerial.h> #include <SPI.h> #include <Adafruit_GFX.h> #include <Adafruit_PCD8544.h> // Software SPI (slower updates, more flexible pin options): // pin 7 - Serial clock out (SCLK) // pin 6 - Serial data out (DIN) // pin 5 - Data/Command select (D/C) // pin 4 - LCD chip select (CS) // pin 3 - LCD reset (RST) Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 9); #define NUMFLAKES 10 #define XPOS 0 #define YPOS 1 #define DELTAY 2 // This software serial will be used to send instruction to the LRF module (with the Tx pin/D10 only,, the Rx pin will not be used) // If you are using an arduino with multipl built-in serial ports like arduino Mega, then ther is no need to use a software serial!! just replace it with a free SerialPort SoftwareSerial portOne(2, 3); // Variables int Trigger = 8; // For the Push button/Trigger that will activate the single mode measurement int led = 13; int counter = 0; boolean Validation = false; boolean hold_trigger = false; //ARDUNIO Serial control command #define LASER_OPEN "O"//Open the module #define MEASURE_ONE "D"//Single measurement #define MEASURE_CONTINUE "C"//Continuous measurement #define STOP_MEASURE "S"//Stop measuring #define STEP_LASER_OPEN 0//Open the module #define STEP_MEASURE_ONE 1//Single measurement #define STEP_MEASURE_CONTINUE 2//Continuous measurement #define STEP_STOP_MEASURE 3//Stop measuring #define MEASURE_CONTINUE_N 200//Continuous measurement #define SUCCESS 0//measurement #define FAIL -1//measurement unsigned char measure_continu_cnt=0; unsigned char measure_continue_flag=0; //Function declaration String ReadData(char Length); String Serial_One_ReadData(char Length); void Measure(char MEASURE_COMMEND);//distance measurement String LaserOpen(); String MeasureOne(); String MeasureContinue(); void measure_continue_data(); String StopMeasure(); void setup() { display.begin(); // init done display.setContrast(55); display.clearDisplay(); // setup the arduino Uno Serial Port(Tx0/Rx0) Serial.begin(9600);//Serial debug baud rate while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // Start both software serial ports portOne.begin(115200); //Laser ranging module baud rate portOne.print("$0003260130&"); // LRF light pointer ON // setup the pin D8 as a pullup Input pin pinMode(Trigger, INPUT_PULLUP); pinMode(led, OUTPUT); } void loop() { digitalWrite(led, LOW); delay(100); digitalWrite(led, HIGH); delay(100); ///portOne.write("$00022426&"); //Serial.write(67); String temp=""; temp= ReadData(); if(temp == LASER_OPEN) Measure(STEP_LASER_OPEN); else if(temp == MEASURE_ONE) Measure(STEP_MEASURE_ONE); else if(temp == MEASURE_CONTINUE) Measure(STEP_MEASURE_CONTINUE); else if(temp == STOP_MEASURE) Measure(STEP_STOP_MEASURE); else if(temp != "") Serial.print("UNDEFINED COMMEND\r\n"); if(measure_continue_flag==1) { measure_continue_data(); } ; } void Measure(char STEP_MEASURE_COMMEND)//distance measurement { String temp=""; switch(STEP_MEASURE_COMMEND) { case STEP_LASER_OPEN: temp = LaserOpen(); if(temp!= "-1") { Serial.print("LASER OPEN SUCCEED\r\n"); } else { Serial.print("LASER OPEN FAILD\r\n"); } break; case STEP_MEASURE_ONE: temp = MeasureOne(); if(temp!= "-1") { if(temp.indexOf("&")==9 | temp.lastIndexOf("$")==10) { temp=temp.substring(20,22)+"."+temp.substring(22,27); } else { temp=temp.substring(10,12)+"."+temp.substring(12,17); } temp="distanc="+temp+"m"+"\r\n"; Serial.print(temp); display.clearDisplay(); // clears the screen and buffer display.setCursor(0, 0); display.print(temp); display.display(); } else { Serial.print("MEASURE_ONE FAILD\r\n"); } break; case STEP_MEASURE_CONTINUE: Serial.print("MEASURE_CONTINUE START\r\n"); temp=MeasureContinue(); measure_continu_cnt=0; measure_continue_flag=1; if(temp!= "-1") { if(temp.indexOf("&")==9 | temp.lastIndexOf("$")==10) { temp=temp.substring(24,26)+"."+temp.substring(26,29); } else { temp=temp.substring(14,16)+"."+temp.substring(16,19); } temp="distanc="+temp+"m"+"\r\n"; Serial.print(temp); display.clearDisplay(); // clears the screen and buffer display.setCursor(0, 0); display.print(temp); display.display(); } else { Serial.print("MEASURE_ONE FAILD\r\n"); } break; case STEP_STOP_MEASURE: temp = StopMeasure(); measure_continu_cnt=0; measure_continue_flag=0; Serial.print("MEASURE STOP\r\n"); break; default: break; } } //Turn on the laser pointer String LaserOpen() { String return_data=""; unsigned char temp=0; portOne.listen(); if (portOne.isListening()) { portOne.flush(); portOne.print("$0003260130&"); return_data = Serial_One_ReadData(22); return return_data;//Turn on the laser module } } //Single range measurement String MeasureOne() { String return_data=""; unsigned char temp=0; portOne.listen(); if (portOne.isListening()) { portOne.flush(); portOne.print("$00022123&"); return_data = Serial_One_ReadData(28); return return_data; } } //Continuous ranging String MeasureContinue() { String return_data=""; unsigned char temp=0; portOne.listen(); if (portOne.isListening()) { portOne.flush(); portOne.print("$00022426&"); return_data = Serial_One_ReadData(48); return return_data; } } void measure_continue_data() { String temp=""; temp=Serial_One_ReadData(38); measure_continu_cnt++; if(temp.substring(17,19)=="15"|temp.substring(17,19)=="16"|temp.substring(17,19)=="") { Serial.print( measure_continu_cnt); Serial.print("ERROR\r\n"); temp=MeasureContinue(); temp=temp.substring(24,26)+"."+temp.substring(26,29);; Serial.print( measure_continu_cnt); temp=" distanc="+temp+"m"+"\r\n"; } else { temp=temp.substring(14,16)+"."+temp.substring(16,19);; Serial.print( measure_continu_cnt); temp=" distanc="+temp+"m"+"\r\n"; } Serial.print(temp); display.clearDisplay(); // clears the screen and buffer display.setCursor(0, 0); display.print(temp); display.display(); } //Stop measurement String StopMeasure() { String return_data=""; unsigned char temp=0; portOne.listen(); if (portOne.isListening()) { portOne.flush(); portOne.print("$0003260029&"); return_data = Serial_One_ReadData(22); return return_data; } } //Read serial port to return data String ReadData() { int wait_time=0; String return_data=""; do { delay(1); wait_time++; while(Serial.available()>0)//serial data read { char CharRead=Serial.read(); if(CharRead!=10&&CharRead!=13) { return_data +=CharRead; } wait_time=0; } } while(wait_time<500); //Wait for 1 second, no data is returned, the read is finished. return return_data; } //Read serial port to return data String Serial_One_ReadData(char Length) { int wait_time=0; String return_data=""; do { delay(1); wait_time++; while( portOne.available()>0)//serial data read { char CharRead= portOne.read(); if(CharRead!=10&&CharRead!=13) { return_data +=CharRead; } wait_time=0; if(return_data.length()>=Length) { wait_time=6000; } } } while(wait_time<2000); //Wait for 1 second, no data is returned, the read is finished. unsigned char last_byte=(unsigned char)return_data.charAt(return_data.length()-1); if( wait_time == 6000) { } else { return_data="-1"; } return return_data; }