Real-time applications

Hi All,
I went through some libraries and did not udertand one basic thing. There are used lot of delay functions inside libraries. That means waiting preset time on one place in the program. So it doesn't depand how fast processor you have but you adding solid times to wait in the program. These delays adding together and made the main loop longer and longer. What is the way to use together for example step motor driving an LiquidCrystal and ultrasonic sensor SRF02? When I made it happened exactly I expected. The step motor ran discontinually due the delay functions in the code. Some things I changed into enhanced state machines but still there are a libraries with delay functions. May be I misunderstand your concept. How do you use it for controlling robots for example where are more than one axis and displays and many sensors? Do you use your own libraries made for real time application where you use state machines for cycle waiting? Or do you use interrupts for critical proceses to be more continuous?

Look what I mean:
Example one of functions for SRF02 (SRF02_Ultrasonic_sensor_(SKU_SEN0005)-DFRobot):
unsigned char SendCmd(unsigned char address,unsigned char cmd)
{
static unsigned char uc_t_done = 0;//externi casovac

Serial.write(address);//set the address of SRF02(factory default is 0)
delay(100);//serial data is fixed at 9600,N,8,2,so we need some time to creat the sencond stop bit
Serial.write(cmd);//send the command to SRF02
delay(100);//serial data is fixed at 9600,N,8,2,so we need some time to creat the sencond stop bit
return
}

Or Example of IIC on http://arduino.cc/en/Tutorial/SFRRangerReader:
...
Wire.write(byte(0x00)); // sets register pointer to the command register (0x00)
Wire.write(byte(0x50)); // command sensor to measure in "inches" (0x50)
// use 0x51 for centimeters
// use 0x52 for ping microseconds
Wire.endTransmission(); // stop transmitting

// step 2: wait for readings to happen
delay(70); // datasheet suggests at least 65 milliseconds

// step 3: instruct sensor to return a particular echo reading
Wire.beginTransmission(112); // transmit to device #112
...

Or Example of LicquidCrystal functions:
void LiquidCrystal::clear()
{
command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
delayMicroseconds(2000); // this command takes a long time!
}

void LiquidCrystal::home()
{
command(LCD_RETURNHOME); // set cursor position to zero
delayMicroseconds(2000); // this command takes a long time!
}

BR
Frank

Because libaries are made mainly by individuals there is no overall plan for them to be used together. If they contain delays then they will be blocking. If the blocking is too much you must rewrite them as state machines or write your own code as a state machine.

That is just the way it is.

I agree. Using some libraries together makes a program slower and slower and as it grows, finally is unusable. In some situations the delay there must be to esure precise timing (e.g. switching on pin). If the delays are longer (in hundreeds of milliseconds) it worth to run other tasks. The solution is to use better techniques, but it requires higher level of knowledge and programming skills. There are many solutions on the internet from simple timing up to full-value multitasking; other question is then MCU's power. Personally, I use simple timing with semaphore system and simple priority system.

Many of the lib's are very badly written

 Serial.write(address);//set the address of SRF02(factory default is 0)
  delay(100);//serial data is fixed at 9600,N,8,2,so we need some time to creat the sencond stop bit

the above is a good example. Serial (well the UART h/w ) deals with all the start/stop bits and it buffers the data in any case. And to cap that 9600 baud is 10k bits per second (round numbers) so waiting 1/10th of a second is just stupid!.

It's up to you to look at the code and decide if its fit for use.

Mark

Thank you for reply. Anyway I am glad to have some examples to start work with arduino. It saves developing time. Good thing is the open source. I can see the code and can make some correction to fit the specific application.