I am working on a petrol pump display, I have read the sticky on homework help and therefore understand and will appreciate all the help that is given to me.

Basically when the Nozzle button is pressed I want it to start dispencing the petrol so the litres dispensed will go up which is controlled by inserting pictures that are in the same directory. My code is below and runs but the numbers take 10 seconds to change to each one and I don't understand how to implement a interval and so it works on a button, with the timer disabled until pressed, I have many ways but they don't work.

procedure TfrmPetrolDisplay.Timer1Timer(Sender: TObject);
Var
  LitresStr:String;
  n1:char;
  n2:char;
  n3:char;
begin
  Litres:=Litres+1;
  LitresStr:=IntToStr(litres);

While Length(litresStr) <=3 do
  LitresStr:='0'+LitresStr;

n1:=LitresStr[1];
n2:=LitresStr[2];
n3:=LitresStr[3];
Begin
Case n1 Of
'0':Img1.picture.loadfromfile('0.bmp');
'1':Img1.picture.loadfromfile('1.bmp');
'2':Img1.picture.loadfromfile('2.bmp');
'3':Img1.picture.loadfromfile('3.bmp');
'4':Img1.picture.loadfromfile('4.bmp');
'5':Img1.picture.loadfromfile('5.bmp');
'6':Img1.picture.loadfromfile('6.bmp');
'7':Img1.picture.loadfromfile('7.bmp');
'8':Img1.picture.loadfromfile('8.bmp');
'9':Img1.picture.loadfromfile('9.bmp');
End;

Begin
Case n2 Of
'0':Img2.picture.loadfromfile('0.bmp');
'1':Img2.picture.loadfromfile('1.bmp');
'2':Img2.picture.loadfromfile('2.bmp');
'3':Img2.picture.loadfromfile('3.bmp');
'4':Img2.picture.loadfromfile('4.bmp');
'5':Img2.picture.loadfromfile('5.bmp');
'6':Img2.picture.loadfromfile('6.bmp');
'7':Img2.picture.loadfromfile('7.bmp');
'8':Img2.picture.loadfromfile('8.bmp');
'9':Img2.picture.loadfromfile('9.bmp');
End;

Begin
Case n3 Of
'0':Img3.picture.loadfromfile('0.bmp');
'1':Img3.picture.loadfromfile('1.bmp');
'2':Img3.picture.loadfromfile('2.bmp');
'3':Img3.picture.loadfromfile('3.bmp');
'4':Img3.picture.loadfromfile('4.bmp');
'5':Img3.picture.loadfromfile('5.bmp');
'6':Img3.picture.loadfromfile('6.bmp');
'7':Img3.picture.loadfromfile('7.bmp');
'8':Img3.picture.loadfromfile('8.bmp');
'9':Img3.picture.loadfromfile('9.bmp');
End;
End;
End;
End;
End;
end.

Thanks, all help appreciated.

Are you saying that it takes 10 seconds for each digit? Or for each change in numbers?

If the latter, make sure you set the timer1.interval property to something relatively low, like 200 milliseconds.

If the former, you'll get a better response by loading all the images once, then just changing the image displayed. You can also avoid some repetition by creatively using some arrays.

For example:

type
  TfrmPetrolDisplay = class( TForm )
    private
      f_images: array[ 1..3 ] of tImage;
      f_digits: array[ 0..9 ] of tBitmap;
    ...
    end;

...

procedure TfrmPetrolDisplay.FormCreate( sender: tObject );
  var cntr: integer;
  begin
  // Initialize our "easy access" images array
  f_images[ 1 ] := Img1;
  f_images[ 2 ] := Img2;
  f_images[ 3 ] := Img3;

  // Load all bitmaps from file or resource or wherever you keep them
  // (this example loads from file)
  for cntr := 0 to 9 do begin
    f_digits[ cntr ] := tBitmap.create;
    f_digits[ cntr ].loadFromFile( intToStr( cntr ) +'.bmp' )
    end
  end;

...

procedure TfrmPetrolDisplay.FormDestroy( sender: tObject );
  var cntr: integer;
  begin
  // clean up our digits images
  for cntr := 0 to 9 do f_digits[ cntr ].free
  end;

...

procedure TfrmPetrolDisplay.Timer1Timer( sender: tObject );
  var
    cntr: integer;
    LitresStr: string;
  begin
  Litres := Litres +1;

  // Convert number of liters to a 3-digit number
  LitresStr := intToStr( Litres );
  LitresStr := stringOfChar( '0', 3 -length( LitresStr ) ) +LitresStr;

  // Set each of the displayed digit images
  for cntr := 1 to 3 do
    f_images[ cntr ].picture.bitmap.assign( f_digits[ cntr ] )
  end;

To start and stop a timer, just set the timer1.enabled property to true or false.

Hope this helps.

I tried your way but I just can't get it to work, my way works I just need to implement a timer in correctly and I don't understand how. Because there is no interval at the moment in my code, it takes 10 seconds to change from 0.00 to 0.01 then 10 seconds from 0.01 to 0.02 etc.

I kind of need my code to just be edited or someone to explain how to implement a timer.

Again much help appreciated from anyone :),

make sure you set the timer1.interval property to something relatively low, like 200 milliseconds.

To start and stop a timer, just set the timer1.enabled property to true or false.

123456789

Alright it seems to be working now although it wasn't yesterday :) lol. Thanks very much.

This article has been dead for over six months. Start a new discussion instead.