Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 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. aux_salvar3 = pot_Y2;
  131. EEPROM_ESCRITA(pot_Y2, adress_escrita);
  132. adress_escrita = adress_escrita +1;
  133. aux_salvar4 = pot_X2;
  134. EEPROM_ESCRITA(pot_X2, adress_escrita);
  135. adress_escrita = adress_escrita +1;
  136. }
  137.  
  138. _leitura = 0;
  139.  
  140. if (mensure >= 20 && mode == 1 ){
  141.  
  142. leitura_EEPROM = EEPROM_LEITURA(_leitura);
  143. logic = ((_leitura)-(4*counter));
  144. counter = counter + 1;
  145. _leitura = _leitura + 1;
  146.  
  147. if (leitura_EEPROM == 0){
  148.  
  149. cont ++;
  150.  
  151. if (cont > 4)
  152. _leitura = 0;
  153. }
  154.  
  155. if (logic == 1)
  156. //base.write(leitura_EEPROM);
  157. if (logic == 2)
  158. //garra.write(leitura_EEPROM);
  159. if (logic == 3)
  160. //elevador.write(leitura_EEPROM);
  161. if (logic == 4)
  162. //encolhedor.write(leitura_EEPROM);
  163.  
  164. delay (500);
  165.  
  166. if (_leitura == EEPROM.length())
  167. _leitura = 0;
  168.  
  169. }
  170.  
  171. /* if (button X == 1)
  172. clear_EEPROM();
  173. */
  174.  
  175.  
  176.  
  177. /* int mode = push_button();
  178.  
  179. if(mode == 1){
  180. Serial.println("---- Modo automatico Inicializado ---");
  181. }
  182. else{
  183. Serial.println("--- Modo Manual Inicializado ---");
  184. }
  185. */
  186. }
  187.  
  188.  
  189. float ultrassonic_mensure(){
  190.  
  191. float cmMsec, inMsec;
  192. long microsec = ultrasonic.timing();
  193. cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
  194. delay(500);
  195. return cmMsec;
  196. }
  197.  
  198. void alerta_pessoa(){
  199.  
  200. digitalWrite(8, HIGH);
  201. delay(400);
  202. digitalWrite(8, LOW);
  203. delay(400);
  204.  
  205. }
  206.  
  207. int push_button(){
  208.  
  209.  
  210. unsigned long timer_present = millis();
  211.  
  212. if(timer_present - timer_future > 150){
  213.  
  214. button = digitalRead(2);
  215. timer_future = millis();
  216.  
  217. if(button == 1){
  218.  
  219. if(aux == 1)
  220. aux = 0;
  221. else
  222. aux = 1;
  223. }
  224.  
  225. if(aux == 1)
  226. return 1;
  227.  
  228. if(aux == 0)
  229. return 0;
  230.  
  231. }
  232. }
  233.  
  234. void EEPROM_ESCRITA(int a, int adress){
  235.  
  236. EEPROM.write(adress, a);
  237. delay (100);
  238.  
  239. }
  240.  
  241.  
  242. int EEPROM_LEITURA(int adress){
  243.  
  244. int leitura = EEPROM.read(adress);
  245.  
  246. if (adress == EEPROM.length())
  247. adress = 0;
  248.  
  249. return leitura;
  250.  
  251. }
  252.  
  253. int SALVAR_COMANDO(){
  254.  
  255. int val = digitalRead(5);
  256.  
  257. if (val == 1)
  258. return 1;
  259.  
  260. else
  261. return 0;
  262.  
  263. }
  264.  
  265. void clear_EEPROM(){
  266.  
  267. for (int i = 0 ; i < EEPROM.length() ; i++) {
  268. EEPROM.write(i, 0);
  269. }
  270.  
  271. Serial.println("--- Memoria Limpa ---");
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement