Hi!
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);
usleep(10);
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);
}
}