CG_MarioKart.ino 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. RC PulseIn Joystick Servo Control
  3. By: Nick Poole
  4. SparkFun Electronics
  5. Date: 5
  6. License: CC-BY SA 3.0 - Creative commons share-alike 3.0
  7. use this code however you'd like, just keep this license and
  8. attribute. Let me know if you make hugely, awesome, great changes.
  9. */
  10. const int ch1_pin = 10 ;
  11. const int ch2_pin = 11 ;
  12. int ch1; // Here's where we'll keep our channel values
  13. int ch2;
  14. int move; // Forward/Back speed
  15. int turn; // Turning Factor
  16. int speeds_pin[2] = {5, 6};
  17. int dirs_pin[2] = {4, 7};
  18. //int pwm_a = 5; //PWM control for motor outputs
  19. //int pwm_b = 6; //PWM control for motor outputs
  20. //int dir_a = 4; //direction control for motor outputs
  21. //int dir_b = 7; //direction control for motor outputs
  22. int threshold = 30 ;
  23. int middleThreshold (int RCvalue, int threshold) { //
  24. if ( (RCvalue < 1500 + threshold) && (RCvalue > 1500 - threshold) ) { RCvalue = 1500;}
  25. return RCvalue ;
  26. }
  27. void setup() {
  28. pinMode(ch1_pin, INPUT); // Set our input pins as such
  29. pinMode(ch2_pin, INPUT);
  30. Serial.begin(9600); // Pour a bowl of Serial (for debugging)
  31. for(int i; i<2; i++){
  32. pinMode(speeds_pin[i], OUTPUT);
  33. pinMode(dirs_pin[i], OUTPUT);
  34. analogWrite(speeds_pin[i], 0) ;
  35. }
  36. // pinMode(pwm_a, OUTPUT); //Set control pins to be outputs
  37. // pinMode(pwm_b, OUTPUT);
  38. // pinMode(dir_a, OUTPUT);
  39. // pinMode(dir_b, OUTPUT);
  40. // analogWrite(pwm_a, 0);
  41. // analogWrite(pwm_b, 0);
  42. }
  43. void loop() {
  44. //Read RC values and give wide center point
  45. ch1 = pulseIn(ch1_pin, HIGH, 25000); // Read the pulse width of
  46. ch2 = pulseIn(ch2_pin, HIGH, 25000); // each channel
  47. ch1 = middleThreshold(ch1, threshold);
  48. ch2 = middleThreshold(ch2, threshold);
  49. //Refresh and scale raw move values
  50. move = map(ch2, 1000,2000, -255, 255); //center over zero
  51. move = constrain(move, -255, 255);
  52. // turn = map(ch1,1000,2000,-255,255);
  53. // turn = constrain(turn, -255, 255);
  54. turn = map(ch1,1000,2000,-255,255);
  55. Serial.print("move:"); //Serial debugging stuff
  56. Serial.println(move);
  57. Serial.print("turn:"); //Serial debugging stuff
  58. Serial.println(turn);
  59. //calculate each motor speed
  60. int speeds[2] = {0, 0} ;
  61. // int dirs[2] = {0, 0} ;
  62. int Dir = move>0 ? 1 : 0;
  63. move = abs(move) ;
  64. turn = constrain(turn, -move, move);
  65. if (turn>0) { speeds[0] = move; speeds[1] = move - turn ; }
  66. if (turn<=0) { speeds[0] = move + turn ; speeds[1] = move ;
  67. }
  68. // speeds[1] = move ;
  69. // speeds[0] = move ;
  70. Serial.print("speeds: "); //Serial debugging stuff
  71. Serial.print(speeds[0]);
  72. Serial.print(" - ");
  73. Serial.println(speeds[1]);
  74. //apply to driver
  75. for(int i =0; i<2; i++) {
  76. // dirs[i] = speeds[i] > 0 ? 1 : 0 ;
  77. speeds[i] = abs( speeds[i]);
  78. digitalWrite(dirs_pin[i], Dir) ;
  79. analogWrite(speeds_pin[i], speeds[i]) ;
  80. }
  81. // analogWrite (
  82. //
  83. //
  84. // /*This is where we do some mixing, by subtracting our "turn"
  85. // variable from the appropriate motor's speed we can execute
  86. // a turn in either direction*/
  87. // if(turn>=0){analogWrite(pwm_b, move-turn); analogWrite(pwm_a, move);};
  88. // if(turn<0){turn=abs(turn); analogWrite(pwm_a, move-turn); analogWrite(pwm_b, move);};
  89. //// analogWrite(pwm_b, move); analogWrite(pwm_a, move);
  90. Serial.print("A: "); //Serial debugging stuff
  91. Serial.print(speeds[0]);
  92. Serial.print(" - ");
  93. // Serial.println(dirs[0]);
  94. Serial.print("B: "); //Serial debugging stuff
  95. Serial.print(speeds[1]);
  96. Serial.print(" - ");
  97. // Serial.println(dirs[1]);
  98. Serial.println(); //Serial debugging stuff
  99. Serial.println();
  100. // Serial.println();
  101. }