Advertisement
Guest User

reciever_uno

a guest
Feb 1st, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <nRF24L01.h>
  3. #include <RF24.h>
  4. #include <CheapStepper.h> //Library can be found here: https://github.com/tyhenry/CheapStepper
  5.  
  6. const unsigned short int MAIN_DELAY = 1000;
  7. const unsigned short int requiredStep = 2048;
  8. const int CE = 6;
  9. const int CSN = 7;
  10. int joystickData[3] = {0};
  11. int leftLimitSwitch = A1;
  12. int rightLimitSwitch = A2;
  13. int leftEndSwitchState = 0;
  14. int rightEndSwitchState = 0;
  15. const int speedSetpoint1 = 1;
  16. const int speedSetpoint2 = 2;
  17. unsigned int speedValue = 1;
  18. int rotateStepperSetpointSlow = 20; // RPM can be set from 1RPM to 15RPM
  19. int rotateStepperSetpointFast = 25; // RPM can be set from 1RPM to 15RPM
  20. bool joystickButtonPressed = 0;
  21. CheapStepper motor (8,9,10,11); //Create 28BYJ-48 object. Pins might be different. See description on https://github.com/tyhenry/CheapStepper
  22.  
  23. RF24 radio(CE, CSN); // CE, CSN
  24. const byte address[6] = "00001";
  25.  
  26. void set_speed()
  27. {
  28. if(speedValue == speedSetpoint1)
  29. {
  30. motor.setRpm(rotateStepperSetpointSlow);
  31. speedValue = speedSetpoint2;
  32. }
  33. else
  34. {
  35. motor.setRpm(rotateStepperSetpointFast);
  36. speedValue = speedSetpoint1;
  37. }
  38. }
  39.  
  40. void setup()
  41. {
  42. pinMode(leftLimitSwitch, INPUT);
  43. digitalWrite(leftLimitSwitch, HIGH);
  44. pinMode(rightLimitSwitch, INPUT);
  45. digitalWrite(rightLimitSwitch, HIGH);
  46. Serial.begin(9600);
  47. motor.setRpm(rotateStepperSetpointSlow); // RPM can be set from 1RPM to 15RPM
  48. radio.begin();
  49. radio.openReadingPipe(0, address);
  50. radio.setPALevel(RF24_PA_MIN);
  51. radio.startListening();
  52. }
  53. void loop()
  54. {
  55. leftEndSwitchState = digitalRead(leftLimitSwitch);
  56. rightEndSwitchState = digitalRead(rightLimitSwitch);
  57. Serial.println(leftEndSwitchState);
  58. Serial.println(rightEndSwitchState);
  59. Serial.println("==================");
  60.  
  61. /*
  62. Start reading from NRF module
  63. */
  64. if ( radio.available())
  65. {
  66. radio.read(&joystickData, sizeof(joystickData)); //read data
  67. Serial.println(joystickData[0]); // X-Position
  68. Serial.println(joystickData[1]); // Y-position
  69. Serial.println(joystickData[2]); // SW Button
  70. Serial.println("@@@@@@@@@@@@@");
  71.  
  72. /*
  73. Set speed dependently on SW button state
  74. */
  75. if(joystickData[2] == 1 && joystickButtonPressed == 0)
  76. {
  77. set_speed();
  78. joystickButtonPressed = 1;
  79. }
  80. if(joystickData[2] == 0)
  81. {
  82. joystickButtonPressed = 0;
  83. }
  84.  
  85. /*
  86. Move rotate stepper
  87. */
  88. /*
  89. if(joystickData[1] > 800)
  90. {
  91. Serial.println("Rotating motor to ClockWise");
  92. motor.moveTo (true, requiredStep);
  93. } else if(joystickData[1] < 300)
  94. {
  95. Serial.println("Rotating motor to CounterClockWise");
  96. motor.moveTo(false, requiredStep);
  97. }else
  98. {
  99. Serial.print("No motor action. Value of Y-position is:");
  100. Serial.println(joystickData[1]);
  101. }
  102. */
  103.  
  104. }
  105. motor.moveTo(true, 2048);
  106. if(MAIN_DELAY !=0)
  107. {delay(MAIN_DELAY);}
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement