Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. #include <RHReliableDatagram.h>
  2. #include <RH_NRF24.h>
  3. #include <SPI.h>
  4.  
  5. #define joyV A0
  6. #define joyH A1
  7. #define CLIENT_ADDRESS 1
  8. #define SERVER_ADDRESS 2
  9.  
  10. int joyposV = 512;
  11. int joyposH = 512;
  12. int Ser = 0;
  13.  
  14. RH_NRF24 RadioDriver;
  15. RHReliableDatagram RadioManager(RadioDriver, CLIENT_ADDRESS);
  16.  
  17. uint8_t motorcontrol[3];
  18. uint8_t SerDir;
  19. uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
  20.  
  21. void setup() {
  22. // put your setup code here, to run once:
  23. pinMode(3,INPUT);
  24. if(!RadioManager.init())
  25. motorcontrol[2] = 0;
  26. Serial.begin(9600);
  27. }
  28.  
  29. void loop() {
  30. // put your main code here, to run repeatedly:
  31. joyposV = analogRead(joyV);
  32. joyposH = analogRead(joyH);
  33.  
  34. if(joyposV < 460){
  35. motorcontrol[2] = 1;
  36.  
  37. motorcontrol[0] = map(joyposV, 460, 0, 0, 255);
  38. motorcontrol[1] = map(joyposV, 460, 0, 0, 255);
  39. }
  40. else if(joyposV > 564){
  41. motorcontrol[2] = 0;
  42.  
  43. motorcontrol[0] = map(joyposV, 564, 1023, 0, 255);
  44. motorcontrol[1] = map(joyposV, 564, 1023, 0, 255);
  45. }
  46. else{
  47. motorcontrol[0] = 0;
  48. motorcontrol[1] = 0;
  49. motorcontrol[2] = 0;
  50. }
  51.  
  52. if(joyposH < 460){
  53. joyposH = map(joyposH, 460, 0, 0, 255);
  54.  
  55. motorcontrol[0] = motorcontrol[0] - joyposH;
  56. motorcontrol[1] = motorcontrol[1] + joyposH;
  57.  
  58. if(motorcontrol[0] < 0)motorcontrol[0] = 0;
  59. if(motorcontrol[1] > 255)motorcontrol[1] = 255;
  60. }
  61. else if(joyposH > 564){
  62. joyposH = map(joyposH, 564, 1023, 0, 255);
  63.  
  64. motorcontrol[0] = motorcontrol[0] + joyposH;
  65. motorcontrol[1] = motorcontrol[1] - joyposH;
  66.  
  67. if(motorcontrol[0] > 255)motorcontrol[0] = 255;
  68. if(motorcontrol[1] < 0)motorcontrol[1] = 0;
  69. }
  70.  
  71. if(motorcontrol[0] < 8)motorcontrol[0] = 0;
  72. if(motorcontrol[1] < 8)motorcontrol[1] = 0;
  73.  
  74. if(digitalRead(3) == HIGH){
  75. SerDir = Ser + 45;
  76. }
  77. else{
  78. SerDir = 0;
  79. }
  80.  
  81. if (SerDir < 0)SerDir = 0;
  82. if (SerDir > 45)SerDir = 45;
  83.  
  84. if(RadioManager.sendtoWait(motorcontrol, SerDir, sizeof(motorcontrol), sizeof(SerDir), SERVER_ADDRESS)){
  85. uint8_t len = sizeof(buf);
  86. uint8_t from;
  87. if(RadioManager.recvfromAckTimeout(buf, &len, 2000, &from)){
  88. Serial.print("Got reply");
  89. }
  90. else{
  91. Serial.print("No reply");
  92. }
  93. }
  94. delay(50);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement