圣源电子制作

标题: ATMEGA328红外遥控电动窗帘-制作DIY-原理图-程序-测试视频-转载自国外网站 [打印本页]

作者: sydz    时间: 2011-12-7 16:41
标题: ATMEGA328红外遥控电动窗帘-制作DIY-原理图-程序-测试视频-转载自国外网站
(, 下载次数: 55)



简介


该项目是“电动窗帘”远程控制。 它是由单片机ATMEGA328与Arduino的BootLoader,电机驱动L293D(我所用的外部二极管L293B,因为我无法找到L293D),红外接收器的TSOP 1738年,直流电动机从旧的打印机和其他小零件。 控制它,我用红外遥控器,从宝联电视调谐器该软件允许移动的帷幕从左至右回,或应采取的步骤。 在去年PEG(窗帘导轨)连接一块小磁铁,两个簧片接触 ​​交互,对铁路的两端。 最后PEG是把它连接到一个线,其中一方的电机和其他卷轴移动

源代码是写在Arduino的。 我用红外遥控器解 ​​码代码NECIRRcv图书馆。




示意图


(, 下载次数: 58)


照片


(, 下载次数: 51)

(, 下载次数: 49)

(, 下载次数: 46)

(, 下载次数: 45)

程序:
  1. #include <WProgram.h>
  2. #include <NECIRrcv.h>
  3. #include <avr/interrupt.h >
  4. #include <avr/io.h >

  5. #define INIT_TIMER_COUNT 6
  6. #define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT

  7. #define BK_TV 4228082566 // TV/FM
  8. #define BK_SC 4161235846 // SCAN
  9. #define BK_ZO 3810290566 // ZOOM
  10. #define BK_P 3776867206 // POWER
  11. #define BK_1 4261505926 // 1
  12. #define BK_2 4094389126 // 2
  13. #define BK_3 3827002246 // 3
  14. #define BK_4 4194659206 // 4
  15. #define BK_5 4127812486 // 5
  16. #define BK_6 3927272326 // 6
  17. #define BK_7 4177947526 // 7
  18. #define BK_8 4111100806 // 8
  19. #define BK_9 3977407366 // 9
  20. #define BK_0 4244794246 // 0
  21. #define BK_VP 3760155526 // VOLUME +
  22. #define BK_VM 3893848966 // VOLUME -
  23. #define BK_CP 3910560646 // CHANNEL +
  24. #define BK_CM 3943984006 // CHANNEL -
  25. #define BK_MU 3877137286 // MUTE
  26. #define BK_10 4010830726 // +100
  27. #define BK_LO 3960695686 // LOOP
  28. #define BK_SO 4278217606 // SOURCE
  29. #define BK_MI 4211370886 // -
  30. #define BK_FU 4044254086// FUNCTION
  31. #define BK_PL 4077677446 // +
  32. #define BK_RS 4060965766 // RESET
  33. #define BK_FR 4027542406 // FREEZY

  34. int state = 0;
  35. int led_state = 0;
  36. int set_leds = 0;
  37. unsigned long dead_time = 0;
  38. unsigned long ircode ;
  39. unsigned long time = 0;
  40. int ttime = 0;
  41. int pLed = 10;
  42. int leftReed = 7;
  43. int rightReed = 8;
  44. int motor1 = 4;
  45. int motor2 = 2;
  46. int statLed = 10;
  47. int led1 = 11;
  48. int led2 = 12;






  49. NECIRrcv ir(6);



  50. void setLeds(int leds){
  51. if(bitRead(leds,0) == 1){
  52. digitalWrite(led1, HIGH);
  53. }
  54. else{
  55. digitalWrite(led1, LOW);
  56. }
  57. if(bitRead(leds,1) == 1){
  58. digitalWrite(led2, HIGH);
  59. }
  60. else{
  61. digitalWrite(led2, LOW);
  62. }
  63. }

  64. void seet(int leds,int mode){
  65. if(bitRead(leds,0) == 1){
  66. digitalWrite(led1, mode);
  67. digitalWrite(led2, LOW);
  68. }
  69. if(bitRead(leds,1) == 1){
  70. digitalWrite(led2, mode);
  71. digitalWrite(led1, LOW);
  72. }
  73. }

  74. void setup(){
  75. pinMode(leftReed, INPUT);
  76. pinMode(rightReed, INPUT);
  77. pinMode(motor1, OUTPUT);
  78. pinMode(motor2, OUTPUT);
  79. pinMode(pLed, OUTPUT);

  80. pinMode(led1, OUTPUT);
  81. pinMode(led2, OUTPUT);
  82. digitalWrite(pLed, HIGH);
  83. Serial.begin(9600) ;
  84. Serial.println("NEC IR code reception") ;
  85. ir.begin() ;
  86. }

  87. void loop(){

  88. if(ir.available()){
  89. ircode = ir.read();
  90. Serial.println(ircode, DEC) ;
  91. switch(state){
  92. case 0:
  93. switch(long(ircode)){
  94. case BK_MI:
  95. rotateLeft(0);
  96. state = 1;
  97. break;
  98. case BK_PL:
  99. rotateRight(0);
  100. state = 1;
  101. break;
  102. case BK_RS:
  103. rotateLeft(200);
  104. break;
  105. case BK_FR:
  106. rotateRight(200);
  107. break;
  108. case BK_FU:
  109. state = 1;
  110. break;
  111. case BK_SC:
  112. state = 2;
  113. //RESET_TIMER2;
  114. time = dead_time = millis();
  115. set_leds = 1;
  116. break;
  117. }
  118. break;

  119. case 2:
  120. switch(long(ircode)){
  121. case BK_SC:
  122. state = 2;
  123. //RESET_TIMER2;
  124. time = dead_time = millis();
  125. set_leds++;
  126. if(set_leds == 3) state = 1;
  127. break;

  128. case BK_FU:
  129. state = 3;
  130. //RESET_TIMER2;
  131. time = dead_time = millis();
  132. break;
  133. }
  134. break;
  135. }
  136. }
  137. switch(state){
  138. case 1:
  139. digitalWrite(pLed, HIGH);
  140. if(long(ircode) == BK_FU){
  141. led_state++;
  142. if(led_state == 4) led_state = 0;
  143. }
  144. Serial.println(led_state, DEC);
  145. setLeds(led_state);
  146. state = 0;
  147. break;
  148. case 2:
  149. if( millis()-time > 500 ){
  150. if(millis()-time > 1000) time = millis();
  151. seet(set_leds, LOW);
  152. }
  153. else{
  154. seet(set_leds, HIGH);
  155. }
  156. if(millis()-dead_time > 20000) {
  157. state = 1;
  158. }
  159. break;

  160. case 3:
  161. if( millis()-time > 200 ){
  162. if(millis()-time > 400) time = millis();
  163. seet(set_leds, LOW);
  164. }
  165. else{
  166. seet(set_leds, HIGH);
  167. }
  168. if(millis()-dead_time > 5000) {
  169. state = 1;
  170. seet(set_leds, HIGH);
  171. delay(1000);
  172. }
  173. break;
  174. }
  175. ircode = 0;
  176. }

  177. void rotateLeft(int time){
  178. ttime = millis();
  179. if(time > 0){
  180. while(millis() - ttime < time && digitalRead(leftReed) == LOW){
  181. digitalWrite(motor1, HIGH);
  182. digitalWrite(motor2, LOW);
  183. }
  184. }else{
  185. while( digitalRead(leftReed) == LOW ){
  186. blinkleds(200);
  187. digitalWrite(motor1, HIGH);
  188. digitalWrite(motor2, LOW);
  189. }
  190. }
  191. stop();

  192. }

  193. void rotateRight(int time){
  194. ttime = millis();
  195. if(time > 0){
  196. while(millis() - ttime < time && digitalRead(rightReed) == LOW){
  197. digitalWrite(motor1, LOW);
  198. digitalWrite(motor2, HIGH);
  199. }
  200. }else{
  201. while( digitalRead(rightReed) == LOW ){
  202. blinkleds(200);
  203. digitalWrite(motor1, LOW);
  204. digitalWrite(motor2, HIGH
  205. );
  206. }
  207. }
  208. stop();

  209. }

  210. void blinkleds(int blinktime){
  211. if( millis()-ttime > blinktime ){
  212. if(millis()-ttime > blinktime) ttime = millis();
  213. digitalWrite(pLed, LOW);
  214. }
  215. else{
  216. digitalWrite(pLed, HIGH);
  217. }
  218. }

  219. void stop(){
  220. digitalWrite(motor1, LOW);
  221. digitalWrite(motor2, LOW);
  222. }

复制代码



清晰原理图下载
(, 下载次数: 28)
程序下载
(, 下载次数: 25)


作者: sydz    时间: 2011-12-7 16:44
   ATMEGA328红外遥控电动窗帘 演示视频
http://www.syyyd.com/forum.php?m ... tid=2122&extra=




欢迎光临 圣源电子制作 (https://syyyd.com/) Powered by Discuz! X3.4