The \"traffic light\" (microprogramming) using Java Logisim., This assignment is
ID: 3603037 • Letter: T
Question
The "traffic light" (microprogramming) using Java Logisim.,This assignment is intended to introduce you to the concept of microprogramming -- storing a sequence of zeros and ones in a ROM and sending synchronous signals to control something, be it traffic lights.
1. Maintain the current timings as much as possible. You will see the sequence of lights as follows for the North-South/East-West traffic lights: Red/Red (longs for safetty), Red/Green (long), Red/Yellow (short), Red/Red (safety), Green/Red (long), Yellow/Red (short) and REPEAT. The walk/don't walk lights are nor programmed and will remain orange (don't walk). Fix this first. During the safety combination of Red/Red, they should remain orange (don't walk). However, when East-West has a green light, it is safe to turn on the North-South (walk) light by outputting a 1, whereupon it will turn white.
2. As a second task (not for additional credit, just part of the assignment) change the major sequence as in (1) to match the European system. The only change is this: The light about to go green is immediately preceeded by a short, single tick period where BOTH red AND yellow are lit as a signal to prepare to move. So, change the sequence to Red/Red (long safety), Red/Red_&_yellow (short), Red/Green (long), Red/Yellow (short), Red/Red (safety), Red_&_Yellow/Red (short), Green/Red (long), Yellow/Red (short) and REPEAT.
The counter will drive the address lines of the ROM through 16 addresses (0 through 15) and a byte of data will by output from each address. I suggest that you make a table with columns for (a) the address, (b) the required display in words, (c) the binary pattern required to make the display in (b), the two hexadecimal digits corresponding to the binary pattern. Be organized and this will not take long at all.
The "traffic light" (microprogramming) using Java Logisim.,
This assignment is intended to introduce you to the concept of microprogramming -- storing a sequence of zeros and ones in a ROM and sending synchronous signals to control something, be it traffic lights.
1. Maintain the current timings as much as possible. You will see the sequence of lights as follows for the North-South/East-West traffic lights: Red/Red (longs for safetty), Red/Green (long), Red/Yellow (short), Red/Red (safety), Green/Red (long), Yellow/Red (short) and REPEAT. The walk/don't walk lights are nor programmed and will remain orange (don't walk). Fix this first. During the safety combination of Red/Red, they should remain orange (don't walk). However, when East-West has a green light, it is safe to turn on the North-South (walk) light by outputting a 1, whereupon it will turn white.
2. As a second task (not for additional credit, just part of the assignment) change the major sequence as in (1) to match the European system. The only change is this: The light about to go green is immediately preceeded by a short, single tick period where BOTH red AND yellow are lit as a signal to prepare to move. So, change the sequence to Red/Red (long safety), Red/Red_&_yellow (short), Red/Green (long), Red/Yellow (short), Red/Red (safety), Red_&_Yellow/Red (short), Green/Red (long), Yellow/Red (short) and REPEAT.
The counter will drive the address lines of the ROM through 16 addresses (0 through 15) and a byte of data will by output from each address. I suggest that you make a table with columns for (a) the address, (b) the required display in words, (c) the binary pattern required to make the display in (b), the two hexadecimal digits corresponding to the binary pattern. Be organized and this will not take long at all.
The "traffic light" (microprogramming) using Java Logisim.,
This assignment is intended to introduce you to the concept of microprogramming -- storing a sequence of zeros and ones in a ROM and sending synchronous signals to control something, be it traffic lights.
1. Maintain the current timings as much as possible. You will see the sequence of lights as follows for the North-South/East-West traffic lights: Red/Red (longs for safetty), Red/Green (long), Red/Yellow (short), Red/Red (safety), Green/Red (long), Yellow/Red (short) and REPEAT. The walk/don't walk lights are nor programmed and will remain orange (don't walk). Fix this first. During the safety combination of Red/Red, they should remain orange (don't walk). However, when East-West has a green light, it is safe to turn on the North-South (walk) light by outputting a 1, whereupon it will turn white.
2. As a second task (not for additional credit, just part of the assignment) change the major sequence as in (1) to match the European system. The only change is this: The light about to go green is immediately preceeded by a short, single tick period where BOTH red AND yellow are lit as a signal to prepare to move. So, change the sequence to Red/Red (long safety), Red/Red_&_yellow (short), Red/Green (long), Red/Yellow (short), Red/Red (safety), Red_&_Yellow/Red (short), Green/Red (long), Yellow/Red (short) and REPEAT.
The counter will drive the address lines of the ROM through 16 addresses (0 through 15) and a byte of data will by output from each address. I suggest that you make a table with columns for (a) the address, (b) the required display in words, (c) the binary pattern required to make the display in (b), the two hexadecimal digits corresponding to the binary pattern. Be organized and this will not take long at all.
Explanation / Answer
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
public class TrafficLight extends JFrame implements ActionListener {
JButton b1, b2, b3;
Signal green = new Signal(Color.green);
Signal yellow = new Signal(Color.yellow);
Signal red = new Signal(Color.red);
public TrafficLight(){
super("Traffic Light");
getContentPane().setLayout(new GridLayout(2, 1));
b1 = new JButton("Red");
b2 = new JButton("Yellow");
b3 = new JButton("Green");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
green.turnOn(false);
yellow.turnOn(false);
red.turnOn(true);
JPanel p1 = new JPanel(new GridLayout(3,1));
p1.add(red);
p1.add(yellow);
p1.add(green);
JPanel p2 = new JPanel(new FlowLayout());
p2.add(b1);
p2.add(b2);
p2.add(b3);
getContentPane().add(p1);
getContentPane().add(p2);
pack();
}
public static void main(String[] args){
TrafficLight tl = new TrafficLight();
tl.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == b1){
green.turnOn(false);
yellow.turnOn(false);
red.turnOn(true);
} else if (e.getSource() == b2){
yellow.turnOn(true);
green.turnOn(false);
red.turnOn(false);
} else if (e.getSource() == b3){
red.turnOn(false);
yellow.turnOn(false);
green.turnOn(true);
}
}
}
class Signal extends JPanel{
Color on;
int radius = 40;
int border = 10;
boolean change;
Signal(Color color){
on = color;
change = true;
}
public void turnOn(boolean a){
change = a;
repaint();
}
public Dimension getPreferredSize(){
int size = (radius+border)*2;
return new Dimension( size, size );
}
public void paintComponent(Graphics g){
g.setColor( Color.black );
g.fillRect(0,0,getWidth(),getHeight());
if (change){
g.setColor( on );
} else {
g.setColor( on.darker().darker().darker() );
}
g.fillOval( border,border,2*radius,2*radius );
}
}
Code :
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class traffic extends Applet implements Runnable
{
Thread t;
Font f,f1;
int i=0,a=0,j=0;
public void init(){
setBackground(Color.lightGray);
f=new Font("TimesNewRoman",f.ITALIC,28);
f1=new Font("TimesNewRoman",Font.ITALIC+Font.BOLD,28);
}
public void start()
{
t=new Thread(this);
t.start();
}
public void run()
{
for(i=25;i>=0;i--)//countdown
{
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
if(i<=25 && i>3)//red
{
a=1;
repaint();
}
else
if(i<=3 && i>0)//yelloe
{
a=2;
repaint();
}
else
if(i==0)//green
{
for(j=0;j<25;j++)
{
a=3;
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
repaint();
}
if(j==25)//end of green(return to red)
{
run();
}
}
}
repaint();
}
public void paint(Graphics g)
{
setBackground(Color.lightGray);//ROAD
g.setColor(Color.black);//pole top
g.fillArc(100,150,100,100,0,180);
g.drawArc(100,150,100,100,0,180);
g.setColor(Color.black);//POLE UP
g.fillRect(150,150,50,150);
g.drawRect(150,150,50,150);
g.setColor(Color.black);//POLE DOWN
g.fillRect(165,300,20,155);
g.drawRect(165,300,20,155);
g.drawOval(150,150,50,50);//RED
g.drawOval(150,200,50,50);//YELLOW
g.drawOval(150,250,50,50);//GREEN
g.setColor(Color.red);//COUNTDOWN STOP
g.setFont(f);
g.drawString(""+i,50,50);
g.setColor(Color.white);//CROSSING1
g.fillRect(300,5,15,125);
g.drawRect(300,5,15,125);
g.setColor(Color.white);
g.fillRect(300,145,15,135);
g.drawRect(300,145,15,135);
g.setColor(Color.white);
g.fillRect(300,300,15,135);
g.drawRect(300,300,15,135);
g.setColor(Color.white);//CROSSING2
g.fillRect(450,5,15,125);
g.drawRect(450,5,15,125);
g.setColor(Color.white);
g.fillRect(450,145,15,135);
g.drawRect(450,145,15,135);
g.setColor(Color.white);
g.fillRect(450,300,15,135);
g.drawRect(450,300,15,135);
g.setColor(Color.black);//TREE1DOWN
g.fillRect(600,300,15,135);
g.drawRect(600,300,15,135);
g.setColor(Color.green);//TREE1UP
g.fillArc(560,290,100,100,0,180);
g.drawArc(560,290,100,100,0,180);
g.setColor(Color.black);//TREE2DOWN
g.fillRect(460,300,15,135);
g.drawRect(460,300,15,135);
g.setColor(Color.green);//TREE2UP
g.fillArc(420,290,100,100,0,180);
g.drawArc(420,290,100,100,0,180);
if(a==1)//REDSIGNAL
{
g.setColor(Color.red);
g.fillOval(150,150,50,50);
g.drawOval(150,150,50,50);
g.drawString("STOP",50,150);
}
if(a==2)//YELLOWSIGNAL
{
g.setColor(Color.yellow);
g.fillOval(150,200,50,50);
g.drawOval(150,200,50,50);
g.drawString("READY",50,200);
}
if(a==3)//GREENSIGNAL
{
g.setColor(Color.blue);//countdown
g.setFont(f);
g.drawString(""+j,150,50);
g.setColor(Color.green);
g.fillOval(150,250,50,50);
g.drawOval(150,250,50,50);
g.drawString("GO",50,250);
}
int x1[]={220,300,300,280};
int y1[]={250,150,250,150};
int n1=4;
int n2=3;
int x2[]={340,380,380};
int y2[]={150,100,150};
int x3[]={460,460,500};
int y3[]={150,100,150};
g.setColor(Color.black);
g.fillPolygon(x1,y1,n1);
g.drawPolygon(x1,y1,n1);
g.setColor(Color.yellow);
g.fillRect(380,100,80,50);
g.drawRect(380,100,80,50);
g.setColor(Color.yellow);
g.fillPolygon(x2,y2,n2);
g.drawPolygon(x2,y2,n2);
g.setColor(Color.yellow);
g.fillPolygon(x3,y3,n2);
g.drawPolygon(x3,y3,n2);
g.setColor(Color.black);
g.fillOval(440,210,60,60);
g.drawOval(440,210,60,60);
g.setColor(Color.black);
g.fillOval(340,210,60,60);
g.drawOval(340,210,60,60);
g.setColor(Color.red);
g.fillRect(300,150,250,100);
g.drawRect(300,150,250,100);
g.setColor(Color.black);
g.setFont(f1);
g.drawString ("Zumbo",380,200);
}
}
import java.awt.*;
import javax.swing.*;
public class Trafficlight {
public static void main(String[] args) throws InterruptedException {
JFrame frame = initializeFrame();
Light l1 = new Light();
frame.add(l1);
while (true) {
l1.incrementValue();
Thread.sleep(500);
l1.swapEm();
}
}
private static JFrame initializeFrame() {
JFrame frame = new JFrame();
frame.setTitle("Trafficligth");
frame.setSize(250, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
return frame;
}
static class Light extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private int iValue = 0;
private int iDistance=90;
private int topLightHeight=10;
private Color iRed = Color.red;
private Color iYellow = Color.black;
private Color iGreen = Color.black;
Light() {
}
public void paintComponent(Graphics g) {
defineLight(g);
}
public int getValue() {
return this.iValue;
}
public void setLighDistance(int distance) {
this.iDistance = distance;
}
public void incrementValue() {
if (this.iValue == 25) {
this.iValue = -1;
}
this.iValue = this.iValue + 1;
}
public void swapEm() {
if (this.iValue == 0) {
this.iYellow = Color.black;
this.iRed = Color.red;
}
if (this.iValue == 10) {
this.iRed = Color.black;
this.iYellow = Color.yellow;
}
if (this.iValue == 15) {
this.iRed = Color.black;
this.iYellow = Color.black;
this.iGreen = Color.green;
}
if (this.iValue == 20) {
this.iGreen = Color.black;
this.iYellow = Color.yellow;
}
repaint();
}
public void defineLight(Graphics g) {
g.setColor(this.iRed);
g.fillRoundRect(80, this.topLightHeight, 80, 80, 70, 70);
g.setColor(this.iYellow);
g.fillRoundRect(80, this.topLightHeight+this.iDistance, 80, 80, 70, 70);
g.setColor(this.iGreen);
g.fillRoundRect(80, this.topLightHeight+2*this.iDistance, 80, 80, 70, 70);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.