Advertisement
KollescheKopfkrank

Untitled

Mar 23rd, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. /*
  2. Tutorial von
  3. http://mertarduinotutorial.blogspot.de/2017/04/arduino-remote-controlled-tank-with.html
  4.  
  5. ---- Receiver Code ----
  6. Mert Arduino Tutorial & Projects (YouTube)
  7. Please Subscribe for Support
  8. */
  9.  
  10. #include <Servo.h> //die Bibliothek, die uns hilft, den Servomotor zu steuern
  11. #include <SPI.h> //die Kommunikationsschnittstelle mit dem Modem
  12. #include "RF24.h" //die Bibliothek, mit der wir das Funkmodem steuern können (nRF24L)
  13.  
  14. //define our L298N control pins
  15. //Motor A
  16. const int RightMotorForward = 2; // IN1
  17. const int RightMotorBackward = 3; // IN2
  18. //Motor B
  19. const int LeftMotorForward = 4; // IN3
  20. const int LeftMotorBackward = 6; // IN4
  21.  
  22. //Definieren Sie den Servonamen
  23. Servo myServo;
  24.  
  25.  
  26. RF24 radio(5,10); /*Dieses Objekt stellt ein Modem dar, das mit dem Arduino verbunden ist.
  27. Die Argumente 5 und 10 sind digitale Pin-Nummern, an die sich Signale anschließen
  28. CE und CSN sind verbunden.*/
  29.  
  30. const uint64_t pipe = 0xE8E8F0F0E1LL; //die Adresse des Modems, das Daten vom Arduino empfängt.
  31.  
  32. int data[1];
  33.  
  34.  
  35. void setup(){
  36. pinMode(RightMotorForward, OUTPUT);
  37. pinMode(LeftMotorForward, OUTPUT);
  38. pinMode(LeftMotorBackward, OUTPUT);
  39. pinMode(RightMotorBackward, OUTPUT);
  40.  
  41. //Definieren Sie die Servo-Eingangspins
  42. myServo.attach(14); //A0
  43.  
  44. radio.begin(); //Es aktiviert das Modem.
  45. radio.openReadingPipe(1, pipe); //bestimmt die Adresse Ihres Modems, die Daten empfängt.
  46. radio.startListening(); //Aktivieren Sie den Empfang von Daten über Modem
  47. }
  48.  
  49. void loop(){
  50. if(radio.available()){
  51. radio.read(data, 1);
  52.  
  53. if(data[0] < 11 && data[0] > 6){
  54. // Das ist rückwärts
  55. // Stellen Sie einen Motor A rückwärts ein
  56. digitalWrite(RightMotorForward, LOW);
  57. digitalWrite(RightMotorBackward, HIGH);
  58. // Stellen Sie einen Motor B rückwärts ein
  59. digitalWrite(LeftMotorForward, LOW);
  60. digitalWrite(LeftMotorBackward, HIGH);
  61. }
  62. if(data[0] > -1 && data[0] < 4){
  63. // Das ist vorwärts
  64. // Setze einen Motor A vorwärts
  65. digitalWrite(RightMotorForward, HIGH);
  66. digitalWrite(RightMotorBackward, LOW);
  67. // Setze einen Motor B vorwärts
  68. digitalWrite(LeftMotorForward, HIGH);
  69. digitalWrite(LeftMotorBackward, LOW);
  70. }
  71. if (data[0] == 5){
  72. // Stopp Motoren
  73. digitalWrite(RightMotorForward, LOW);
  74. digitalWrite(RightMotorBackward, LOW);
  75. digitalWrite(LeftMotorForward, LOW);
  76. digitalWrite(LeftMotorBackward, LOW);
  77. }
  78. // Das ist Rückwärts
  79. // Setze einen Motor A rückwärts
  80. if(data[0] < 21 && data[0] > 16){
  81. digitalWrite(RightMotorForward, HIGH);
  82. digitalWrite(RightMotorBackward, LOW);
  83. // Setze einen Motor A rückwärts
  84. digitalWrite(LeftMotorForward, LOW);
  85. digitalWrite(LeftMotorBackward, HIGH);
  86. }
  87. // Biegen Sie rechts ab
  88. if(data[0] > 10 && data[0] < 14){
  89. digitalWrite(RightMotorForward, LOW);
  90. digitalWrite(RightMotorBackward, HIGH);
  91. digitalWrite(LeftMotorForward, HIGH);
  92. digitalWrite(LeftMotorBackward, LOW);
  93. }
  94. // Biegen Sie links ab
  95. if(data[0] == 15){
  96. digitalWrite(RightMotorForward, LOW);
  97. digitalWrite(RightMotorBackward, LOW);
  98. digitalWrite(LeftMotorForward, LOW);
  99. digitalWrite(LeftMotorBackward, LOW);
  100. }
  101. // für den Servomotor
  102. if(data[0] < 31 && data[0] > 21){
  103. int potValue = data[0];
  104. int potPos = map(potValue, 21, 30, 10, 170);
  105. myServo.write(potPos);
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement