Guest User

Untitled

a guest
Oct 18th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #include <Otto.h>
  2. Otto Otto;
  3. #include <IRremote.h>
  4.  
  5.  
  6. IRrecv ir_rx(2);
  7. decode_results ir_rx_results;
  8.  
  9. unsigned long fnc_ir_rx_decode()
  10. {
  11. bool decoded=false;
  12. if( ir_rx.decode(&ir_rx_results))
  13. {
  14. decoded=true;
  15. ir_rx.resume();
  16. }
  17. if(decoded)
  18. return ir_rx_results.value;
  19. else
  20. return 0;
  21. }
  22.  
  23.  
  24. #define LeftLeg 2 // left leg pin, servo[0]
  25. #define RightLeg 3 // right leg pin, servo[1]
  26. #define LeftFoot 4 // left foot pin, servo[2]
  27. #define RightFoot 5 // right foot pin, servo[3]
  28. #define Buzzer 13 //buzzer pin
  29.  
  30. #include <EnableInterrupt.h>
  31.  
  32. #define Timeout 500
  33. #define Shift 8
  34. unsigned long time, dtime,timeout_mark;
  35. unsigned long res = 0;
  36. unsigned long Button_ID = 0;
  37. unsigned int buf = 0;
  38. byte impuls = 1;
  39. byte Channl_ID = 1;
  40. byte Button_en = 0;
  41. byte state = 0;
  42. byte Channl_buf = 0;
  43. // Return if the key was pressed
  44. bool RC(long BT_ID)
  45. {
  46. if (Timeout < millis() - timeout_mark)
  47. Button_ID = 0x733;
  48. if (BT_ID == Button_ID)
  49. return 1;
  50. return 0;
  51. }
  52.  
  53. //Configuration Remote Control
  54.  
  55. void IRRC_setup(int pin, uint8_t Channl)
  56. {
  57.  
  58. pinMode(pin, INPUT); //set the pin to input
  59. digitalWrite(pin, HIGH); //use the internal pullup resistor
  60.  
  61. time = micros();
  62.  
  63. enableInterrupt(pin, change, CHANGE);
  64.  
  65. switch ( Channl )
  66. {
  67. case 1:
  68. Channl_ID = 0xFC;
  69. return;
  70. case 2:
  71. Channl_ID = 0x3C;
  72. return;
  73. case 3:
  74. Channl_ID = 0xCC;
  75. return;
  76. case 4:
  77. Channl_ID = 0x0C;
  78. return;
  79. case 5:
  80. Channl_ID = 0xF0;
  81. return;
  82. case 6:
  83. Channl_ID = 0x30;
  84. return;
  85. case 7:
  86. Channl_ID = 0xC0;
  87. return;
  88. case 8:
  89. Channl_ID = 0x00;
  90. return;
  91. //default:
  92. }
  93. }
  94.  
  95. //Interrupt function
  96. void change()
  97. {
  98.  
  99. dtime = micros();
  100. buf = dtime - time ;
  101. time = dtime;
  102.  
  103. //procesar la duración del pulso (200 redondeando hacia arriba con buf% 200> 101 y (o) si el pulso es <101 asignación de una sola longitud)
  104. if (buf % 200 > 101)
  105. {
  106. buf = (buf / 200) + 1;
  107. }
  108. else
  109. {
  110. buf = buf / 200;
  111. }
  112. if (buf == 0) buf = 1;
  113.  
  114. if (state == 0) // Initial state
  115. {
  116. if (!impuls) //Checking the first bits (000)
  117. {
  118. state++;
  119. res = 0;
  120. res = res << buf;
  121. //Serial.println("Start");
  122. }
  123. } else {
  124. if (impuls)
  125. {
  126. res = res << buf;
  127. for (int i = 0; i < buf ; i++)
  128. {
  129. res |= 1 << i ;
  130. }
  131. }else{
  132. res = res << buf;
  133.  
  134. if ((byte)(res & 0x7F) == 0x38)
  135. {
  136. buf = 1;
  137. for (int i = 1; i < Shift; i ++)
  138. buf = (buf << 1) + 1;
  139.  
  140. Channl_buf = (res >> 6) & buf ;
  141.  
  142. if (Channl_buf == Channl_ID)
  143. {
  144. Button_ID = res >> (6+Shift);
  145. while(!(Button_ID & 1)) Button_ID = Button_ID >> 1;
  146. timeout_mark = millis();
  147. //Serial.println (Button_ID, HEX);
  148. }
  149. state = 0;
  150. res = 0;
  151. }
  152. }
  153. }
  154. impuls = !impuls;
  155. }
  156.  
  157.  
  158. void setup() {
  159. Otto.init(LeftLeg, RightLeg, LeftFoot, RightFoot, true, Buzzer);
  160. Otto.home();
  161.  
  162. ir_rx.enableIRIn();
  163.  
  164. IRRC_setup(2,1);
  165.  
  166. }
  167.  
  168. void loop() {
  169. if ((unsigned long)fnc_ir_rx_decode() & (0x00FF629D)) {
  170. Otto.walk(1,1000,1); // FORWARD
  171. }
  172. if ((unsigned long)fnc_ir_rx_decode() & (0x00FFA857)) {
  173. Otto.walk(1,1000,-1); // BACKWARD
  174. }
  175. if ((unsigned long)fnc_ir_rx_decode() & (0x00FF22DD)) {
  176. Otto.turn(1,1000,1); // LEFT
  177. }
  178. if ((unsigned long)fnc_ir_rx_decode() & (0x00FFC23D)) {
  179. Otto.turn(1,1000,-1); // RIGHT
  180. }
  181. if ((unsigned long)fnc_ir_rx_decode() & (0x00FFC23D)) {
  182. Otto.turn(1,1000,-1); // RIGHT
  183. }
  184.  
  185. }
Add Comment
Please, Sign In to add comment