Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Jakub,Kolodziejski
traffic-light-arduino
Commits
bd01373f
Commit
bd01373f
authored
Jun 20, 2019
by
Jakub,Kolodziejski
Browse files
Initial commit
parents
Pipeline
#124
canceled with stages
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
traffic-light-arduino.ino
0 → 100644
View file @
bd01373f
// Traffic light controller
// Date: 1/10/2019
// Author: Jakub Kolodziejski
#define NS_GREEN 12
#define NS_YELLOW 14
#define NS_RED 15
#define EW_GREEN 21
#define EW_YELLOW 22
#define EW_RED 23
#define STATUS_LED 13
// Built-in LED blink rate
#define LED_BLINK_INTERVAL 1000
unsigned
long
led_blink_next
=
0
;
// Traffic light intervals
unsigned
long
green_time
=
10000
;
// time (milliseconds) to stay green
unsigned
long
yellow_time
=
3000
;
// time (milliseconds) to stay yellow
unsigned
long
red_safety_time
=
2000
;
// time (milliseconds) to stay red in all directions for safety
unsigned
long
traffic_light_next
=
red_safety_time
;
int
state
=
0
;
unsigned
long
t_start
;
unsigned
long
t_now
;
unsigned
long
t_status
;
unsigned
long
t_dot
;
bool
auto_run
=
true
;
unsigned
long
uptime
=
0
;
// Function to reset the Arduino
void
(
*
resetArduino
)
(
void
)
=
0
;
//declare reset function @ address 0
// the setup routine runs once when you press reset:
void
setup
()
{
// initialize the serial connection
Serial
.
begin
(
115200
);
Serial
.
println
(
F
(
"
\n
# Serial started"
));
// initialize the light contols pins as an output.
pinMode
(
LED_BUILTIN
,
OUTPUT
);
pinMode
(
NS_RED
,
OUTPUT
);
pinMode
(
NS_YELLOW
,
OUTPUT
);
pinMode
(
NS_GREEN
,
OUTPUT
);
pinMode
(
EW_RED
,
OUTPUT
);
pinMode
(
EW_YELLOW
,
OUTPUT
);
pinMode
(
EW_GREEN
,
OUTPUT
);
// set all lights off
all_off
();
// initialize time keeping
t_now
=
millis
();
t_start
=
t_now
;
t_status
=
t_now
;
t_dot
=
t_now
;
Serial
.
println
(
F
(
"# Ready"
));
}
// the loop routine runs over and over again forever:
void
loop
()
{
// // Prototype for fixed interval occurence
// if (millis() >= foo_next or force_update) {
// foo_next += FOO_INTERVAL;
// // do stuff
// }
// Prototype for fixed interval occurence
if
(
auto_run
&&
millis
()
>=
traffic_light_next
)
{
state
=
(
state
<
6
)
?
state
+
1
:
1
;
// increment state or loop back to state 1
set_state
();
}
// Blink the built-in LED at a fixed rate only to indicate the code is running
if
(
millis
()
>=
led_blink_next
)
{
led_blink_next
+=
LED_BLINK_INTERVAL
;
toggle
(
LED_BUILTIN
);
// Serial.print(F("state: "));
// Serial.println(state);
}
// do stuff only when you receive data:
if
(
Serial
.
available
()
>
0
)
{
// read the incoming byte:
int
incomingByte
=
Serial
.
read
();
switch
(
incomingByte
)
{
case
'?'
:
help
();
break
;
case
'r'
:
resetArduino
();
break
;
case
'a'
:
Serial
.
println
(
F
(
"# Auto-run enabled"
));
auto_run
=
true
;
traffic_light_next
=
millis
();
break
;
case
'1'
:
case
'2'
:
case
'3'
:
case
'4'
:
case
'5'
:
case
'6'
:
auto_run
=
false
;
state
=
incomingByte
-
48
;
// -48 converts the ASCII to the proper integer
set_state
();
break
;
default:
Serial
.
println
(
F
(
"# Unrecognized command"
));
break
;
}
}
}
void
help
()
{
Serial
.
println
(
F
(
"# Command List"
));
Serial
.
println
(
F
(
"# ? - Print this help screen"
));
Serial
.
println
(
F
(
"# r - Reset the Arduino"
));
Serial
.
println
(
F
(
"# a - Run lights automatically"
));
Serial
.
println
(
F
(
"# 1 - State 1: NS red, EW red (safety delay)"
));
Serial
.
println
(
F
(
"# 2 - State 2: NS green, EW red"
));
Serial
.
println
(
F
(
"# 3 - State 3: NS yellow, EW red"
));
Serial
.
println
(
F
(
"# 4 - State 4: NS red, EW red (safety delay)"
));
Serial
.
println
(
F
(
"# 5 - State 5: NS red, EW green"
));
Serial
.
println
(
F
(
"# 6 - State 6: NS red, EW yellow"
));
}
void
all_off
()
{
digitalWrite
(
NS_RED
,
LOW
);
digitalWrite
(
NS_YELLOW
,
LOW
);
digitalWrite
(
NS_GREEN
,
LOW
);
digitalWrite
(
EW_RED
,
LOW
);
digitalWrite
(
EW_YELLOW
,
LOW
);
digitalWrite
(
EW_GREEN
,
LOW
);
}
void
on
(
int
pin
)
{
digitalWrite
(
pin
,
HIGH
);
}
void
toggle
(
int
pin
)
{
digitalWrite
(
pin
,
!
digitalRead
(
pin
));
}
void
set_state
()
{
switch
(
state
)
{
case
1
:
Serial
.
println
(
F
(
"# State 1: NS red, EW red (safety delay)"
));
traffic_light_next
+=
red_safety_time
;
all_off
();
on
(
NS_RED
);
on
(
EW_RED
);
break
;
case
2
:
Serial
.
println
(
F
(
"# State 2: NS green, EW red"
));
traffic_light_next
+=
green_time
;
all_off
();
on
(
NS_GREEN
);
on
(
EW_RED
);
break
;
case
3
:
Serial
.
println
(
F
(
"# State 3: NS yellow, EW red"
));
traffic_light_next
+=
yellow_time
;
all_off
();
on
(
NS_YELLOW
);
on
(
EW_RED
);
break
;
case
4
:
Serial
.
println
(
F
(
"# State 4: NS red, EW red (safety delay)"
));
traffic_light_next
+=
red_safety_time
;
all_off
();
on
(
NS_RED
);
on
(
EW_RED
);
break
;
case
5
:
Serial
.
println
(
F
(
"# State 5: NS red, EW green"
));
traffic_light_next
+=
green_time
;
all_off
();
on
(
NS_RED
);
on
(
EW_GREEN
);
break
;
case
6
:
Serial
.
println
(
F
(
"# State 6: NS red, EW yellow"
));
traffic_light_next
+=
yellow_time
;
all_off
();
on
(
NS_RED
);
on
(
EW_YELLOW
);
break
;
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment