Bläddra i källkod

initial commit

Michael Tang 1 år sedan
incheckning
c7ddef448f
11 ändrade filer med 634 tillägg och 0 borttagningar
  1. 0 0
      README.md
  2. 20 0
      _01_blinkingLed.ino
  3. 35 0
      _02_btnAndLed02.ino
  4. 30 0
      _03_flowingLed.ino
  5. 30 0
      _04_breathingLed.ino
  6. 49 0
      _05_rgbLed.ino
  7. 158 0
      _06_segment.ino
  8. 53 0
      _07_LCD1602.ino
  9. 48 0
      _08_photoresistance.ino
  10. 40 0
      _09_serial.ino
  11. 171 0
      _10_motor.ino

+ 0 - 0
README.md


+ 20 - 0
_01_blinkingLed.ino

@@ -0,0 +1,20 @@
+/***********************************************************
+File name:   01_blinkingLed.ino
+Description:  Lit LED, let LED blinks.
+Website: www.adeept.com
+E-mail: support@adeept.com
+Author: Tom
+Date: 2015/05/02 
+***********************************************************/
+int ledPin=8; //definition digital 8 pins as pin to control the LED
+void setup()
+{
+    pinMode(ledPin,OUTPUT);    //Set the digital 8 port mode, OUTPUT: Output mode
+}
+void loop()
+{  
+    digitalWrite(ledPin,HIGH); //HIGH is set to about 5V PIN8
+    delay(1000);               //Set the delay time, 1000 = 1S
+    digitalWrite(ledPin,LOW);  //LOW is set to about 5V PIN8
+    delay(1000);               //Set the delay time, 1000 = 1S
+} 

+ 35 - 0
_02_btnAndLed02.ino

@@ -0,0 +1,35 @@
+/***********************************************************
+File name: 02_btnAndLed02.ino
+Description: Using interrupt mode, every time you press the 
+             button, LED status is switched(ON->OFF,OFF->ON).
+Website: www.adeept.com
+E-mail: support@adeept.com
+Author: Tom
+Date: 2015/05/02 
+***********************************************************/
+
+int ledpin=11;            //definition digital 11 pins as pin to control the LED
+int btnpin=2;             //Set the digital 2 to button interface 
+volatile int state = LOW; //Defined output status LED Interface
+
+void setup()
+{                
+  pinMode(ledpin, OUTPUT);                  //Set digital 11 port mode, the OUTPUT for the output
+  attachInterrupt(0, stateChange, FALLING); //Monitoring Interrupt 0 (Digital PIN 2) changes in the input pins FALLING
+}
+void loop()                     
+{
+  digitalWrite(ledpin, state);              //Output control status LED, ON or OFF 
+}
+void stateChange()                          //Interrupt function
+{
+  if(digitalRead(btnpin)==LOW)              //Detection button interface to low
+  { 
+   
+      delayMicroseconds(10000);             //Delay 10ms for the elimination of key leading-edge jitter
+      if(digitalRead(btnpin)==LOW)          //Confirm button is pressed
+      {
+          state = !state;                   //Negate operation, each time you run the program here, state the HGIH becomes LOW, or the state becomes the LOW HGIH.
+      }
+   }
+}

+ 30 - 0
_03_flowingLed.ino

@@ -0,0 +1,30 @@
+/***********************************************************
+File name: 03_flowingLed.ino
+Description: LED turn lighting control
+Website: www.adeept.com
+E-mail: support@adeept.com
+Author: Tom
+Date: 2015/05/02 
+***********************************************************/
+
+void setup()
+{ 
+  unsigned char ledPin;           //ledPin will be set to 1,2,3,4,5,6, 7 and 8.
+  for(ledPin=1;ledPin<=8;ledPin++)//In turn set 1 ~ 8 digital pins to output mode 
+  pinMode(ledPin,OUTPUT);         //Set the  ledPin pin to output mode 
+}
+
+void loop()
+{   
+  unsigned char ledPin;           //ledPin will be set to 1,2,3,4,5,6, 7 and 8.
+  for(ledPin=1;ledPin<=8;ledPin++)//Every 200ms on in order LED1 ~ 8 
+  {
+    digitalWrite(ledPin,HIGH);    //led on
+    delay(200);                   //Delay 200 ms
+  }
+  for(ledPin=1;ledPin<=8;ledPin++)//Every 200ms off in order LED1 ~ 8 
+  {
+    digitalWrite(ledPin,LOW);     //led off
+    delay(200);                   //Delay 200 ms
+  } 
+}

+ 30 - 0
_04_breathingLed.ino

@@ -0,0 +1,30 @@
+/***********************************************************
+File name: 04_breathingLed.ino
+Description: PWM control the LED gradually from dark to 
+             brighter, then from brighter to dark
+Website: www.adeept.com
+E-mail: support@adeept.com
+Author: Tom
+Date: 2015/05/02 
+***********************************************************/
+int ledpin=11; //definition digital 11 pins as pin to control the LED
+
+void setup ()
+{
+  pinMode(ledpin,OUTPUT); //Set digital 11 port mode, the OUTPUT for the output
+}
+ 
+void loop()
+{
+   for (int a=0; a<=255;a++)  //Loop, PWM control of LED brightness increase
+   {
+     analogWrite(ledpin,a);   //PWM output value a (0~255)
+     delay(15);                //The duration of the current brightness level. 15ms           
+   }
+   for (int a=255; a>=0;a--)  //Loop, PWM control of LED brightness Reduced
+   {
+     analogWrite(ledpin,a);   //PWM output value a (255~0)
+     delay(15);                //The duration of the current brightness level. 15ms 
+   }
+   delay(100);                //100ms delay
+}

+ 49 - 0
_05_rgbLed.ino

@@ -0,0 +1,49 @@
+                                                  /***********************************************************
+File name: 05_rgbLed.ino
+Description:Control the RGB LED emitting red, green, blue, yellow,
+            white and purple light, then the RGB LED will be off,
+            each state continues 1s, after repeating the above 
+            procedure.   
+Website: www.adeept.com
+E-mail: support@adeept.com
+Author: Tom
+Date: 2015/05/02 
+*************************************************************/
+int redPin = 11;   // R petal on RGB LED module connected to digital pin 11 
+int greenPin = 10; // G petal on RGB LED module connected to digital pin 9 
+int bluePin = 9;   // B petal on RGB LED module connected to digital pin 10 
+void setup()    
+{   
+   pinMode(redPin, OUTPUT);   // sets the redPin to be an output 
+   pinMode(greenPin, OUTPUT); // sets the greenPin to be an output 
+   pinMode(bluePin, OUTPUT);  // sets the bluePin to be an output 
+}    
+void loop()  // run over and over again  
+{    
+    // Basic colors:  
+    color(255, 0, 0); // turn the RGB LED red  
+    delay(1000);      // delay for 1 second  
+    color(0,255, 0);  // turn the RGB LED green  
+    delay(1000);      // delay for 1 second  
+    color(0, 0, 255); // turn the RGB LED blue
+    delay(1000);      // delay for 1 second  
+  
+    // Example blended colors:  
+    color(255,255,0);   // turn the RGB LED yellow   
+    delay(1000);        // delay for 1 second  
+    color(255,255,255); // turn the RGB LED white  
+    delay(1000);        // delay for 1 second  
+    color(128,0,255);   // turn the RGB LED purple  
+     delay(1000);       // delay for 1 second  
+     color(0,0,0);      // turn the RGB LED off  
+     delay(1000);       // delay for 1 second  
+}     
+     
+void color (unsigned char red, unsigned char green, unsigned char blue)// the color generating function  
+{    
+     analogWrite(redPin, 255-red);     // PWM signal output   
+     analogWrite(greenPin, 255-green); // PWM signal output
+     analogWrite(bluePin, 255-blue);   // PWM signal output
+}     
+
+

+ 158 - 0
_06_segment.ino

@@ -0,0 +1,158 @@
+/***********************************************************
+File name: 06_segment.ino
+Description:Segment display Numbers 0 to 9, each digital display 
+            1 second.After the cycle show
+Website: www.adeept.com
+E-mail: support@adeept.com
+Author: Tom
+Date: 2015/05/02 
+***********************************************************/
+
+int a=7; //definition digital 7  pins as pin to control 'a' section(segment)
+int b=6; //definition digital 6  pins as pin to control 'b' section(segment)
+int c=4; //definition digital 4  pins as pin to control 'c' section(segment)
+int d=11;//definition digital 11 pins as pin to control 'd' section(segment)
+int e=10;//definition digital 10 pins as pin to control 'e' section(segment)
+int f=8; //definition digital 8  pins as pin to control 'f' section(segment)
+int g=9; //definition digital 9  pins as pin to control 'g' section(segment)
+int dp=5;//definition digital 5  pins as pin to control 'dp' section(segment)
+void digital_0(void) //Segment display digital 0
+{
+  digitalWrite(a,HIGH);
+  digitalWrite(b,HIGH);
+  digitalWrite(c,HIGH);
+  digitalWrite(d,HIGH);
+  digitalWrite(e,HIGH);
+  digitalWrite(f,HIGH);
+  digitalWrite(g, LOW);
+  digitalWrite(dp,LOW);
+}
+void digital_1(void) //Segment display digital 1
+{
+  digitalWrite(a,LOW);
+  digitalWrite(b,HIGH);
+  digitalWrite(c,HIGH);
+  digitalWrite(d,LOW);
+  digitalWrite(e,LOW);
+  digitalWrite(f,LOW);
+  digitalWrite(g,LOW);
+  digitalWrite(dp,LOW);
+}
+void digital_2(void) //Segment display digital 2
+{
+  digitalWrite(a,HIGH);
+  digitalWrite(b,HIGH);
+  digitalWrite(c,LOW);
+  digitalWrite(d,HIGH);
+  digitalWrite(e,HIGH);
+  digitalWrite(f,LOW);
+  digitalWrite(g,HIGH);
+  digitalWrite(dp,LOW);
+}
+void digital_3(void) //Segment display digital 3
+{
+  digitalWrite(a,HIGH);
+  digitalWrite(b,HIGH);
+  digitalWrite(c,HIGH);
+  digitalWrite(d,HIGH);
+  digitalWrite(e,LOW);
+  digitalWrite(f,LOW);
+  digitalWrite(g,HIGH);
+  digitalWrite(dp,LOW);
+}
+void digital_4(void) //Segment display digital 4
+{
+  digitalWrite(a,LOW);
+  digitalWrite(b,HIGH);
+  digitalWrite(c,HIGH);
+  digitalWrite(d,LOW);
+  digitalWrite(e,LOW);
+  digitalWrite(f,HIGH);
+  digitalWrite(g,HIGH);
+  digitalWrite(dp,LOW);
+}
+void digital_5(void) //Segment display digital 5
+{
+  digitalWrite(a,HIGH);
+  digitalWrite(b,LOW);
+  digitalWrite(c,HIGH);
+  digitalWrite(d,HIGH);
+  digitalWrite(e,LOW);
+  digitalWrite(f,HIGH);
+  digitalWrite(g,HIGH);
+  digitalWrite(dp,LOW);
+}
+void digital_6(void) //Segment display digital 6
+{
+  digitalWrite(a,HIGH);
+  digitalWrite(b,LOW);  
+  digitalWrite(c,HIGH);
+  digitalWrite(d,HIGH);
+  digitalWrite(e,HIGH);
+  digitalWrite(f,HIGH);
+  digitalWrite(g,HIGH);
+  digitalWrite(dp,LOW);
+}
+void digital_7(void) //Segment display digital 7
+{
+  digitalWrite(a,HIGH);
+  digitalWrite(b,HIGH);  
+  digitalWrite(c,HIGH);  
+  digitalWrite(d,LOW); 
+  digitalWrite(e,LOW);
+  digitalWrite(f,LOW);
+  digitalWrite(g,LOW);
+  digitalWrite(dp,LOW);
+}
+void digital_8(void) //Segment display digital 8
+{
+  digitalWrite(a,HIGH);
+  digitalWrite(b,HIGH);
+  digitalWrite(c,HIGH);
+  digitalWrite(d,HIGH);
+  digitalWrite(e,HIGH);
+  digitalWrite(f,HIGH);
+  digitalWrite(g,HIGH);
+  digitalWrite(dp,LOW);
+}
+void digital_9(void) //Segment display digital 9
+{
+  digitalWrite(a,HIGH);
+  digitalWrite(b,HIGH);
+  digitalWrite(c,HIGH);
+  digitalWrite(d,HIGH);
+  digitalWrite(e,LOW);
+  digitalWrite(f,HIGH);
+  digitalWrite(g,HIGH);
+  digitalWrite(dp,LOW);
+}
+void setup()
+{
+  int i;             //Define variables
+  for(i=4;i<=11;i++)
+  pinMode(i,OUTPUT); //Set up 4 ~ 11 pins to output mode
+}
+void loop()
+{
+  digital_0(); //Segment display digital 0
+  delay(1000); //Delay 1s
+  digital_1(); //Segment display digital 1
+  delay(1000); //Delay 1s
+  digital_2(); //Segment display digital 2
+  delay(1000); //Delay 1s
+  digital_3(); //Segment display digital 3
+  delay(1000); //Delay 1s
+  digital_4(); //Segment display digital 4
+  delay(1000); //Delay 1s
+  digital_5(); //Segment display digital 5
+  delay(1000); //Delay 1s
+  digital_6(); //Segment display digital 6
+  delay(1000); //Delay 1s
+  digital_7(); //Segment display digital 7
+  delay(1000); //Delay 1s
+  digital_8(); //Segment display digital 8
+  delay(1000); //Delay 1s
+  digital_9(); //Segment display digital 8
+  delay(1000); //Delay 1s
+}
+

+ 53 - 0
_07_LCD1602.ino

@@ -0,0 +1,53 @@
+/***********************************************************
+File name: 07_LCD1602.ino
+Description: LCD1602 display a string "Hello Geeks!" scrolling,
+             then display “Adeept” and “www.adeept.com” static.
+Website: www.adeept.com
+E-mail: support@adeept.com
+Author: Tom
+Date: 2015/05/02 
+***********************************************************/
+
+#include <LiquidCrystal.h>
+
+char array1[]="     Adeept     ";                //the string to print on the LCD
+char array2[]="  hello geeks!                ";  //the string to print on the LCD
+char array3[]=" www.adeept.com ";                //the string to print on the LCD
+
+int tim = 250;  //the value of delay time
+
+// initialize the library with the numbers of the interface pins
+LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
+
+void setup()
+{
+  lcd.begin(16, 2);  // set up the LCD's number of columns and rows: 
+}
+
+void loop() 
+{
+    lcd.clear();  //Clears the LCD screen and positions the cursor in the upper-left corner
+    lcd.setCursor(15,0);                   // set the cursor to column 15, line 1
+    for (int positionCounter2 = 0; positionCounter2 < 30; positionCounter2++)
+    {
+      lcd.scrollDisplayLeft();             //Scrolls the contents of the display one space to the left.
+      lcd.print(array2[positionCounter2]); // Print a message to the LCD.
+      delay(tim);                          //wait for 250 microseconds
+    }
+
+    lcd.clear();  //Clears the LCD screen and positions the cursor in the upper-left corner.   
+    
+    lcd.setCursor(0,0);                    // set the cursor to column 15, line 0
+    for (int positionCounter1 = 0; positionCounter1 < 16; positionCounter1++)
+    {
+      lcd.print(array1[positionCounter1]); // Print a message to the LCD.
+      delay(tim);                          //wait for 250 microseconds
+    }
+    
+    lcd.setCursor(0,1);                    // set the cursor to column 15, line 1
+    for (int positionCounter3 = 0; positionCounter3 < 16; positionCounter3++)
+    {
+      lcd.print(array3[positionCounter3]); // Print a message to the LCD.
+      delay(tim);                          //wait for 250 microseconds
+    }
+}

