1

I have to make a program that has an active waiting of 1000 milliseconds and then increments a variable during that time inside a while loop. And then, this variable has to be initialized to 0 in every cycle.

Can someone give me a clue on how to do this? I have this code but it's not working at all, just compilation error.

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Real_Time; use Ada.Real_Time;
package body waittime is
    task body periodictime is   

        use type Time;
        use type Time_Span;

        Poll_Time : Ada.Real_Time.Time := 5; -- time to start polling
        WaitVar : Natural := 0;
        WaitTime : constant Time_Span := Milliseconds (1000);
    begin
        loop
            delay until Poll_Time;
            Poll_Time = Poll_Time + WaitTime;
            WaitVar := WaitVar+1;
            Put_Line (WaitVar);
        end loop;
    end periodictime;
end waittime;
3
  • 2
    Ada, Ada, Ada, Ada - I get tired of people not knowing how to write names. You could have put the errors you get - would make helping you much easier and faster. May 8, 2016 at 6:57
  • 1
    Usually it’s best to say what the error message actually is rather than expect us to download your code, write a package spec, and run the compiler ourselves. May 8, 2016 at 7:20
  • What does "And then, this variable has to be initialized to 0 in every cycle.” mean? May 8, 2016 at 7:31

2 Answers 2

2

Having written an (OK, obvious) package spec, compiling with -gnatfl gives

 1. with Ada.Text_IO; use Ada.Text_IO;
 2. with Ada.Real_Time; use Ada.Real_Time;
 3. package body waittime is
 4.     task body periodictime is
 5.         use type Time;
 6.         use type Time_Span;
 7.
 8.         Poll_Time : Ada.Real_Time.Time := 5; -- time to start polling
                                              |
    >>> expected private type "Ada.Real_Time.Time"
    >>> found type universal integer

 9.         WaitVar : Natural := 0;
10.         WaitTime : constant Time_Span := Milliseconds (1000);
11.     begin
12.         loop
13.             delay until Poll_Time;
14.             Poll_Time = Poll_Time + WaitTime;
                          |
    >>> "=" should be ":="

15.             WaitVar := WaitVar+1;
16.             Put_Line (WaitVar);
                1         3
    >>> no candidate interpretations match the actuals:
    >>> missing argument for parameter "Item" in call to "put_line" declared at a-textio.ads:259
    >>> expected type "Standard.String"
    >>> found type "Standard.Integer"
    >>>   ==> in call to "Put_Line" at a-textio.ads:263

17.         end loop;
18.     end periodictime;
19. end waittime;

The error at line 16 is that Put_Line expects a String; Ada doesn’t do conversions on the fly. You could produce a string representation by saying Natural’Image (Waitvar), which is standard Ada, or the GNAT extension Waitvar’Img.

The error at line 14 is easily fixed.

The error at line 8 explains what it wants, but to fix it requires another change. I think you should

  1. Initialize with Ada.Real_Time.Clock
  2. Swap lines 13 and 14.
-1

Here I wrote some sample code for you: http://www.filedropper.com/ada-periodic. You can compile this i.e. in unix terminal by *gnatmake .adb and run by ./main

There are three files: main.adb, periodic.ads and periodic.adb In main.adb there is procedure that start program. This procedure have infinite loop. And this file include periodic package (by using with Periodic in code)

In periodic.ads there is declaration of package. There is only one task in this packge, that is start automatically with main procedure.

In periodic.adb there is a definition of package. There is a task definition that contain infinite loop with delay until structure. You have to get current clock time and increase it by period time you want to wait. Thanks to this operation you wait period time, do something, and set next time to wait.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.