Hallo zusammen, bin neu hier und auch neu in der uC-Programmierung. Ich möchte gerne eine Ampelsteuerung umsetzten, habe aber folgenden Fehler: Die LEDs beginnen schon zu blinken, owbohl die erste Verzweigung false liefert. Warum ist das so? Der Code befindet sich im Anhang. Vielleicht könnte mir einer helfen. mfg Tobias
:
Verschoben durch Moderator
Nein die geschweiften Klammern sind nicht falsch gesetzt. Beim normal Betrieb geht das Programm immer in die Verzweigungen, die jedoch FALSE sind. Komischerweise beim Debuggen nicht. An was kann das liegen?
Für
1 | // Ampel: 1 Ampel 1 & 1 Ampel 2
|
2 | |
3 | // Configuration variables
|
4 | const byte LEDOff = HIGH; // If LED are switeched against Vcc, then LEDOff = HIGH |
5 | const byte LEDOn = LOW; |
6 | const byte Off = LEDOff; |
7 | const byte On = LEDOn; |
8 | |
9 | const int GapTime = 200; // delay time, one light on |
10 | const byte LEDcount = 6; // no of LEDs |
11 | |
12 | byte CurrentLED = 0; // current LED, horizontal loop through array |
13 | byte CurrentLEDState = Off; // current LED State |
14 | byte CurrentPhase = 0; // vertical loop through array |
15 | int CurrentDelay = 0; // Delay in current phase |
16 | |
17 | int Counter = 1; // loop counter |
18 | |
19 | byte LEDMode = 1; // op modes - 0: All off 1: Normal Operation 2: LED yellow flash (night mode) |
20 | byte CurrentLEDMode = 1; // Current LED output mode |
21 | byte NewLEDMode = 1; // New LED output mode after inout read |
22 | byte ChangedLEDMode = 1; // Flag: mode changed |
23 | |
24 | const byte LEDAmpel [6] = {9,10,11,12,13,14}; // LED pins Ampel1 R-Y-G Ampel2 R-Y-G |
25 | |
26 | const byte InputOnOff = 1; // Input pin for on/off |
27 | const byte InputNight = 0; // Input pin for day/night |
28 | |
29 | byte ReadingOnOff = HIGH; // Reading value from input OnOff |
30 | byte ReadingNight = HIGH; // Reading value from input OnOff |
31 | const byte InputOn = LOW; // Input Reading for ON |
32 | const byte InputOff = HIGH; // Input Reading for OFF |
33 | |
34 | const byte TimeStretch = 3; // overall delay stretch factor |
35 | byte TimeStretchable = 0; // Phase delay to be stretched? |
36 | int StretchedDelay = 0; // computed stretched delay |
37 | |
38 | //**************************************************
|
39 | //* *
|
40 | //* Mode as Array in Flash *
|
41 | //* *
|
42 | //**************************************************
|
43 | const byte NoOfModes = 3; // how many modes: arry dim 1 |
44 | const byte Folge_Laenge[NoOfModes] = {1, 8, 2}; // how many lines per mode |
45 | const byte Folge_Max_Laenge = 9; // how long is maximum mode: array dim 2 |
46 | |
47 | const byte LED_Folge[NoOfModes] [Folge_Max_Laenge] [8] PROGMEM = // {A1R,A1Y,A2G,A2R,A2G,DELAY in ticks of 0.1 sec ,Stretchable): array dim 3 |
48 | {
|
49 | //Mode 0: Aus
|
50 | {
|
51 | {Off, Off, Off, Off, Off, Off, 20, 0}, // 1.) Alles aus |
52 | },
|
53 | |
54 | // Mode 1: Normal
|
55 | {
|
56 | {On, Off, Off, On, Off, Off, 30, 0}, // 0.)beide Ampeln sind rot |
57 | {On, On, Off, On, Off, Off, 15, 0}, // 1.)Ampel 1 geht auf rot/gelb |
58 | {Off, Off, On, On, Off, Off, 30, 1}, // 2.)Ampel 1 geht auf grün |
59 | {Off, On, Off, On, Off, Off, 20, 0}, // 3.)Ampel 1 geht auf gelb |
60 | {On, Off, Off, On, Off, Off, 30, 0}, // 4.)beide Ampeln sind rot |
61 | {On, Off, Off, On, On, Off, 10, 0}, // 5.)Ampel 2 geht auf rot/gelb |
62 | {On, Off, Off, Off, Off, On, 30, 1}, // 6.)Ampel 2 geht auf grün |
63 | {On, Off, Off, Off, On, Off, 20, 0}, // 7.)Ampel 2 geht auf gelb |
64 | },
|
65 | // Mode 2: Blinken Gelb
|
66 | {
|
67 | {Off, On, Off, Off, On, Off, 5, 0}, // 0.) Beide Ampeln leuchten gelb |
68 | {Off, Off, Off, Off, Off, Off, 5, 0}, // 1.) Alles aus |
69 | },
|
70 | };
|
71 | |
72 | |
73 | //**************************************************
|
74 | //* *
|
75 | //* Function to read inputs and flag if a change *
|
76 | //* in mode has happened. Is called out of delay *
|
77 | //* *
|
78 | //**************************************************
|
79 | void ReadInput() |
80 | {
|
81 | ReadingOnOff = digitalRead(InputOnOff); // read first input |
82 | ReadingNight = digitalRead(InputNight); // read second input |
83 | |
84 | NewLEDMode = 1; // normal op |
85 | if(ReadingNight == InputOn) // if night mode |
86 | {
|
87 | NewLEDMode = 2; // then switch to mode 2 |
88 | }
|
89 | if(ReadingOnOff == InputOn) // if all off |
90 | {
|
91 | NewLEDMode = 0; // then switch to mode 0 |
92 | }
|
93 | |
94 | ChangedLEDMode = 1; // set change flag |
95 | if (CurrentLEDMode == NewLEDMode) ChangedLEDMode = 0; // has mode changed? if not clear flag |
96 | CurrentLEDMode = NewLEDMode; // overwrite current mode with new mode |
97 | }
|
98 | |
99 | //**************************************************
|
100 | //* *
|
101 | //* Delay routine, calls ReadInput() to see if *
|
102 | //* something changed and bails out if so *
|
103 | //* *
|
104 | //**************************************************
|
105 | |
106 | void breakbleDelay(int lengthDelay, int myTimeStretchable) |
107 | {
|
108 | if(myTimeStretchable ==1) StretchedDelay = 100 * lengthDelay * TimeStretch; else StretchedDelay = 100 * lengthDelay; // if interval stretchable, then scale & stretch, else only scale |
109 | for(int Counter = 0; Counter < StretchedDelay; Counter++) |
110 | {
|
111 | delay(1); // wait a millsec |
112 | ReadInput(); // read inputs |
113 | if(ChangedLEDMode == 1) break; // if something changed then break out of delay |
114 | }
|
115 | }
|
116 | |
117 | //**************************************************
|
118 | //* *
|
119 | //* Set up input and output pins. *
|
120 | //* IMPORTANT:Switch off pullup for inputs *
|
121 | //* if not, communication floats *
|
122 | //* *
|
123 | //**************************************************
|
124 | void setup() |
125 | {
|
126 | for(CurrentLED = 0 ; CurrentLED <= LEDcount ; CurrentLED +=1) // cycle through LEDs |
127 | {
|
128 | pinMode(LEDAmpel[CurrentLED], OUTPUT); // initialize the digital pin as an output. |
129 | digitalWrite(LEDAmpel[CurrentLED], LEDOn); // switch on all LEDs (shows cycle at powerup) |
130 | delay(GapTime); |
131 | digitalWrite(LEDAmpel[CurrentLED], LEDOff); // switch off all LEDs |
132 | }
|
133 | pinMode(InputOnOff, INPUT); // initialize the digital pin as an input: Set Mode 0; override |
134 | pinMode(InputNight, INPUT); // initialize the digital pin as an output |
135 | digitalWrite(InputOnOff, LOW); // Switch off pullup - IMPORTANT: if not, communication floats |
136 | digitalWrite(InputNight, LOW); // Switch off pullup - IMPORTANT: if not, communication floats |
137 | } // end setup |
138 | |
139 | //**************************************************
|
140 | //* *
|
141 | //* Main loop *
|
142 | //* *
|
143 | //* *
|
144 | //**************************************************
|
145 | void loop() |
146 | {
|
147 | for(CurrentPhase = 0; CurrentPhase < Folge_Laenge[CurrentLEDMode]; CurrentPhase++) // cycle through phases of current mode |
148 | {
|
149 | CurrentDelay = pgm_read_word_near(&LED_Folge[CurrentLEDMode][CurrentPhase][LEDcount]); // read delay time from flas |
150 | TimeStretchable = pgm_read_word_near(&LED_Folge[CurrentLEDMode][CurrentPhase][LEDcount +1]); // read stretchable flag from flash |
151 | for(CurrentLED = 0 ; CurrentLED < LEDcount ; CurrentLED +=1) // cycle through LEDs for current phase |
152 | {
|
153 | CurrentLEDState = pgm_read_word_near(&LED_Folge[CurrentLEDMode][CurrentPhase][CurrentLED]); // read LED state for cuurent mode/phase/LED from flash |
154 | digitalWrite(LEDAmpel[CurrentLED], CurrentLEDState); // set LED output accordingly |
155 | }
|
156 | breakbleDelay(CurrentDelay, TimeStretchable); // wait defined time and break on input |
157 | if(ChangedLEDMode == 1) break; // if input changed, then start afresh in new mode |
158 | }
|
159 | } // end main loop |
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.