+ 48 - 0
_08_photoresistance.ino

@@ -0,0 +1,48 @@
+/***********************************************************
+File name: 08_photoresistance.ino
+Description: We measure the light intensity of information with
+             photosensitive resistance, and displayed on the
+             LCD1602.
+Website: www.adeept.com
+E-mail: support@adeept.com
+Author: Tom
+Date: 2015/05/02 
+***********************************************************/
+
+#include <LiquidCrystal.h>
+
+char array1[]="   Adeept test   ";  //the string to print on the LCD
+
+char array2[]="   DATA: 0000    ";  //the string to print on the LCD
+
+int tim = 50;                       //the value of delay time
+
+// initialize the library with the numbers of the interface pins
+LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
+int photoresistorPin = 0;           // potentiometer wiper (middle terminal) connected to analog pin 3
+
+void setup()
+{
+  lcd.begin(16, 2);    // set up the LCD's number of columns and rows: 
+  lcd.clear();         //Clears the LCD screen and positions the cursor in the upper-left corner 
+  lcd.setCursor(0,0);  // set the cursor to column 15, line 0
+  for (int positionCounter1 = 0; positionCounter1 < 16; positionCounter1++)
+  {
+      lcd.print(array1[positionCounter1]);  // Print a message to the LCD.
+      delay(tim);     //wait for 250 microseconds
+  }
+}
+
+void loop() 
+{
+  array2[9]=analogRead(photoresistorPin)/1000+0x30;   //Take one thousand - bit data 
+  array2[10]=analogRead(photoresistorPin)/100%10+0x30;//Take one hundred - bit data
+  array2[11]=analogRead(photoresistorPin)/10%10+0x30; //Take ten-bit data
+  array2[12]=analogRead(photoresistorPin)%10+0x30;    //Take a bit of data
+  lcd.setCursor(0,1);                                 // set the cursor to column 15, line 1
+  for (int positionCounter3 = 0; positionCounter3 < 16; positionCounter3++)
+  {
+    lcd.print(array2[positionCounter3]);             // Print a message to the LCD.
+    delay(tim);                                      //wait for 250 microseconds
+  }
+}

+ 40 - 0
_09_serial.ino

@@ -0,0 +1,40 @@
+/***********************************************************
+File name: 09_serial.ino
+Description: If you send a character ‘1’ or ‘0’ on the 
+             serial monitor, the status of LED will be lit 
+             or gone out.
+Website: www.adeept.com
+E-mail: support@adeept.com
+Author: Tom
+Date: 2015/05/02 
+***********************************************************/
+
+int ledpin=11;           //definition digital 11 pins as pin to control the LED
+
+void setup()
+{
+  Serial.begin(9600);   // opens serial port, sets data rate to 9600 bps
+  pinMode(ledpin,OUTPUT);//Set digital 11 port mode, the OUTPUT for the output
+}
+void loop()
+{
+    char receiveVal;                  // Defined receive data
+   
+    if(Serial.available() > 0)       //Receive serial data
+    {        
+        receiveVal = Serial.read();  //Save the serial data received 
+        
+       if(receiveVal == '1')          //Receive data is 1, lit LED lights    
+       { 
+           digitalWrite(ledpin,HIGH); //print out the value of the LED       
+           Serial.println("LED:ON"); //send data to the serial monitor
+       }
+       if(receiveVal == '0')          //Receive data is 0, off LED lights
+       { 
+           digitalWrite(ledpin,LOW);  //print out the value of the LED                
+           Serial.println("LED:OFF");//send data to the serial monitor
+      } 
+    }
+  delay(50);                          //delay 50ms
+}
+

+ 171 - 0
_10_motor.ino

@@ -0,0 +1,171 @@
+/***********************************************************
+File name: 10_motor.ino
+Description: The state of DC motor includes its forward, reverse,
+             acceleration, deceleration and stop.
+Website: www.adeept.com
+E-mail: support@adeept.com
+Author: Tom
+Date: 2015/05/02 
+***********************************************************/
+
+const int motorIn1 = 11;  //attach to one of the pin of the motor
+const int motorIn2 = 10;  //attach to another pin of the motor
+
+int btn1pin=13;           //Set the digital 13 to button interface 
+int led1pin=5;            //definition digital 5 pins as pin to control the LED
+int btn2pin=12;           //Set the digital 12 to button interface 
+int led2pin=4;            //definition digital 4 pins as pin to control the LED
+int btn3pin=7;            //Set the digital 7 to button interface 
+int led3pin=3;            //definition digital 3 pins as pin to control the LED
+int btn4pin=6;            //Set the digital 6 to button interface 
+int led4pin=2;            //definition digital 2 pins as pin to control the LED
+
+int state = 0;            //Record the motor state. 0:STOP  1:forward  2:reverse  
+int DCmotorspeed = 128;   //Motor speed  0~255
+
+void setup()
+{
+  pinMode(motorIn1,OUTPUT);     //initialize the motorIn1 pin as output 
+  pinMode(motorIn2,OUTPUT);     //initialize the motorIn2 pin as output 
+  pinMode(btn1pin,INPUT_PULLUP);//Set digital 13 port mode, the INPUT for the input
+  pinMode(led1pin,OUTPUT);      //Set digital 5 port mode, the OUTPUT for the output
+  pinMode(btn2pin,INPUT_PULLUP);//Set digital 12 port mode, the INPUT for the input
+  pinMode(led2pin,OUTPUT);      //Set digital 4 port mode, the OUTPUT for the output
+  pinMode(btn3pin,INPUT_PULLUP);//Set digital 7 port mode, the INPUT for the input
+  pinMode(led3pin,OUTPUT);      //Set digital 3 port mode, the OUTPUT for the output
+  pinMode(btn4pin,INPUT_PULLUP);//Set digital 6 port mode, the INPUT for the input
+  pinMode(led4pin,OUTPUT);      //Set digital 2 port mode, the OUTPUT for the output
+}
+
+void loop()
+{
+  if(digitalRead(btn1pin)==LOW)          //Detection button interface to low
+  {   
+      delay(10);                         //Delay 10ms for the elimination of key leading-edge jitter
+      if(digitalRead(btn1pin)==LOW)      //Confirm button is pressed
+      {     
+        while(digitalRead(btn1pin)==LOW);//Wait for key interfaces to high
+        delay(10);                       //delay 10ms for the elimination of key trailing-edge jitter
+        while(digitalRead(btn1pin)==LOW);//Confirm button release
+        for(int i=0;i<4;i++)     
+        {
+          digitalWrite(led1pin,HIGH); //Output control status LED, ON
+          delay(100);                 //delay 100ms
+          digitalWrite(led1pin,LOW);  //Output control status LED, OFF
+          delay(100);                 //delay 100ms
+        }
+        if(state!=0)                  //Detecting the motor is running
+        {
+           state = 0;                 // Motor stop
+           digitalWrite(led1pin,LOW); //Output control status LED, OFF
+        }
+        else
+        {   
+           state = 1;                 //Motor Run
+           digitalWrite(led1pin,HIGH);//Output control status LED, ON
+        }  
+      }
+   }
+   
+  if(digitalRead(btn2pin)==LOW)          //Detection button interface to low
+  {   
+      delay(10);                         //Delay 10ms for the elimination of key leading-edge jitter
+      if(digitalRead(btn2pin)==LOW)      //Confirm button is pressed
+      {     
+        while(digitalRead(btn2pin)==LOW);//Wait for key interfaces to high
+        delay(10);                       //delay 10ms for the elimination of key trailing-edge jitter
+        while(digitalRead(btn2pin)==LOW);//Confirm button release
+        if(state!=0)                     //Detecting the motor is running
+        {
+           for(int i=0;i<4;i++)
+           {
+              digitalWrite(led2pin,HIGH);//Output control status LED, ON
+              delay(100);                //delay 100ms
+              digitalWrite(led2pin,LOW); //Output control status LED, OFF
+              delay(100);                //delay 100ms
+            }
+            if(state==1)                 //Motor forward
+            {state = 2;}                 //Motor reverse
+            else if(state==2)            //Motor reverse
+            {state = 1;}                 //Motor forward
+         }    
+      }
+   }
+   
+  if(digitalRead(btn3pin)==LOW)          //Detection button interface to low
+  {   
+      delay(10);                         //Delay 10ms for the elimination of key leading-edge jitter
+      if(digitalRead(btn3pin)==LOW)      //Confirm button is pressed
+      {     
+        while(digitalRead(btn3pin)==LOW);//Wait for key interfaces to high
+        delay(10);                       //delay 10ms for the elimination of key trailing-edge jitter
+        while(digitalRead(btn3pin)==LOW);//Confirm button release
+        if(state!=0)
+        {
+            for(int i=0;i<4;i++)
+            {
+               digitalWrite(led3pin,HIGH);//Output control status LED, ON
+               delay(100);                //delay 100ms
+               digitalWrite(led3pin,LOW); //Output control status LED, OFF
+               delay(100);                //delay 100ms
+             }
+             if(DCmotorspeed<230)         
+             {DCmotorspeed += 20;}       //Motor speed increases 20
+             else
+             {DCmotorspeed = 230;}       //Motor speed  230
+         }
+      }
+   }
+   
+   if(digitalRead(btn4pin)==LOW)         //Detection button interface to low
+   {   
+      delay(10);                         //Delay 10ms for the elimination of key leading-edge jitter
+      if(digitalRead(btn4pin)==LOW)      //Confirm button is pressed
+      {     
+        while(digitalRead(btn4pin)==LOW);//Wait for key interfaces to high
+        delay(10);                       //delay 10ms for the elimination of key trailing-edge jitter
+        while(digitalRead(btn4pin)==LOW);//Confirm button release
+        if(state!=0)
+        {
+           for(int i=0;i<4;i++)
+           {
+              digitalWrite(led4pin,HIGH);//Output control status LED, ON
+              delay(100);                //Delay 100ms
+              digitalWrite(led4pin,LOW); //Output control status LED, OFF
+              delay(100);                //Delay 100ms
+            }
+            if(DCmotorspeed>30)
+            {DCmotorspeed -= 20;}        //Motor speed reduction 20
+            else
+            {DCmotorspeed = 20;}         //Motor speed 20
+        }  
+      }
+   }
+   
+  switch(state)
+  {
+    case 0:  clockwise(0);                  //rotate clockwise 
+             break;
+    case 1:  clockwise(DCmotorspeed);       //rotate clockwise 
+             break;
+    case 2:  counterclockwise(DCmotorspeed);//rotate clockwise 
+             break;
+    default: clockwise(0);                  //rotate clockwise 
+             break;
+  }
+}
+
+//The function to drive motor rotate clockwise
+void clockwise(int Speed)
+{
+  analogWrite(motorIn1,Speed); //set the speed of motor
+  analogWrite(motorIn2,0);     //stop the motorIn2 pin of motor
+}
+
+//The function to drive motor rotate counterclockwise
+void counterclockwise(int Speed)
+{
+  analogWrite(motorIn1,0);     //stop the motorIn1 pin of motor
+  analogWrite(motorIn2,Speed); //set the speed of motor
+}
+