_10_motor.ino 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /***********************************************************
  2. File name: 10_motor.ino
  3. Description: The state of DC motor includes its forward, reverse,
  4. acceleration, deceleration and stop.
  5. Website: www.adeept.com
  6. E-mail: support@adeept.com
  7. Author: Tom
  8. Date: 2015/05/02
  9. ***********************************************************/
  10. const int motorIn1 = 11; //attach to one of the pin of the motor
  11. const int motorIn2 = 10; //attach to another pin of the motor
  12. int btn1pin=13; //Set the digital 13 to button interface
  13. int led1pin=5; //definition digital 5 pins as pin to control the LED
  14. int btn2pin=12; //Set the digital 12 to button interface
  15. int led2pin=4; //definition digital 4 pins as pin to control the LED
  16. int btn3pin=7; //Set the digital 7 to button interface
  17. int led3pin=3; //definition digital 3 pins as pin to control the LED
  18. int btn4pin=6; //Set the digital 6 to button interface
  19. int led4pin=2; //definition digital 2 pins as pin to control the LED
  20. int state = 0; //Record the motor state. 0:STOP 1:forward 2:reverse
  21. int DCmotorspeed = 128; //Motor speed 0~255
  22. void setup()
  23. {
  24. pinMode(motorIn1,OUTPUT); //initialize the motorIn1 pin as output
  25. pinMode(motorIn2,OUTPUT); //initialize the motorIn2 pin as output
  26. pinMode(btn1pin,INPUT_PULLUP);//Set digital 13 port mode, the INPUT for the input
  27. pinMode(led1pin,OUTPUT); //Set digital 5 port mode, the OUTPUT for the output
  28. pinMode(btn2pin,INPUT_PULLUP);//Set digital 12 port mode, the INPUT for the input
  29. pinMode(led2pin,OUTPUT); //Set digital 4 port mode, the OUTPUT for the output
  30. pinMode(btn3pin,INPUT_PULLUP);//Set digital 7 port mode, the INPUT for the input
  31. pinMode(led3pin,OUTPUT); //Set digital 3 port mode, the OUTPUT for the output
  32. pinMode(btn4pin,INPUT_PULLUP);//Set digital 6 port mode, the INPUT for the input
  33. pinMode(led4pin,OUTPUT); //Set digital 2 port mode, the OUTPUT for the output
  34. }
  35. void loop()
  36. {
  37. if(digitalRead(btn1pin)==LOW) //Detection button interface to low
  38. {
  39. delay(10); //Delay 10ms for the elimination of key leading-edge jitter
  40. if(digitalRead(btn1pin)==LOW) //Confirm button is pressed
  41. {
  42. while(digitalRead(btn1pin)==LOW);//Wait for key interfaces to high
  43. delay(10); //delay 10ms for the elimination of key trailing-edge jitter
  44. while(digitalRead(btn1pin)==LOW);//Confirm button release
  45. for(int i=0;i<4;i++)
  46. {
  47. digitalWrite(led1pin,HIGH); //Output control status LED, ON
  48. delay(100); //delay 100ms
  49. digitalWrite(led1pin,LOW); //Output control status LED, OFF
  50. delay(100); //delay 100ms
  51. }
  52. if(state!=0) //Detecting the motor is running
  53. {
  54. state = 0; // Motor stop
  55. digitalWrite(led1pin,LOW); //Output control status LED, OFF
  56. }
  57. else
  58. {
  59. state = 1; //Motor Run
  60. digitalWrite(led1pin,HIGH);//Output control status LED, ON
  61. }
  62. }
  63. }
  64. if(digitalRead(btn2pin)==LOW) //Detection button interface to low
  65. {
  66. delay(10); //Delay 10ms for the elimination of key leading-edge jitter
  67. if(digitalRead(btn2pin)==LOW) //Confirm button is pressed
  68. {
  69. while(digitalRead(btn2pin)==LOW);//Wait for key interfaces to high
  70. delay(10); //delay 10ms for the elimination of key trailing-edge jitter
  71. while(digitalRead(btn2pin)==LOW);//Confirm button release
  72. if(state!=0) //Detecting the motor is running
  73. {
  74. for(int i=0;i<4;i++)
  75. {
  76. digitalWrite(led2pin,HIGH);//Output control status LED, ON
  77. delay(100); //delay 100ms
  78. digitalWrite(led2pin,LOW); //Output control status LED, OFF
  79. delay(100); //delay 100ms
  80. }
  81. if(state==1) //Motor forward
  82. {state = 2;} //Motor reverse
  83. else if(state==2) //Motor reverse
  84. {state = 1;} //Motor forward
  85. }
  86. }
  87. }
  88. if(digitalRead(btn3pin)==LOW) //Detection button interface to low
  89. {
  90. delay(10); //Delay 10ms for the elimination of key leading-edge jitter
  91. if(digitalRead(btn3pin)==LOW) //Confirm button is pressed
  92. {
  93. while(digitalRead(btn3pin)==LOW);//Wait for key interfaces to high
  94. delay(10); //delay 10ms for the elimination of key trailing-edge jitter
  95. while(digitalRead(btn3pin)==LOW);//Confirm button release
  96. if(state!=0)
  97. {
  98. for(int i=0;i<4;i++)
  99. {
  100. digitalWrite(led3pin,HIGH);//Output control status LED, ON
  101. delay(100); //delay 100ms
  102. digitalWrite(led3pin,LOW); //Output control status LED, OFF
  103. delay(100); //delay 100ms
  104. }
  105. if(DCmotorspeed<230)
  106. {DCmotorspeed += 20;} //Motor speed increases 20
  107. else
  108. {DCmotorspeed = 230;} //Motor speed 230
  109. }
  110. }
  111. }
  112. if(digitalRead(btn4pin)==LOW) //Detection button interface to low
  113. {
  114. delay(10); //Delay 10ms for the elimination of key leading-edge jitter
  115. if(digitalRead(btn4pin)==LOW) //Confirm button is pressed
  116. {
  117. while(digitalRead(btn4pin)==LOW);//Wait for key interfaces to high
  118. delay(10); //delay 10ms for the elimination of key trailing-edge jitter
  119. while(digitalRead(btn4pin)==LOW);//Confirm button release
  120. if(state!=0)
  121. {
  122. for(int i=0;i<4;i++)
  123. {
  124. digitalWrite(led4pin,HIGH);//Output control status LED, ON
  125. delay(100); //Delay 100ms
  126. digitalWrite(led4pin,LOW); //Output control status LED, OFF
  127. delay(100); //Delay 100ms
  128. }
  129. if(DCmotorspeed>30)
  130. {DCmotorspeed -= 20;} //Motor speed reduction 20
  131. else
  132. {DCmotorspeed = 20;} //Motor speed 20
  133. }
  134. }
  135. }
  136. switch(state)
  137. {
  138. case 0: clockwise(0); //rotate clockwise
  139. break;
  140. case 1: clockwise(DCmotorspeed); //rotate clockwise
  141. break;
  142. case 2: counterclockwise(DCmotorspeed);//rotate clockwise
  143. break;
  144. default: clockwise(0); //rotate clockwise
  145. break;
  146. }
  147. }
  148. //The function to drive motor rotate clockwise
  149. void clockwise(int Speed)
  150. {
  151. analogWrite(motorIn1,Speed); //set the speed of motor
  152. analogWrite(motorIn2,0); //stop the motorIn2 pin of motor
  153. }
  154. //The function to drive motor rotate counterclockwise
  155. void counterclockwise(int Speed)
  156. {
  157. analogWrite(motorIn1,0); //stop the motorIn1 pin of motor
  158. analogWrite(motorIn2,Speed); //set the speed of motor
  159. }