====== Программирования на C++ в среде Arduino ======
===== Traffic lights example (модифицированная версия) =====
/*
* Traffic lights example
*
* Red light to pin 7
* Yellow light to pin 6
* Green light to pin 5
*
* Pedestrian Red light to pin 4
* Pedestrian Green light to pin 3
* Pedestrian pushbutton pin 2 (it is connected to the GND, therefore internal PULLUP resistor must be ON)
*
* Arduino IDE 1.6.12
*/
// Pins
int red = 7, yellow = 6, green = 5;
int p_red = 4, p_green = 3;
int button = 2;
// System variables
byte state = 0; // initial state
unsigned long i = 1; // system counter
unsigned int del = 100; // system delay legnth
boolean flag = false;
void change_traffic_lights(uint8_t r,uint8_t y, uint8_t g){
digitalWrite(red, r);
digitalWrite(yellow, y);
digitalWrite(green, g);
}
void change_pedastrian_lights(uint8_t r,uint8_t g){
digitalWrite(p_red, r);
digitalWrite(p_green, g);
}
void setup() {
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(p_red, OUTPUT);
pinMode(p_green, OUTPUT);
pinMode(button, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// Check button
if(digitalRead(button) == 0) {
flag = true;
}
delay(del);
switch(state){
case 0:
change_traffic_lights(0,0,1);
change_pedastrian_lights(1,0);
if(flag){
if((i%30)==0){
flag = false;
Serial.print("case 0, state = 3 ");Serial.println(flag);
state = 3;
i = 1;
}
}
break;
case 1:
change_traffic_lights(1,1,0);
change_pedastrian_lights(1,0);
if((i%30)==0){
Serial.print("case 1, state = 0 ");Serial.println(flag);
state = 0;
flag = false;
i = 1;
}
break;
case 2:
change_traffic_lights(1,0,0);
change_pedastrian_lights(0,1);
if((i%100)==0){
Serial.print("case 2, state = 1 ");Serial.println(flag);
state = 1;
flag = false;
i = 1;
}
break;
case 3:
change_traffic_lights(0,1,0);
change_pedastrian_lights(1,0);
if((i%20)==0){
Serial.print("case 3, state = 2 ");Serial.println(flag);
state = 2;
flag = false;
i = 1;
}
break;
default:
break;
}
i++;
}
===== Traffic lights example (без использования блокировочных функций delay()) =====
/*
* Traffic lights example
*
* Red light to pin 7
* Yellow light to pin 6
* Green light to pin 5
*
* Pedestrian Red light to pin 4
* Pedestrian Green light to pin 3
* Pedestrian pushbutton pin 2 (it is connected to the GND, therefore internal PULLUP resistor must be ON)
*
* Arduino IDE 1.6.12
*/
// Pins
int red = 7, yellow = 6, green = 5;
int p_red = 4, p_green = 3;
int button_1 = 2;
int butState_1 = HIGH;
int butDelay = 10;
int checkButton_1 = 0;
uint32_t prevButt_time_1 = 0;
uint32_t prevTraffic_time = 0;
// System variables
byte state = 0; // initial state
unsigned long i = 1; // system counter
unsigned int del = 100; // system delay legnth
boolean flag = false;
void change_traffic_lights(uint8_t r,uint8_t y, uint8_t g){
digitalWrite(red, r);
digitalWrite(yellow, y);
digitalWrite(green, g);
}
void change_pedastrian_lights(uint8_t r,uint8_t g){
digitalWrite(p_red, r);
digitalWrite(p_green, g);
}
uint8_t checkButton(){
uint8_t newButtonState = digitalRead(button_1);
if(newButtonState != butState_1){
Serial.print("newButtonState = "); Serial.println(newButtonState);
return true;
} else {
return false;
}
}
void setup() {
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(p_red, OUTPUT);
pinMode(p_green, OUTPUT);
pinMode(button_1, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// Check button
switch(state){
case 0:
change_traffic_lights(0,0,1);
change_pedastrian_lights(1,0);
if(checkButton()){
prevTraffic_time = millis();
state = 5;
Serial.println("state = 0; ");
}
break;
case 5:
change_traffic_lights(0,0,1);
change_pedastrian_lights(1,0);
if(millis() - prevTraffic_time >= 3000){
prevTraffic_time = millis();
state = 3;
Serial.println("state = 5; ");
}
break;
case 1:
change_traffic_lights(1,1,0);
change_pedastrian_lights(1,0);
if(millis() - prevTraffic_time >= 3000){
prevTraffic_time = millis();
Serial.println("state = 1; ");
state = 0;
}
break;
case 2:
change_traffic_lights(1,0,0);
change_pedastrian_lights(0,1);
if(millis() - prevTraffic_time >= 5000){
prevTraffic_time = millis();
Serial.println("state = 2; ");
state = 1;
}
break;
case 3:
change_traffic_lights(0,1,0);
change_pedastrian_lights(1,0);
if(millis() - prevTraffic_time >= 2000){
prevTraffic_time = millis();
Serial.println("state = 3; ");
state = 2;
}
break;
default:
break;
}
}