Laser not starting after particle photon is reset

When I flash fresh the firmware works fine
but when I reset the particle photon the laser does not work.

I am opening serial connection to the U81 distance sensor laser

const int powerUp1 = D6;
byte cont_fast[] = { 0xAA, 0x00, 0x00, 0x20, 0x00, 0x01, 0x00, 0x06, 0x27 };
byte cont_exit[] = { 0x58 };

int secAlive = 20;//number of seconds to keep on reading
int secPassedSinceTrigger = 0;

int minR = 0;
int maxR = 0;
int avgR = 0;

unsigned int hexadecimalToDecimal(String hexString) {
    unsigned int decValue = 0;
    int nextInt;

    for (int i = 0; i < hexString.length(); i++) {

        nextInt = int(hexString.charAt(i));
        if (nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9);
        if (nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15);
        if (nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15);
        nextInt = constrain(nextInt, 0, 15);

        decValue = (decValue * 16) + nextInt;
    }

    return decValue;
}

String write_cont_fast() {
    //digitalWrite(powerUp1, HIGH);
    
    int secPrev = Time.local() % 86400;
    bool waitUntilTimeUp = true;
    while(waitUntilTimeUp){
        Serial1.write(cont_fast, sizeof(cont_fast));
        delay(1000);//let the command execute
        
        int bytesAvailable = Serial1.available();
        
        byte bytes[bytesAvailable];
        int hexCounter = 0;
        Serial1.readBytes((char*)bytes, bytesAvailable);
        String output;
        bool IsDataValid = false;
        for (int i = 0; i < bytesAvailable; i++) {
            String data = String(bytes[i], HEX);
            
            if(i==0 && data=="aa"){
                IsDataValid = true;
            }
            
            if(data.length()==1){
                data = "0" + data;
            }
            
            if (i >= 8 && i <= 9) {
                output = output + data;
            }
        }
        
        if(IsDataValid){
            int milimeters = hexadecimalToDecimal(output);
            //float inches = milimeters * 0.0393701;
            if(milimeters<minR  || minR==0){
                minR = milimeters;
            }
            
            if(milimeters>maxR){
                maxR = milimeters;
            }
            
            avgR = (minR + maxR)/2;
            
            CloudPublish("OUTPUT-min", String(33.37-(minR* 0.0393701))); 
            CloudPublish("OUTPUT-max", String(33.37-(maxR* 0.0393701))); 
            CloudPublish("OUTPUT-avg", String(33.37-(avgR* 0.0393701)));
        }
    
        int secNow = Time.local() % 86400;
        secPassedSinceTrigger = secNow - secPrev;
            
        if(secPassedSinceTrigger >= secAlive){
            waitUntilTimeUp = true;
            Serial1.write(cont_exit, sizeof(cont_exit));
            //digitalWrite(powerUp1, LOW);
        }
    }
    secPassedSinceTrigger = 0;
}

String write_cont_exit() {
    //Serial1.write("X");
    //Serial1.write(cont_exit, sizeof(cont_exit));
    //delay(500);//let the command execute
}

void CloudPublish(String key, String value) {
    Particle.publish(key, value);
}

void setup() {
    pinMode(powerUp1, OUTPUT);
    digitalWrite(powerUp1, HIGH);
    
    Serial1.begin(19200, SERIAL_8N1);
}

void loop() {
    write_cont_fast();
    write_cont_exit();
    
    CloudPublish("OUTPUT-min", String(minR* 0.0393701)); 
    CloudPublish("OUTPUT-max", String(maxR* 0.0393701)); 
    CloudPublish("OUTPUT-avg", String(avgR* 0.0393701));
    
    minR = 0;
    maxR = 0;
    avgR = 0;
}

Maybe the sensor needs resetting too.

Some devices using serial comms have issues when you reset the processor. The serial TX line to the device will toggle during the reset. The can cause the receiver to think some data is being received. Yet it’s garbage. Much like when you sending info to the terminal at 115200, but your terminal window is set to 9600 you will see garbage characters.

If your device expects a CR or LF to signal the end of your command, then I would send a few CR’s in the setup() after Serial1.begin(19200, SERIAL_8N1); Just to clear the receivers buffer out.

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