Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. #include <Ultrasonic.h>
  2. #include <Servo.h>
  3. #include <EEPROM.h>
  4. #define TRIGGER_PIN 12
  5. #define ECHO_PIN 13
  6.  
  7. Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);
  8.  
  9. //Servo base;
  10. //Servo garra;
  11. //Servo elevador;
  12. //Servo encolhedor;
  13.  
  14. float ultrassonic_mensure();
  15. void alerta_pessoa();
  16. int push_button();
  17. void EEPROM_ESCRITA(int a, int adress);
  18. int EEPROM_LEITURA(int adress);
  19. int SALVAR_COMANDO();
  20. void clear_EEPROM();
  21.  
  22. int _leitura = 0;
  23. int cont = 0;
  24. int adress_escrita = 0;
  25. int logic;
  26. int leitura_X;
  27. int leitura_Y;
  28. int leitura_EEPROM;
  29. int pot_X;
  30. int pot_Y;
  31. int pot_X2;
  32. int counter = 0;
  33. int pot_Y2;
  34. int timer_presente;
  35. int timer_passado = 0;
  36. int aux_leitura_Y;
  37. int timer;
  38. int timer_pass = 0;
  39. int mode;
  40. int salvar;
  41. int button;
  42. int aux;
  43. int aux_salvar;
  44. int aux_salvar2;
  45. int aux_salvar3;
  46. int aux_salvar4;
  47. unsigned long timer_future;
  48.  
  49.  
  50. void setup() {
  51.  
  52. Serial.begin(9600);
  53. pinMode(2, INPUT);
  54. pinMode(8, OUTPUT);
  55. pinMode(5, INPUT);
  56.  
  57. //base.attach(9);
  58. //garra.attach(10);
  59. //elevador.attach(11);
  60. //encolhedor.attach(6);
  61.  
  62.  
  63. }
  64.  
  65. void loop() {
  66.  
  67. float mensure = ultrassonic_mensure();
  68.  
  69. if (mensure >= 20 && mode != 1){
  70.  
  71. leitura_Y = analogRead(0);
  72. leitura_X = analogRead(1);
  73. pot_X = map(leitura_X, 0, 1023, 0, 180);
  74. pot_Y = map(leitura_Y,0,1023,0,180);
  75.  
  76. timer_presente = millis();
  77.  
  78. if ((timer_presente - timer_passado) > 100 && pot_Y != 89){
  79.  
  80. Serial.print(pot_Y);
  81. Serial.println(" Eixo Y. ");
  82. //base.write(pot_Y);
  83.  
  84. }
  85.  
  86. if((timer_presente - timer_passado) > 100 && pot_X != 91){
  87.  
  88. Serial.print(pot_X);
  89. Serial.println(" Eixo X. ");
  90. //garra.write(pot_X);
  91. }
  92.  
  93. /*
  94. if((timer_presente - timer_passado) > 100 && pot_X2 != 91){
  95.  
  96. Serial.print(pot_X2);
  97. Serial.println(" Eixo X2. ");
  98. //elevador.write(pot_X2);
  99. }
  100.  
  101. if((timer_presente - timer_passado) > 100 && pot_Y2 != 91){
  102.  
  103. Serial.print(pot_Y2);
  104. Serial.println(" Eixo Y2. ");
  105. //encolhedor.write(pot_Y2);
  106. }
  107. */
  108.  
  109. }
  110.  
  111. timer_passado = millis();
  112.  
  113. if (mensure < 20){
  114. Serial.println("Alerta Pessoa no Perimetro");
  115. alerta_pessoa();
  116. }
  117.  
  118. salvar = SALVAR_COMANDO();
  119. if (adress_escrita == EEPROM.length())
  120. Serial.println("Aperte o botao X para limpar a memoria e gravar novos movimentos");
  121.  
  122. if (mensure >= 20 && salvar == 1 && adress_escrita != EEPROM.length()){
  123.  
  124. aux_salvar = pot_X;
  125. EEPROM_ESCRITA(pot_X, adress_escrita);
  126. adress_escrita = adress_escrita +1;
  127. aux_salvar2 = pot_Y;
  128. EEPROM_ESCRITA(pot_Y, adress_escrita);
  129. adress_escrita = adress_escrita +1;
  130. EEPROM_ESCRITA(pot_Y2, adress_escrita);
  131. adress_escrita = adress_escrita +1;
  132. EEPROM_ESCRITA(pot_X2, adress_escrita);
  133. adress_escrita = adress_escrita +1;
  134. }
  135.  
  136. _leitura = 0;
  137.  
  138. if (mensure >= 20 && mode == 1 ){
  139.  
  140. leitura_EEPROM = EEPROM_LEITURA(_leitura);
  141. logic = ((_leitura)-(4*counter));
  142. counter = counter + 1;
  143. _leitura = _leitura + 1;
  144.  
  145. if (leitura_EEPROM == 0){
  146.  
  147. cont ++;
  148.  
  149. if (cont > 4)
  150. _leitura = 0;
  151. }
  152.  
  153. if (logic == 1)
  154. //base.write(leitura_EEPROM);
  155. if (logic == 2)
  156. //garra.write(leitura_EEPROM);
  157. if (logic == 3)
  158. //elevador.write(leitura_EEPROM);
  159. if (logic == 4)
  160. //encolhedor.write(leitura_EEPROM);
  161.  
  162. delay (500);
  163.  
  164. if (_leitura == EEPROM.length())
  165. _leitura = 0;
  166.  
  167. }
  168.  
  169. /* if (button X == 1)
  170. clear_EEPROM();
  171. */
  172.  
  173.  
  174.  
  175. /* int mode = push_button();
  176.  
  177. if(mode == 1){
  178. Serial.println("---- Modo automatico Inicializado ---");
  179. }
  180. else{
  181. Serial.println("--- Modo Manual Inicializado ---");
  182. }
  183. */
  184. }
  185.  
  186.  
  187. float ultrassonic_mensure(){
  188.  
  189. float cmMsec, inMsec;
  190. long microsec = ultrasonic.timing();
  191. cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
  192. delay(500);
  193. return cmMsec;
  194. }
  195.  
  196. void alerta_pessoa(){
  197.  
  198. digitalWrite(8, HIGH);
  199. delay(400);
  200. digitalWrite(8, LOW);
  201. delay(400);
  202.  
  203. }
  204.  
  205. int push_button(){
  206.  
  207.  
  208. unsigned long timer_present = millis();
  209.  
  210. if(timer_present - timer_future > 150){
  211.  
  212. button = digitalRead(2);
  213. timer_future = millis();
  214.  
  215. if(button == 1){
  216.  
  217. if(aux == 1)
  218. aux = 0;
  219. else
  220. aux = 1;
  221. }
  222.  
  223. if(aux == 1)
  224. return 1;
  225.  
  226. if(aux == 0)
  227. return 0;
  228.  
  229. }
  230. }
  231.  
  232. void EEPROM_ESCRITA(int a, int adress){
  233.  
  234. EEPROM.write(adress, a);
  235. delay (100);
  236.  
  237. }
  238.  
  239.  
  240. int EEPROM_LEITURA(int adress){
  241.  
  242. int leitura = EEPROM.read(adress);
  243.  
  244. if (adress == EEPROM.length())
  245. adress = 0;
  246.  
  247. return leitura;
  248.  
  249. }
  250.  
  251. int SALVAR_COMANDO(){
  252.  
  253. int val = digitalRead(5);
  254.  
  255. if (val == 1)
  256. return 1;
  257.  
  258. else
  259. return 0;
  260.  
  261. }
  262.  
  263. void clear_EEPROM(){
  264.  
  265. for (int i = 0 ; i < EEPROM.length() ; i++) {
  266. EEPROM.write(i, 0);
  267. }
  268.  
  269. Serial.println("--- Memoria Limpa ---");
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement