Barobo
PortalSign In
HomeShopTutorialsFree CurriculumHour of Code
RoboBlockly for Learning Math and Coding

Traffic Light Control

Piano
Linkbot Image Mindstorm Image
0x2418126-6-12y2418126-6-12
Grid:
Tics Lines:
Width px
Hash Lines:
Width px
Labels:
Font px
Trace Lines:
Robot 1:
Width px
Robot 2:
Width px
Robot 3:
Width px
Robot 4:
Width px
Labels: x-axis y-axis Show Grid
Grid: 12x12 inches 24x24 inches 36x36 inches
72x72 inches 96x96 inches 192x192 inches
Quad: 4 Quadrants 1 Quadrant 1&4 Quadrants
Units: US Customary Metric
Background: Background Image

Robot 1

0
S
M
L
Initial Position:
( in, in)
Initial Angle:
deg
Current Position: (0 in, 0 in)
Current Angle: 90 deg
Wheel Radius:
Track Width:
in

Robot 2

0
S
M
L
Initial Position:
( in, in)
Initial Angle:
deg
Current Position: (6 in, 0 in)
Current Angle: 90 deg
Wheel Radius:
Track Width:
in

Robot 3

0
S
M
L
Initial Position:
( in, in)
Initial Angle:
deg
Current Position: (12 in, 0 in)
Current Angle: 90 deg
Wheel Radius:
Track Width:
in

Robot 4

0
S
M
L
Initial Position:
( in, in)
Initial Angle:
deg
Current Position: (18 in, 0 in)
Current Angle: 90 deg
Wheel Radius:
Track Width:
in
Traffic Light Control
/* Code generated by RoboBlockly v2.5 */
/* Problem Statement: 
    Control the traffic light with switch. If the button is
    off, it will only display the right light. If we push the
    button, turn off the red light and traffic light turns
    green for 3 seconds, then turns yellow for 1 second.
    */
#include <arduino.h>
#include <stdbool.h>
double switchState;

switchState = 0;
// Set the LED pins to output mode
// red
pinMode(5, OUTPUT);
// yellow
pinMode(4, OUTPUT);
// green
pinMode(3, OUTPUT);
// Set the switch pin to input mode
pinMode(2, INPUT);
while(true) {
  // Read the state of the switch
  switchState = digitalRead(2);
  // If the button is not pushed, turn the red LED ON and everything else OFF
  if (switchState == 0) {
    // red light
    digitalWrite(5, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(3, LOW);
  }
  else {
    // If the button is pushed, traffic turns green for 3
    // seconds, then turns yellow for 1 second, then red
    // red OFF
    digitalWrite(5, LOW);
    // green light
    digitalWrite(3, HIGH);
    delay(3000);
    digitalWrite(3, LOW);
    // yellow light
    digitalWrite(4, HIGH);
    delay(1000);
    digitalWrite(4, LOW);
  }
}
Load Blocks
Hardware
LArduino
Workspace Show Ch Save File
Problem Statement:

Control the traffic light with switch. If the button is off, it will only display the right light. If we push the button, turn off the red light and traffic light turns green for 3 seconds, then turns yellow for 1 second.


		
Rubbish bin
Time