in System Integration by


I have come across a problem. 
I currently have a GUI that controls a linear actuator. The gui has an "open", "close" and emergency stop button. All of which work nicely with the actuator. 

I am currently trying to integrate an ultrasonic rangefinder into the "close" operation, so if an object comes within 3cm, the actuator stops closing. 

This is the code I have come up with so far, however when i hit the close button on the gui, nothing happens and the gui crashes ( I have to perform a sudo kill on putty to close the GUI). Just wondering if anyone can spot a problem with the code below. Ive defined the GPIO pins and modes in the device driver earlier on. 


void DeviceDriver_Close()


digitalWrite(PIN_TRIGGER, LOW);
digitalWrite(PIN_TRIGGER, HIGH);
digitalWrite(PIN_TRIGGER, LOW);

int echo, previousEcho, lowHigh, highLow, rangeCm;
long getMicrotime(), startTime, stopTime, difference;

lowHigh = highLow = echo = previousEcho = 0;
while (0 == lowHigh || highLow == 0)

previousEcho = echo;
echo = digitalRead(PIN_ECHO);

if(0 == lowHigh && 0 == previousEcho && 1 == echo)

lowHigh = 1;
startTime = getMicrotime();


if(1 == lowHigh && 1 == previousEcho && 0 == echo)
highLow = 1;
stopTime = getMicrotime();

difference = stopTime - startTime;
rangeCm = difference / 58;

while (rangeCm > 3)


digitalWrite(In1_GPIO, HIGH); \\ closes the actuator
digitalWrite(In2_GPIO, LOW);
digitalWrite(PWM_GPIO, 100);

if (rangeCm <=3)


digitalWrite(In1_GPIO, LOW); \\ stops the actuator
digitalWrite(In2_GPIO, LOW);
digitalWrite(PWM_GPIO, 100);



1 Answer

0 votes


it seems to me, that you have implemented a complete control loop within the DeviceDriver_Close() method. I think that the GUI is not "crashed" - it is just blocked because of endless running while() loops.

Please make sure that every device driver function that you have called from the GUI returns immediately.

Take care to implement a clear separation between GUI application and control logic for your device. The GUI should send only commands and receive status information. But do not make control loops in the context of the GUI application.

For more details see the article Integrating with the device it explains the basics for this topic.

Best regards,


Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint