Advertisement
Guest User

Untitled

a guest
Mar 8th, 2017
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.58 KB | None | 0 0
  1. // NRF24 based radio controller for radio controlled tractors
  2. // by modelleicher
  3. // NRF24 Library: https://github.com/TMRh20/RF24
  4. // Inspiration by: "RC Tractor Guy" on youtube https://www.youtube.com/user/magicalmachines/videos
  5. // Also great thanks to Antony Cartwright for the best NRF24 tutorial on youtube (bare basics explanation) https://www.youtube.com/watch?v=Qn2YLSYz3WM
  6.  
  7. // work in progress, just a start for now.
  8. // February 2017
  9.  
  10.  
  11. #include <SPI.h>
  12. #include "RF24.h"
  13.  
  14.  
  15. // 12 possible bytes for now.. Always send one more byte than the receiver awaits, for some reason.. Otherwise it doesn't work!?
  16. uint8_t dataPackage[12];
  17.  
  18. // the raw input values, analogRead will be stored in those.
  19. int raw_X, raw_Y, raw_RX, raw_RY = 512;
  20.  
  21. // speed range, use three possible speed ranges for finer control
  22. byte drive_speedRanges[] = {100, 50, 0};
  23. byte drive_curSpeedRange = 2;
  24.  
  25. //
  26. // 0 - 255
  27. // 50 - 205
  28. // 100 - 155
  29.  
  30. // byte to store the raw adress values in
  31. byte raw_address = 0;
  32. // byte to store the raw values of up to 16 buttons
  33. byte raw_buttonByte1, raw_buttonByte2 = 0;
  34.  
  35. // analogRead value for the 6 buttons
  36. int anRead = 0;
  37.  
  38. // final state (on/off of the buttons) and the variables needet for software debounce
  39. byte final_buttonByte1, final_buttonByte2 = 0;
  40. byte lastButtonOnTime1[8];
  41. byte lastButtonOnTime2[8];
  42. byte lastFinal_buttonByte1, lastFinal_buttonByte2;
  43.  
  44.  
  45. int dt = 0;
  46. int lastMillis = 0;
  47.  
  48. // initialize the RF24 library. Pin 9 and 10 are used for CE and CSN.
  49. RF24 radio(9, 10);
  50.  
  51. // Most codes use some crazy 64 bit long weird number as a pipe. But "1" works just fine..
  52. const uint64_t pipe = 1;
  53.  
  54.  
  55. void setup() {
  56.   // serial is only used for debugging. Uncomment if no debugging is needed.
  57.   Serial.begin(9600);
  58.   // begin radio
  59.   radio.begin();
  60.   // i use PA_MIN for now.. It is good for at least 2m.. Only use higher levels if your power supply can support it and you have a proper Capacitor across the VCC and GND pins on the NRF module. (I suggest at least 100ยตF)
  61.   radio.setPALevel(RF24_PA_MIN);
  62.   // debug
  63.   Serial.println("started..");
  64.   // open the writing pipe since we are a sender
  65.   radio.openWritingPipe(pipe);
  66.   // debug
  67.   Serial.println("opened..");
  68.  
  69.   pinMode(5, INPUT_PULLUP);
  70.   pinMode(6, INPUT_PULLUP);
  71.   pinMode(7, INPUT_PULLUP);
  72.   pinMode(8, INPUT_PULLUP);
  73.  
  74. }
  75.  
  76. void loop() {
  77.   // delta time, time since last loop
  78.   dt = millis() - lastMillis;
  79.   lastMillis = millis();
  80.  
  81.  
  82.   // read the 4 analog values and save them in the variables
  83.   raw_X = analogRead(1);
  84.   raw_Y = analogRead(0);
  85.   raw_RX = analogRead(3);
  86.   raw_RY = analogRead(2);
  87.  
  88.   // map the raw values and store them in the dataPackage
  89.   dataPackage[1] = map(raw_X, 0, 1023, 0, 255);
  90.   dataPackage[2] = map(raw_Y, 0, 1023, 0, 255);
  91.   dataPackage[3] = map(raw_RX, 0, 1023, 0, 255);
  92.   dataPackage[4] = map(raw_RY, 0, 1023, 0 + drive_speedRanges[drive_curSpeedRange], 255 - drive_speedRanges[drive_curSpeedRange]);
  93.  
  94.   // get the current adress from the jumpers (pin 5 to 8) and write to the adress byte
  95.   // since they use internal pullup we invert the digitalRead value
  96.   bitWrite(raw_adress, 0, !digitalRead(5));
  97.   bitWrite(raw_adress, 1, !digitalRead(6));
  98.   bitWrite(raw_adress, 2, !digitalRead(7));
  99.   bitWrite(raw_adress, 3, !digitalRead(8));
  100.  
  101.   dataPackage[0] = raw_address;
  102.  
  103.   // get the Joystick buttons
  104.   bitWrite(raw_buttonByte1, 0, digitalRead(2));
  105.   bitWrite(raw_buttonByte1, 1, digitalRead(3));
  106.  
  107.   // get 6 buttons from one analog pin using voltage dividers and analog read.
  108.   anRead = analogRead(A7);
  109.  
  110.   if (anRead < 560 && anRead > 490) {
  111.     raw_buttonByte2 = B00000000;
  112.   }
  113.   else if (anRead < 20) {
  114.     raw_buttonByte2 = B00000001;
  115.   }
  116.   else if (anRead < 270 && anRead > 200) {
  117.     raw_buttonByte2 = B00000010;
  118.   }
  119.   else if (anRead < 450 && anRead > 390) {
  120.     raw_buttonByte2 = B00000100;
  121.   }
  122.   else if (anRead < 660 && anRead > 585) {
  123.     raw_buttonByte2 = B00001000;
  124.   }
  125.   else if (anRead < 820 && anRead > 720) {
  126.     raw_buttonByte2 = B00010000;
  127.   }
  128.   else if (anRead > 900) {
  129.     raw_buttonByte2 = B00100000;
  130.   }
  131.  
  132.   // button debouncing for both button bytes and storing them in on/off variables
  133.   for (byte i = 0; i < 8; i++)
  134.   {
  135.     if (bitRead(raw_buttonByte1, i) == HIGH)
  136.     {
  137.       lastButtonOnTime1[i] = lastButtonOnTime1[i] + dt;
  138.       if (lastButtonOnTime1[i] > 200)
  139.       {
  140.         bitWrite(final_buttonByte1, i, !bitRead(final_buttonByte1, i));
  141.         lastButtonOnTime1[i] = 0;
  142.       }
  143.     }
  144.     else
  145.     {
  146.       lastButtonOnTime1[i] = 0;
  147.     }
  148.  
  149.   }
  150.  
  151.   for (byte i = 0; i < 8; i++)
  152.   {
  153.     if (bitRead(raw_buttonByte2, i) == HIGH)
  154.     {
  155.       lastButtonOnTime2[i] = lastButtonOnTime2[i] + dt;
  156.       if (lastButtonOnTime2[i] > 200)
  157.       {
  158.         bitWrite(final_buttonByte2, i, !bitRead(final_buttonByte2, i));
  159.         lastButtonOnTime2[i] = 0;
  160.       }
  161.     }
  162.     else
  163.     {
  164.       lastButtonOnTime2[i] = 0;
  165.     }
  166.  
  167.   }
  168.  
  169.  
  170.   // apply the max speed value change (use least significant bit of buttonByte2 for now)
  171.   if (final_buttonByte2 != lastFinal_buttonByte2)
  172.   {
  173.     drive_curSpeedRange = drive_curSpeedRange + 1;
  174.     if (drive_curSpeedRange > 2) {
  175.       drive_curSpeedRange = 0;
  176.     }
  177.   }
  178.  
  179.  
  180.   // update the "last" values
  181.   lastFinal_buttonByte1 = final_buttonByte1;
  182.   lastFinal_buttonByte2 = final_buttonByte2;
  183.  
  184.   // send the dataPackage
  185.   radio.write(dataPackage, sizeof(dataPackage));
  186.   //debug
  187.   Serial.println(final_buttonByte2, BIN);
  188.   Serial.println(dataPackage[4]);
  189.   //Serial.println(raw_adress);
  190.   // wait for a short amount of time.. Not sure if needed.
  191.   delay(10);
  192.   // debug
  193.  
  194.  
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement