Advertisement
RuiViana

AG_Testa_Step

Jan 30th, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 KB | None | 0 0
  1. #include <Stepper.h>
  2. #include <EEPROM.h>
  3. #define STEPS 2048                                    // 32 (Fullsteps) * (Reduction ratio)
  4. #define COIL1 8
  5. #define COIL2 9
  6. #define COIL3 10
  7. #define COIL4 11
  8. #define analog A0
  9. Stepper stepper(STEPS, COIL1, COIL2, COIL3, COIL4);     // Create an instance off the stepper class:
  10. int pos = 0;                                            // inicial position off stepper
  11. int val = 0;                                            // Valor lido de A0
  12. int estabiliza = 0;                                     // Controle para o motor ficar parado devido flutuacoes em A0
  13.  
  14. #define botA 4                                          // Botao para salvar posicao A do step
  15. #define botB 5                                          // Botao para salvar posicao B do step
  16. #define botC 6                                          // Botao para salvar posicao C do step
  17. #define botD 7                                          // Botao para salvar posicao D do step
  18.  
  19. int eeAddress = 0;                                      // EEPROM endereco inicial
  20. //--------------------------------------------
  21. void setup()
  22. {
  23.   stepper.setSpeed(4);                                  // Speed of motor to 4 rpm
  24.   Serial.begin(57600);
  25.   pinMode(botA, INPUT_PULLUP);
  26.   pinMode(botB, INPUT_PULLUP);
  27.   pinMode(botC, INPUT_PULLUP);
  28.   pinMode(botD, INPUT_PULLUP);
  29.   EEPROM.get(eeAddress, val);
  30. }
  31. //--------------------------------------------
  32. void loop()
  33. {
  34.   if (digitalRead(botA) == LOW)                         // Se botao A foi apertado
  35.   {
  36.     delay(20);                                          // Debouncing
  37.     if (digitalRead(botA) == LOW)                       // Se botao A continua apertado                                
  38.     {
  39.       EEPROM.put(eeAddress, val);                       // Salva na EEPROM 0
  40.     }
  41.   }
  42.   if (digitalRead(botB) == LOW)                         // Se botao B foi apertado
  43.   {
  44.     delay(20);                                          // Debouncing
  45.     if (digitalRead(botB) == LOW)                       // Se botao B continua apertado      
  46.     {
  47.       EEPROM.put(eeAddress + 2, val);                   // Salva na EEPROM 2
  48.     }
  49.   }
  50.   if (digitalRead(botC) == LOW)                         // Se botao C foi apertado
  51.   {
  52.     delay(20);                                          // Debouncing
  53.     if (digitalRead(botC) == LOW)                       // Se botao C continua apertado      
  54.     {
  55.       EEPROM.put(eeAddress + 4, val);                   // Salva na EEPROM 4
  56.     }
  57.   }
  58.   if (digitalRead(botD) == LOW)                         // Se botao D foi apertado
  59.   {
  60.     delay(20);                                          // Debouncing
  61.     if (digitalRead(botD) == LOW)                       // Se botao D continua apertado      
  62.     {
  63.       EEPROM.put(eeAddress + 6, val);                   // Salva na EEPROM 6
  64.     }
  65.   }
  66.   val = analogRead(analog);                             // Le Ao e salva em val
  67.   /*  Serial.print(val);
  68.     Serial.print("  ");
  69.     Serial.print(pos);
  70.     Serial.print("  ");*/
  71.   Serial.println(val - pos);
  72.   if (val != abs(pos))                                  // Se de haver movimento do motor
  73.   {
  74.     if (estabiliza == 0)                                // Se nao esta establizado (diferenca de 2)
  75.     {
  76.       if ((val - pos) > 0)                              // Se deve mover FW
  77.       {
  78.         stepper.step(1);                                // Move a step toward the pot reading
  79.         pos++;                                          // put a couple of plus characters after pos.
  80.       }
  81.       if ((val - pos) < 0)                              // Se deve mover BW
  82.       {
  83.         stepper.step(-1);                               // Move a step toward the pot reading
  84.         pos--;                                          // put a couple of plus characters after pos.
  85.       }
  86.     }
  87.     if (abs(val - pos) == 0)                            // Se chegou no ponto solicitado FW
  88.     {
  89.       estabiliza = 1;                                   // Informa establizdo
  90.     }
  91.     if (estabiliza == 1)                                // Se está establizado
  92.     {
  93.       digitalWrite(COIL1, LOW);                         // Turn Off coils to reduce motor heating
  94.       digitalWrite(COIL2, LOW);
  95.       digitalWrite(COIL3, LOW);
  96.       digitalWrite(COIL4, LOW);
  97.       if (abs(val - pos) > 2)                           // Se necessita mover + que 2
  98.       {
  99.         estabiliza = 0;                                 // Desliga establizado
  100.       }
  101.     }
  102.   }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement