学院 |
班级 |
学号 |
|||
姓名 |
日期 |
成绩 |
|||
实验题目 |
GPIO亮灯 |
||||
实验目的 |
点亮三个灯闪烁频率为一秒 |
||||
硬件原理 |
无 |
||||
关键代码及注释 |
const int ledPin1 = GREEN_LED; // the number of the LED pin const int ledPin2 = YELLOW_LED; const int ledPin3 = RED_LED; // Variables will change: int ledState = LOW; // ledState used to set the LED int ledState1 = LOW; int ledState2 = LOW; int ledState3 = LOW; int count1=0; long previousMillis = 0; // will store last time LED was updated long term = 0; // the follow variables is a long because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long interval = 1000; // interval at which to blink (milliseconds) void setup() { // set the digital pin as output: pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); } void loop() { // here is where you'd put code that needs to be running all the time. // check to see if it's time to blink the LED; that is, if the // difference between the current time and last time you blinked // the LED is bigger than the interval at which you want to // blink the LED. unsigned long currentMillis = millis();
if(count1<3 ||(count1>=8 &&count1<11)){ if(currentMillis - previousMillis > interval) { // save the last time you blinked the LED previousMillis = currentMillis; // if the LED is off turn it on and vice-versa: if (ledState == LOW) ledState = HIGH; else{ ledState = LOW; count1+=1; } // set the LED with the ledState of the variable: digitalWrite(ledPin1, ledState); digitalWrite(ledPin2, ledState); digitalWrite(ledPin3, ledState); } } if(count1>=3 && count1<8){ if(currentMillis - previousMillis > interval) { previousMillis = currentMillis; if (ledState1 == LOW && ledState2 == LOW && ledState3 == LOW) ledState1 = HIGH; else if(ledState1 ==HIGH){ ledState1 = LOW; ledState2 = HIGH; } else if(ledState2 == HIGH){ ledState2 = LOW; ledState3 = HIGH; count1+=1; //最后一个灯闪过之后,count+1 } else if(ledState3 == HIGH){ ledState3 = LOW; ledState1 = HIGH; } digitalWrite(ledPin1, ledState1); digitalWrite(ledPin2, ledState2); digitalWrite(ledPin3, ledState3); } } } |
||||
实验步骤 |
|
||||
实验结果 |
|||||
思考与反馈 |
无 |