Autor Beitrag
Hacker1
Hält's aus hier
Beiträge: 6



BeitragVerfasst: Di 16.05.06 11:37 
Hallo Leute

Ich bräuchte Hilfe in meinem Ampelprojekt in Delphi ich komme nicht weiter ich weis nicht wie ich anfangen soll

meine schritte:

1.Anfangsposition: jede Ampel ist aus
2.Autoampel ist grün
Fußgängerampel ist rot
3.Ampel springt auf orange kurz danach auf rot
4.Fußgängerampel wird grün
5.Fußgängerampel wird rot
6.Autoampel wird gelb danach auf grün

Dies soll mit der while schleifen funktion passieren habe echt kein plan ich hoffe ich könnt mir helfen

mfg

hacker1
starsurfer
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 334

Win 95, Win 98, Win XP, Win Vista, Linux
D5 Enterprise ,D2005, D6 Personal, Visual C++ Express 2005, C++ Builder 6 E, Dev-C++
BeitragVerfasst: Di 16.05.06 12:31 
also:

ich hab keine Ahnung wie du das mit 'while' machen willst, zumal selbst wenns ginge wäre das so schnell das das fürs Auge nicht sichbar wäre UND du würdest den Comp auch noch in ne Endlosschleife schicken....

ich würds so machen:

Was brauch ich für Komponenten?
5x TShape //für die ampellichter
1x TTimer //zumsteuern der abläufe
1x Button // zum an und aus schalten

Was brauch ich an Variablen?
StatusAutoAmpel:array[0..2] of bool;//speichert den wert der Ampel - 3te Stelle:rot, 2te Stelle:gelb, 1te Stelle:Grün - true = an, false =aus
StatusFussgaengerAmpel:integer;//speichert den Wert der Ampel - 2Rot, 1:Grün

Wann leuchtet welches Licht?
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
Auto         |Fußgänger
-----------------------
Rot Gelb Grün| Rot Grün
aus aus  an  | an  aus
aus an   aus | an  aus
an  aus  aus | aus an
an  an   aus | an  aus
aus aus  an  | an  aus


Daraus muss ich jetzt ermitteln welches licht wann an ist,
zB. Fügänger haben nur Grün wenn
ausblenden Quelltext
1:
Autoampel Rot AN ist UND Autoampel Gelb AUS					


um keinen Status am anfang zu haben setzt du beim Formcreate einfach alles auf False bzw auf 0 ( auch den Timer ausschalten)

mit den klick auf dem Button startest du den Ampel Vorgang

die "Formeln" für jedes Licht setzt du jetzt im Timer Ereignis um.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.Timer1Timer(Sender: TObject);
begin

end;


und setzt die Werte halt wie sie sein müssen.
Bsp:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
if (StatusAutoAmpel[2]=true) and (StatusAutoAmpel[1]=false) then //Abfragen ob Rot an ist und Grün aus
 begin
 fussgaengerAmpel:=1//Fußgängerampelt hat jetzt Status 1 = Grün leuchtet
 end;


hast du die Formeln umgesetzt muss du die TShaps noch nach Status sichtbar bzw unsichtbar machen...

star

_________________
GEIZ IST GEIL! - Ihr Sozialamt
DaKirsche
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 187

Win XP Pro, SuSe Linux 7.3 - 10.2, Win 2k3 Server, Win 2000, Win NT 4.0
Delphi 2006 Pro, Java, HTML, SQL, PHP, CSS
BeitragVerfasst: Di 16.05.06 13:05 
@starsurfer : wozu gibt es sleep(); ?? Damit wird es langsamer. Dann Application.Processmessages, damit die Änderung angezeigt wird und Klicks abgefragt werden und eine Boolsche Variable zum Beenden der Schleife.


Also ich habe es zwar etwas umständlich gelöst, aber zumindest mit While-Schleife...

Er wiederholt den Vorgang der kompletten Ampelphasen solange, bis der Stop-Button geklickt wurde....
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
    procedure FormCreate(Sender: TObject);  //Initialisierungsprozedur von Form1
    procedure StartAmpelSimulationClick(Sender: TObject); //Button
    procedure SetStartWerte();
    procedure ColorizeAll();
    procedure ColorizeSingle(AmpelLicht: TShape; Farbe : TColor);
    procedure BeendeSimulationClick(Sender: TObject);  //Button
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
   stop : boolean;
implementation

{$R *.dfm}

procedure TForm1.SetStartWerte();
begin
stop := false;
ColorizeAll();
ColorizeSingle(Shape1, clRed);
ColorizeSingle(Shape5, clLime);
end;

procedure TForm1.ColorizeAll();
begin
ColorizeSingle(Shape1, clSilver);
ColorizeSingle(Shape2, clSilver);
ColorizeSingle(Shape3, clSilver);
ColorizeSingle(Shape4, clSilver);
ColorizeSingle(Shape5, clSilver);
end;

procedure TForm1.ColorizeSingle(AmpelLicht: TShape; Farbe : TColor);
begin
AmpelLicht.Brush.Color := Farbe;
Application.Processmessages; //Fragt z.B. Mausklicks ab und aktualisiert Bildschirm.
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
//Festlegen der Farben der Shapes
//Für die Optik sollten die Shapes in den Propertys Shape den Wert stCircle haben
//Alle Ampeln sind aus
Shape1.Brush.Color := clsilver;
Shape2.Brush.Color := clsilver;
Shape3.Brush.Color := clsilver;
Shape4.Brush.Color := clsilver;
Shape5.Brush.Color := clsilver;
end;

procedure TForm1.StartAmpelSimulationClick(Sender: TObject);
begin
SetStartWerte();

 while (not stop) do begin
    sleep(1000); //1Sek. Pause
    ColorizeAll();
     ColorizeSingle(Shape4, clYellow);
     ColorizeSingle(Shape1, clRed);
    sleep(1000); //1Sek. Pause
    ColorizeAll();
     ColorizeSingle(Shape3, clRed);
     ColorizeSingle(Shape1, clRed);
    sleep(1000); //1Sek. Pause
    ColorizeAll();
     ColorizeSingle(Shape3, clRed);
     ColorizeSingle(Shape2, clLime);
    sleep(1000); //1Sek. Pause
    ColorizeAll();
     ColorizeSingle(Shape1, clRed);
     ColorizeSingle(Shape3, clRed);
    sleep(1000); //1Sek. Pause
    ColorizeAll();
     ColorizeSingle(Shape1, clRed);
     ColorizeSingle(Shape4, clYellow);
    sleep(1000); //1Sek. Pause
    ColorizeAll();
     ColorizeSingle(Shape1, clRed);
     ColorizeSingle(Shape5, clLime);
    sleep(1000); //1Sek. Pause
end;
ColorizeAll(); //Alles auf Ausgangswerte.
end;
procedure TForm1.BeendeSimulationClick(Sender: TObject);
begin
stop := true;
end;
chriss1988
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 389

windows xp prof,home,windows98
delphi5
BeitragVerfasst: Di 16.05.06 13:12 
und was bedeutet bei dir die einzelnen prozeduren?

ausblenden Delphi-Quelltext
1:
2:
3:
procedure ColorizeAll(); 
procedure SetStartWerte();
procedure ColorizeSingle(AmpelLicht: TShape; Farbe : TColor);
LLCoolDave
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 212

Win XP
Delphi 2005
BeitragVerfasst: Di 16.05.06 13:16 
DaKirsche: Ich würde trotzdem von sleep abraten, da reagiert das Programm gar nicht mehr. Sinnvoller wäre es bis zum verstreichen einer bestimmten immer wieder die neu ankommenden Nachrichten zu verarbeiten, dann reagiert das Programm auch weiterhin, auch wenn die Prozedur solange Pause macht:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
var
lasttickcount: cardinal;

lasttickcount := GetTickCount;
while lasttickcount + 100 < GetTickCount do
  Application.ProcessMessages;


Da bleibt die Prozedur auch 100ms stehen, aber man kann in der zwischenzeit das Programm trotzdem verschieben, Beenden, Buttons klicken etc.

Sleep ist wie das Bremsen eines Autos mit einem Anker. Es funktioniert zweifelsohne, hat jedoch nicht immer den gewünschten Effekt (es wird wohl entweder die Ankerkette oder ein Teil Karosserie nachgeben), und erscheint bei näherer Betrachtung doch eher unbrauchbar wegen anderer Alternativen.
DaKirsche
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 187

Win XP Pro, SuSe Linux 7.3 - 10.2, Win 2k3 Server, Win 2000, Win NT 4.0
Delphi 2006 Pro, Java, HTML, SQL, PHP, CSS
BeitragVerfasst: Di 16.05.06 13:32 
Mit ColorizeAll(); werden alle AmpelLichter auf grau gesetzt.
ColorizeSingle kann man mit den Werten der Shapebezeichnung und eines Farbwertes ein bestimmtes Licht färben (zum "leuchten bringen").
SetStartWerte(); macht quasi beides und setzt damit die Anfangswerte (FAmpel rot AAmel grün).

Nagut, aber wenns so sein soll kann ich es auch insofern übertreiben und sleep(1); 1000mal in einer Schleife wiederholen lassen, dann wird alle 1ms die eingabe abgefragt....
chriss1988
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 389

windows xp prof,home,windows98
delphi5
BeitragVerfasst: Di 16.05.06 13:36 
jo sorry war mein fehler habs zuspät erkannt
@ DaKirsche
dein code hatte ein paar fehler(die ampel war falsch)

und das mit dem stop geht auch nicht

also hier nochmal der code richtig sosollte die ampel laufen

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    Shape1: TShape;
    Shape2: TShape;
    Shape3: TShape;
    Shape4: TShape;
    Shape5: TShape;
    StartAmpelSimulation: TButton;
    timer1: TTimer;
    BeendeSimulation: TButton;
    procedure FormCreate(Sender: TObject);  //Initialisierungsprozedur von Form1
    procedure StartAmpelSimulationClick(Sender: TObject); //Button
    procedure SetStartWerte();
    procedure ColorizeAll();  
    procedure ColorizeSingle(AmpelLicht: TShape; Farbe : TColor);  
    procedure BeendeSimulationClick(Sender: TObject);  //Button  
  private  
    { Private-Deklarationen }
  public  
    { Public-Deklarationen }  
  end;  

 
var  
  Form1: TForm1;
   stop : boolean;  
implementation

 
{$R *.dfm}  

 
procedure TForm1.SetStartWerte();
begin  
stop := false;  
ColorizeAll();
ColorizeSingle(Shape1, clRed);
ColorizeSingle(Shape4, clred);
end;  

 
procedure TForm1.ColorizeAll();
begin
ColorizeSingle(Shape1, clSilver);
ColorizeSingle(Shape2, clSilver);
ColorizeSingle(Shape3, clSilver);  
ColorizeSingle(Shape4, clSilver);  
ColorizeSingle(Shape5, clSilver);  
end;  

 
procedure TForm1.ColorizeSingle(AmpelLicht: TShape; Farbe : TColor);
begin
AmpelLicht.Brush.Color := Farbe;
Application.Processmessages; //Fragt z.B. Mausklicks ab und aktualisiert Bildschirm.  
end;  

 
procedure TForm1.FormCreate(Sender: TObject);  
begin  
//Festlegen der Farben der Shapes  
//Für die Optik sollten die Shapes in den Propertys Shape den Wert stCircle haben  
//Alle Ampeln sind aus
Shape1.Brush.Color := clsilver;
Shape2.Brush.Color := clsilver;
Shape3.Brush.Color := clsilver;
Shape4.Brush.Color := clsilver;
Shape5.Brush.Color := clsilver;
end;  

 
procedure TForm1.StartAmpelSimulationClick(Sender: TObject);
begin  
SetStartWerte();  


 while (not stop) do begin


    sleep(1000); //1Sek. Pause
    ColorizeAll();
     ColorizeSingle(Shape1, clred);
     ColorizeSingle(Shape5, clGreen);


    sleep(1000); //1Sek. Pause  
    ColorizeAll();
     ColorizeSingle(Shape4, clRed);
     ColorizeSingle(Shape1, clRed);


    sleep(1000); //1Sek. Pause  
    ColorizeAll();
     ColorizeSingle(Shape1, clred);
     ColorizeSingle(Shape2, clyellow);
     ColorizeSingle(Shape4, clRed);


    sleep(1000); //1Sek. Pause
    ColorizeAll();
     ColorizeSingle(Shape3, clGreen);
     ColorizeSingle(Shape4, clRed);

    sleep(2000); //1Sek. Pause
     ColorizeAll();
     ColorizeSingle(Shape2, clYellow);
     ColorizeSingle(Shape4, clRed);

   sleep(1000); //1Sek. Pause
     ColorizeAll();
     ColorizeSingle(Shape2, clYellow);
     ColorizeSingle(Shape1, clRed);
     ColorizeSingle(Shape4, clRed);
end;


end;
procedure TForm1.BeendeSimulationClick(Sender: TObject);
begin
stop := true;
end;

end.
DaKirsche
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 187

Win XP Pro, SuSe Linux 7.3 - 10.2, Win 2k3 Server, Win 2000, Win NT 4.0
Delphi 2006 Pro, Java, HTML, SQL, PHP, CSS
BeitragVerfasst: Di 16.05.06 13:48 
nagut, das Einzige, was du jetzt verändert hast ist, dass teilweise 3 Lampen leuchten.....sonst sehe ich nix, was du geändert hast....aber ok....^^
Jetstream
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 222



BeitragVerfasst: So 21.05.06 11:32 
Du kannst dir bei Initialisierung des Programms die Systemzeit merken, und diese dann per Timer alle x Millisekunden mit der aktuellen Zeit vergleichen, ob sich die Ampeln umschalten müssen.

Das ist etwas eleganter als der sleep() Befehl.
Jakob Schöttl
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 929
Erhaltene Danke: 1


Delphi 7 Professional
BeitragVerfasst: Mo 22.05.06 12:13 
In einer While-Schleife kannst du das vielleicht auch lösen mit dem Sleep Befehl.
Sleep(Millisekunden): Hält das Programm die angegebenen millisekunden an.

Nachteil: Man kann das Programm nur mit gewalt und dem Taskmanager beenden, es sei denn, du baust bei jedem Durchgang folgenden Befehl ein.
Application.ProcessMessages: Unterbricht die aktuelle Procedure bzw. funktion und reagiert auf andere Ereignisse für die Anwendung. Da könnte dann die Anwendung geschlossen werden vom Benutzer...

Vielleicht hilfts dir was
Affenkopf
Hält's aus hier
Beiträge: 1



BeitragVerfasst: So 29.04.07 20:51 
hallo leute, wie kann ich diese ampelprogramm denn mit case Anweisungen schreiben. Ich habs mal so programmiert wies hier steht, dass geht auch, aber mein Lehrer will, dass wir es mit Case Anweisungen machen.

Gruß Affe
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 29.04.07 23:35 
Moin und :welcome: im Forum!

user profile iconAffenkopf hat folgendes geschrieben:
wie kann ich diese ampelprogramm denn mit case Anweisungen schreiben. Ich habs mal so programmiert wies hier steht, dass geht auch, aber mein Lehrer will, dass wir es mit Case Anweisungen machen.

Laut Forumsregeln musst du für eine neue Frage einen neuen Thread erstellen, sonst gibt es hier Chaos, weil man später nix mehr wiederfindet... :? :mahn: ;)

Aber trotzdem hier schonmal der Hinweis: hast du bereits die Suchfunktion verwendet? :arrow: Suche in: Delphi-Forum, Delphi-Library AMPEL, vielleicht ist ja schon was dabei, was dich weiter bringt. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
hansg
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 168

Win XP Professional SP3, Vista Ultimate 32 SP1
Delphi 6 Professional
BeitragVerfasst: Mo 30.04.07 19:54 
Hallo,

habe mal auf die schnelle was gebastelt für eine Ampel.
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Shape1: TShape;
    Shape2: TShape;
    Shape3: TShape;
    Button1: TButton;
    Button2: TButton;
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  z: integer;


implementation

{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  z:=z+1;                               // Zähler Ablauf
  case z of
  3begin                              // Rotgelb
       shape1.Brush.Color:=clred;
       shape2.Brush.Color:=clyellow;
     end;

  4begin                             // Grün
       shape1.Brush.Color:=clwhite;
       shape2.Brush.Color:=clwhite;
       shape3.Brush.Color:=clgreen;
     end;


  10:begin                             // Gelb
       shape2.Brush.Color:=clyellow;
       shape3.Brush.Color:=clwhite;
     end;

  14:begin                             // Rot
      shape1.Brush.Color:=clred;
      shape2.Brush.Color:=clwhite;
     end;
  17:begin
       z:=0;                  // Zähler löschen / Neustart
     end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  shape1.Brush.Color:=clred;
  z:=0;
  timer1.Enabled:=true;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  timer1.Enabled:=false;
  shape1.Brush.Color:=clwhite;
  shape2.Brush.Color:=clwhite;
  shape3.Brush.Color:=clwhite;
end;

end.

_________________
Gruß Hans