Arduino-based two-way traffic light system

 

Student No.        231        

Name       Fufu        

Instructor: Zhao Zhipeng

   --July  2nd   _  _ __  

                                                     Training topic: traffic light system

1 System design

    1.  Design requirements
      1.  design task

   Design a two-way traffic light system based on Arduino to realize control and direction display functions

      1.  Performance Index Requirements

1. Basic requirements

(1) Independently complete the DF light status (red, yellow, green) according to the requirements of the topic

     (3) Switch one direction every 10 seconds, the yellow light starts flashing three times before the green light is on and ends

     (4) Display the current direction of travel through the dot matrix

     (5) Adjust the passing time through button control

     (6) Set the emergency state by pressing the button (all red lights are on)

     

2. Play part

  1. Set up sidewalk traffic lights and control them by buttons

2. During the emergency state, the buzzer can be used to alarm

3. Set the countdown time

1.2 Design ideas and design block diagram

1.2.1 Design ideas

First set the dot matrix to display the direction of traffic lights, make the DF light on the breadboard, and connect the remaining buttons and buzzer. Then set the on-off sequence of the lights (the three lights in the north and south directions are on and off according to the requirements), and then call the function sub-function to realize the button control of the passing time and the emergency state of the buzzer.

 1.2.2 Overall Design Block Diagram

2 Design of each module program

1. Compile and debug the main function. In this training, the main program is similar to the flickering process of a water lamp, and then connect and debug the breadboard. Burn the main program into the main control board through USB, and then test the function for debugging.

2. Write the program of each module, use the module function and library function that comes with Arduino itself, the program editing will be greatly improved. The alarm and control time are controlled by buttons. If you press the button, the red light will be on and the buzzer will sound. Release the button to return to the cycle; press the second button to adjust the passing time.

Figure 2-1 Program flow chart

4. Regarding the design of the program, the main key parts:

1. Array definition of dot matrix:

#include<LedControl.h>//关于点阵的头文件

LedControl lc=LedControl(13,11,12,1);

Emphasize the declaration of the Lattice function library, and Lattice can play a role.

2. Dot matrix clear screen:

  lc.shutdown(0,false);

  lc.setIntensity(0,3);

  lc.clearDisplay(0);

3. Function declaration:

int k1=0;
int k2=0;
int keypin1=10;
int keypin2=3;
int redpin = 4;
int yellowpin = 5;
int greenpin = 6;
int redpin2 = 7;
int yellowpin2 = 8;
int greenpin2 = 9;

void setup()
{
  lc.shutdown(0,false);
  lc.setIntensity(0,3);
  pinMode(keypin1,INPUT);
  pinMode(keypin2,INPUT);
  pinMode(redpin,OUTPUT);
  pinMode(yellowpin,OUTPUT);
  pinMode(greenpin,OUTPUT);
  pinMode(redpin2,OUTPUT);
  pinMode(yellowpin2,OUTPUT);
  pinMode(greenpin2,OUTPUT);
  }

3 Debugging process

The code is long and cumbersome, and there are some logical problems. The program code written to solve a problem is relatively long. All grammatical errors must be checked and eliminated, and the debugging process must be serious and focused.

Testing is the process of finding errors by running the program. Common errors include missing program braces, missing semicolons at the end, wrong use of Chinese symbols, wrong keywords, wrong comments, library files not loaded, wrong port selection, serial port occupied, The driver is not installed correctly, etc. Debugging is the process of determining the nature of errors found in testing and correcting them. Testing and debugging are usually carried out alternately, that is, testing-debugging-retesting-re-debugging.

Use as little test data as possible to find more errors, and select appropriate test data so that the system can execute each statement at runtime to test whether each instruction is correct. Test the operation of the program when legal data and illegal data are input. For legal data, the generality and specificity of the data should also be considered.

The test can be carried out according to the steps of module test, assembly test and confirmation test.

1. Module testing is the process of testing each module separately. After writing a module, it should be tested immediately, because the memory of the module is deep at this time, and a single module is relatively small, so it is easy to construct test data, and it is convenient to check and correct errors.

2. Assembly test is the process of gradually assembling and testing all modules (modules that should pass the module test) according to the pre-established plan. In general, data and control information should be transmitted between modules, so the main task of assembly testing is to find errors in the module interface.

3. Confirmation testing is the last step. Generally, before the software is delivered to the user, it should be confirmed whether the software meets the user's requirements. Usually, it is tested with a use case close to the actual situation.

When testing the program, the modularization of the Arduino program can be used to view the value of variables or expressions at any time, and sometimes part of the program code can be directly modified with modules to improve the correctness of the code and compress the scope of error codes.

4 Functional test

4.1 Test instruments and equipment

Connect the Arduino with the Led light and the dot matrix on the breadboard with a bread wire. After powering on, check the on-off status of each small light and dot matrix. If the circuit connection is correct, the light is on. If it is not on, re-adjust. Burn the main program into the development version, and test the operation of the main program. The light in the picture indicates that the operation is good, and the dot matrix starts to run when the light is on.

                        

          

4.2 Performance index test

The experiment realizes the on-light state of the traffic light (red, yellow, green), and switches a direction every 10 seconds. Before the green light turns on, the yellow light starts to flash three times and ends, and the current direction of traffic is displayed through the dot matrix. The control function of the two buttons is realized through the subroutine, the called function starts to cycle, and the button plays a control role. However, the buzzer sounded uncontrollably, and all the lights went out when the key was pressed in an emergency state, which may be due to the wrong sequence of statements in the module, or incomplete syntax. It has some traffic light functions.

4.3 Error Analysis

5 Experience from practical training

The Arduino experiment was carried out after the school started. Even if I learned the C language, it was only superficial. It is destined that this training will not be easy. The design of this training is based on Arduino's two-way traffic light system, which realizes control and direction display functions. In order to let us consolidate the textbook content of C language programming and the courses offered by Arduino experiments learned this semester, we will read the traffic light topic after we get it. I found it very difficult, and when I came back and started writing codes and connecting circuits, I realized that I couldn't solve more problems than I thought.

This training mainly uses the Arduino development version, 8*8 dot matrix, and breadboard, and combines the theoretical knowledge of C language with the actual operation process. This is the first time I have done it. I am not experienced enough and I am not careful enough. There are many problems in the operation, mainly because I don’t know how to do it. I found that I didn't understand some of the knowledge I had learned before, and I couldn't master it well.

In this training, it is not very difficult to connect the circuit. The most difficult thing is to write the code. I really have no way to start. I can only check the information and books on the Internet, and then ask my classmates. By comparing their design process, I think the modules in the books Optimized design thinking saves time and is less prone to errors. I also tried to use the Arduino graphical programming to practice, and I think that is also good, and I don't need to write the program myself. Later, I found my own shortcomings, and gradually changed my design ideas. I connected the development board, dot matrix, LED, buttons, and buzzer on the breadboard through the breadboard. First, set the direction of the dot matrix and then compile. From the LED lights in the north and south directions to the program controlled by the two buttons, write the code along the way. When the program was burned in and the light turned on, I felt extremely excited and proud, and very happy in my heart.

Through this training, I have a deeper understanding of the Arduino program and c program, and learned how to use it on the breadboard. Even though this training can't be done completely, I feel that I have gained a lot. I encountered many different problems during this process, which exposed my lack of knowledge. After my own thinking and hands-on operation, it finally became my own. own stuff. The practical training that I once thought was difficult has been done well now. In the future study and life, I must make unremitting efforts not to shrink back when encountering difficulties, but to find out the problem and solve it one by one. In the future, the theoretical knowledge I have learned must be combined with practice to improve my practical ability and independent thinking ability.

6 References

  1. "C Programming" / Tan Haoqiang - Tsinghua University Press, 2019.12
  2. Fundamentals of Arduino Programming (Second Edition)/Edited by Chen Luzhou-Beijing University of Aeronautics and Astronautics Press, 2015.3

appendix

Appendix 1: Simulation waveform diagram (some modules)

Appendix 2: Program List

#include"LedControl.h"
LedControl lc=LedControl(13,11,12,1);
byte d[8]={
B00001000,
B00000100,
B00000010,
B11111111,
B11111111,
B00000010,
B00000100,
B00001000,};
byte h[8]={
B00010000,
B00100000,
B01000000,
B11111111,
B11111111,
B01000000,
B00100000,
B00010000,};
int k1=0;
int k2=0;
int keypin1=10;
int keypin2=3;
int redpin = 4;
int yellowpin = 5;
int greenpin = 6;
int redpin2 = 7;
int yellowpin2 = 8;
int greenpin2 = 9;

