digital-Write(PIN_RED, HIGH); // RED LED를 켭니다digitalWrite(PIN_YELLOW, LOW); // YELLOW LED를 끕니다digitalWrite(PIN_GREEN, LOW); // GREEN LED를 끕니다delay(RED_TIME); // RED_TIME으로 정의된 시간 동안 RED LED가 켜진 상태를 유지합니다
아두이노 UNO R4 코드
/* * 이 아두이노 우노 R4 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 우노 R4 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-uno-r4/arduino-uno-r4-traffic-light */#define PIN_RED 2 // The Arduino UNO R4 pin connected to R pin of traffic light module#define PIN_YELLOW 3 // The Arduino UNO R4 pin connected to Y pin of traffic light module#define PIN_GREEN 4 // The Arduino UNO R4 pin connected to G pin of traffic light module#define RED_TIME 4000 // RED time in millisecond#define YELLOW_TIME 4000 // YELLOW time in millisecond#define GREEN_TIME 4000 // GREEN time in millisecondvoidsetup() {pinMode(PIN_RED, OUTPUT);pinMode(PIN_YELLOW, OUTPUT);pinMode(PIN_GREEN, OUTPUT);}// the loop function runs over and over again forevervoidloop() {// red light ondigitalWrite(PIN_RED, HIGH); // turn ondigitalWrite(PIN_YELLOW, LOW); // turn offdigitalWrite(PIN_GREEN, LOW); // turn offdelay(RED_TIME); // keep red light on during a period of time// yellow light ondigitalWrite(PIN_RED, LOW); // turn offdigitalWrite(PIN_YELLOW, HIGH); // turn ondigitalWrite(PIN_GREEN, LOW); // turn offdelay(YELLOW_TIME); // keep yellow light on during a period of time// green light ondigitalWrite(PIN_RED, LOW); // turn offdigitalWrite(PIN_YELLOW, LOW); // turn offdigitalWrite(PIN_GREEN, HIGH); // turn ondelay(GREEN_TIME); // keep green light on during a period of time}
적절한 Arduino Uno R4 보드(예: Arduino Uno R4 WiFi) 및 COM 포트를 선택하세요.
코드를 복사하여 Arduino IDE에서 엽니다.
Arduino IDE에서 Upload 버튼을 클릭하여 코드를 Arduino UNO R4에 업로드하세요.
교통 신호등 모듈을 확인하세요.
신호등은 각 지역에서 사용되는 설계에 따라 다르게 작동합니다. 여기에서 제공되는 정보는 신호등이 어떻게 교통을 제어하는지에 대한 기본적인 아이디어를 제공합니다.
위에 표시된 코드는 각 조명을 개별적으로 제어할 수 있게 합니다. 이제 우리는 코드의 성능을 향상시키겠습니다.
Arduino UNO R4 코드 최적화
코드 개선을 위해 조명을 제어할 수 있는 기능을 추가합시다.
/* * 이 아두이노 우노 R4 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 우노 R4 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-uno-r4/arduino-uno-r4-traffic-light */#define PIN_RED 2 // The Arduino UNO R4 pin connected to R pin of traffic light module#define PIN_YELLOW 3 // The Arduino UNO R4 pin connected to Y pin of traffic light module#define PIN_GREEN 4 // The Arduino UNO R4 pin connected to G pin of traffic light module#define RED_TIME 2000 // RED time in millisecond#define YELLOW_TIME 1000 // YELLOW time in millisecond#define GREEN_TIME 2000 // GREEN time in millisecond#define RED 0 // Index in array#define YELLOW 1 // Index in array#define GREEN 2 // Index in arrayconstint pins[] = { PIN_RED, PIN_YELLOW, PIN_GREEN };constint times[] = { RED_TIME, YELLOW_TIME, GREEN_TIME };voidsetup() {pinMode(PIN_RED, OUTPUT);pinMode(PIN_YELLOW, OUTPUT);pinMode(PIN_GREEN, OUTPUT);}// the loop function runs over and over again forevervoidloop() {// red light on trafic_light_on(RED);delay(times[RED]); // keep red light on during a period of time// yellow light on trafic_light_on(YELLOW);delay(times[YELLOW]); // keep yellow light on during a period of time// green light on trafic_light_on(GREEN);delay(times[GREEN]); // keep green light on during a period of time}void trafic_light_on(int light) {for (int i = RED; i <= GREEN; i++) {if (i == light)digitalWrite(pins[i], HIGH); // turn onelsedigitalWrite(pins[i], LOW); // turn off }}
우리는 for 루프를 사용하여 코드를 더 좋게 만들 수 있습니다.
/* * 이 아두이노 우노 R4 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 우노 R4 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-uno-r4/arduino-uno-r4-traffic-light */#define PIN_RED 2 // The Arduino UNO R4 pin connected to R pin of traffic light module#define PIN_YELLOW 3 // The Arduino UNO R4 pin connected to Y pin of traffic light module#define PIN_GREEN 4 // The Arduino UNO R4 pin connected to G pin of traffic light module#define RED_TIME 2000 // RED time in millisecond#define YELLOW_TIME 1000 // YELLOW time in millisecond#define GREEN_TIME 2000 // GREEN time in millisecond#define RED 0 // Index in array#define YELLOW 1 // Index in array#define GREEN 2 // Index in arrayconstint pins[] = {PIN_RED, PIN_YELLOW, PIN_GREEN};constint times[] = {RED_TIME, YELLOW_TIME, GREEN_TIME};voidsetup() {pinMode(PIN_RED, OUTPUT);pinMode(PIN_YELLOW, OUTPUT);pinMode(PIN_GREEN, OUTPUT);}// the loop function runs over and over again forevervoidloop() {for (int light = RED; light <= GREEN; light ++) { trafic_light_on(light);delay(times[light]); // keep light on during a period of time }}void trafic_light_on(int light) {for (int i = RED; i <= GREEN; i ++) {if (i == light)digitalWrite(pins[i], HIGH); // turn onelsedigitalWrite(pins[i], LOW); // turn off }}
코드 개선을 위해 delay() 대신 millis() 함수를 사용합시다.
/* * 이 아두이노 우노 R4 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 우노 R4 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-uno-r4/arduino-uno-r4-traffic-light */#define PIN_RED 2 // The Arduino UNO R4 pin connected to R pin of traffic light module#define PIN_YELLOW 3 // The Arduino UNO R4 pin connected to Y pin of traffic light module#define PIN_GREEN 4 // The Arduino UNO R4 pin connected to G pin of traffic light module#define RED_TIME 2000 // RED time in millisecond#define YELLOW_TIME 1000 // YELLOW time in millisecond#define GREEN_TIME 2000 // GREEN time in millisecond#define RED 0 // Index in array#define YELLOW 1 // Index in array#define GREEN 2 // Index in arrayconstint pins[] = { PIN_RED, PIN_YELLOW, PIN_GREEN };constint times[] = { RED_TIME, YELLOW_TIME, GREEN_TIME };unsignedlong last_time = 0;int light = RED; // start with RED lightvoidsetup() {pinMode(PIN_RED, OUTPUT);pinMode(PIN_YELLOW, OUTPUT);pinMode(PIN_GREEN, OUTPUT); trafic_light_on(light); last_time = millis();}// the loop function runs over and over again forevervoidloop() {if ((millis() - last_time) > times[light]) { light++;if (light >= 3) light = RED; // new circle trafic_light_on(light); last_time = millis(); }// TO DO: your other code}void trafic_light_on(int light) {for (int i = RED; i <= GREEN; i++) {if (i == light)digitalWrite(pins[i], HIGH); // turn onelsedigitalWrite(pins[i], LOW); // turn off }}
동영상
비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.