Ein Roboter mit bürstenlosem Antrieb, differenzial und NRF24L01 Funk. Großflächig gebaut um ein großes Solarpanel aufzunehmen. https://gitlab.informatik.hs-fulda.de/fdai5253/roboter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.6 KiB

  1. #include <MX1508.h>
  2. #define PINA 9
  3. #define PINB 10
  4. #define NUMPWM 2
  5. // MX1508 schematics(in Chinese) can be found here at: http://sales.dzsc.com/486222.html
  6. /*
  7. * MX1508(uint8_t pinIN1, uint8_t pinIN2, DecayMode decayMode, NumOfPwmPins numPWM);
  8. * DecayMode must be FAST_DECAY or SLOW_DECAY,
  9. * NumOfPwmPins, either use 1 or 2 pwm.
  10. * I recommend using 2 pwm pins per motor so spinning motor forward and backward gives similar response.
  11. * if using 1 pwm pin, make sure its pinIN1, then set pinIN2 to any digital pin. I dont recommend this setting because
  12. * we need to use FAST_DECAY in one direction and SLOW_DECAY for the other direction.
  13. */
  14. MX1508 motorA(PINA,PINB, FAST_DECAY, NUMPWM);
  15. void setup() {
  16. Serial.begin(115200);
  17. }
  18. /*
  19. * Ramp up to pwm = 100, by increasing pwm by 1 every 50 millisecond.
  20. * then ramp down to pwm = -100, by decreasing pwm every 50 millisecond.
  21. * positive value pwm results in forward direction.
  22. * negative value pwm results in opposite direction.
  23. */
  24. void loop() {
  25. // put your main code here, to run repeatedly:
  26. static unsigned long lastMilli = 0;
  27. static bool cwDirection = true; // assume initial direction(positive pwm) is clockwise
  28. static int pwm = 1;
  29. if(millis()-lastMilli > 50){ // every 50 millisecond
  30. if (cwDirection && pwm++ > 100 ) {
  31. cwDirection = false;
  32. } else if (!cwDirection && pwm-- < -100) {
  33. cwDirection = true;
  34. }
  35. motorA.motorGo(pwm);
  36. lastMilli = millis();
  37. Serial.println(motorA.getPWM()); // we can just print pwm but just showing that member function getPWM() works.
  38. }
  39. }