void setup()
{
  lc.shutdown(0,false);
  lc.setIntensity(0,3);

lc.clearDisplay(0);
  pinMode(keypin1,INPUT);
  pinMode(keypin2,INPUT);
  pinMode(redpin,OUTPUT);
  pinMode(yellowpin,OUTPUT);
  pinMode(greenpin,OUTPUT);
  pinMode(redpin2,OUTPUT);
  pinMode(yellowpin2,OUTPUT);
  pinMode(greenpin2,OUTPUT);
  }
  void loop()
  {
    k2=digitalRead(keypin2);
  if(k2==LOW)
  {
    k1=digitalRead(keypin1);
      if(k1==LOW)
      {
        lc.clearDisplay(0);
        for(int row=0;row<8;row++)
        lc.setRow(0,row,h[row]);
      digitalWrite(redpin,HIGH);

 digitalWrite(greenpin2,HIGH);
    delay(7000);
      digitalWrite(redpin,LOW);
      digitalWrite(yellowpin,HIGH);
    delay(500);
      digitalWrite(yellowpin,LOW);
    delay(500);
      digitalWrite(yellowpin,HIGH);
    delay(500);
     digitalWrite(yellowpin,LOW);
    delay(500);
     digitalWrite(yellowpin,HIGH);
    delay(500);
     digitalWrite(yellowpin,LOW);
    delay(500);
        lc.clearDisplay(0);
        for(int row=0;row<8;row++)
        lc.setRow(0,row,d[row]);   
      digitalWrite(greenpin2,LOW);
      digitalWrite(greenpin,HIGH);
      digitalWrite(redpin,LOW);
      digitalWrite(redpin2,HIGH);
      digitalWrite(yellowpin,LOW);
    delay(7000);
      digitalWrite(redpin2,LOW);
     digitalWrite(yellowpin2,HIGH);
    delay(500);
      digitalWrite(yellowpin2,LOW);
    delay(500);
      digitalWrite(yellowpin2,HIGH);
    delay(500);
     digitalWrite(yellowpin2,LOW);
    delay(500);
     digitalWrite(yellowpin2,HIGH);
    delay(500);
     digitalWrite(yellowpin2,LOW);
    delay(500); 
      digitalWrite(yellowpin2,LOW); 
      digitalWrite(greenpin,LOW);
      }
 else if(k1==HIGH)
       {
        lc.clearDisplay(0);
        for(int row=0;row<8;row++)
        lc.setRow(0,row,h[row]);
      digitalWrite(redpin,HIGH); digitalWrite(greenpin2,HIGH);
    delay(15000);
      digitalWrite(redpin,LOW);
      digitalWrite(yellowpin,HIGH);
    delay(500);
      digitalWrite(yellowpin,LOW);
    delay(500);
      digitalWrite(yellowpin,HIGH);
    delay(500);
     digitalWrite(yellowpin,LOW);
    delay(500);
     digitalWrite(yellowpin,HIGH);
    delay(500);
     digitalWrite(yellowpin,LOW);
    delay(500);
        lc.clearDisplay(0);
        for(int row=0;row<8;row++)
        lc.setRow(0,row,d[row]);   
      digitalWrite(greenpin2,LOW);
      digitalWrite(greenpin,HIGH);
      digitalWrite(redpin,LOW);
      digitalWrite(redpin2,HIGH);
      digitalWrite(yellowpin,LOW);
    delay(15000);
      digitalWrite(redpin2,LOW);
     digitalWrite(yellowpin2,HIGH);
    delay(500);
      digitalWrite(yellowpin2,LOW);
    delay(500);
      digitalWrite(yellowpin2,HIGH);
    delay(500);
     digitalWrite(yellowpin2,LOW);
    delay(500);
     digitalWrite(yellowpin2,HIGH);
    delay(500);
     digitalWrite(yellowpin2,LOW);
    delay(500); 
      digitalWrite(yellowpin2,LOW); 
      digitalWrite(greenpin,LOW);
     }
      }
     else if(k2==HIGH)
     
       digitalWrite(redpin,HIGH);
       digitalWrite(redpin2,HIGH);
       delay(5000);
        digitalWrite(redpin,LOW);
       digitalWrite(redpin2,LOW);
     
    
   
    }



Guess you like

Origin blog.csdn.net/qq_57749830/article/details/126302032