CG_MarioKart.ino 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 ch3;
  15. int move; // Forward/Back speed
  16. int turn; // Turning Factor
  17. int pwm_a = 4; //PWM control for motor outputs
  18. int pwm_b = 7; //PWM control for motor outputs
  19. int dir_a = 5; //direction control for motor outputs
  20. int dir_b = 6; //direction control for motor outputs
  21. int threshold = 30 ;
  22. int middleThreshold (int RCvalue, int threshold) { //
  23. if ( (RCvalue < 1500 + threshold) && (RCvalue > 1500 - threshold) ) { RCvalue = 1500;}
  24. return RCvalue ;
  25. }
  26. void setup() {
  27. pinMode(ch1_pin, INPUT); // Set our input pins as such
  28. pinMode(ch2_pin, INPUT);
  29. Serial.begin(9600); // Pour a bowl of Serial (for debugging)
  30. pinMode(pwm_a, OUTPUT); //Set control pins to be outputs
  31. pinMode(pwm_b, OUTPUT);
  32. pinMode(dir_a, OUTPUT);
  33. pinMode(dir_b, OUTPUT);
  34. analogWrite(pwm_a, 0);
  35. analogWrite(pwm_b, 0);
  36. }
  37. void loop() {
  38. ch1 = pulseIn(ch1_pin, HIGH, 25000); // Read the pulse width of
  39. ch2 = pulseIn(ch2_pin, HIGH, 25000); // each channel
  40. ch1 = middleThreshold(ch1, threshold);
  41. ch2 = middleThreshold(ch2, threshold);
  42. move = map(ch2, 1000,2000, -255, 255); //center over zero
  43. move = constrain(move, -255, 255); //only pass values whose absolutes are
  44. //valid pwm values
  45. /*What we're doing here is determining whether we want to move
  46. forward or backward*/
  47. if(move>0){digitalWrite(dir_a, 1);digitalWrite(dir_b, 1);};
  48. if(move<0){digitalWrite(dir_a, 0);digitalWrite(dir_b, 0); move=abs(move);};
  49. /*Here we're determining whether a left or a right turn is being
  50. executed*/
  51. turn = map(ch1,1000,2000,-255,255);
  52. turn = constrain(turn, -255, 255);
  53. /*This is where we do some mixing, by subtracting our "turn"
  54. variable from the appropriate motor's speed we can execute
  55. a turn in either direction*/
  56. if(turn>=0){analogWrite(pwm_b, move-turn); analogWrite(pwm_a, move);};
  57. if(turn<0){turn=abs(turn); analogWrite(pwm_a, move-turn); analogWrite(pwm_b, move);};
  58. Serial.print("move:"); //Serial debugging stuff
  59. Serial.println(move);
  60. Serial.print("turn:"); //Serial debugging stuff
  61. Serial.println(turn);
  62. Serial.print("move-turn:"); //Serial debugging stuff
  63. Serial.println(move-turn);
  64. Serial.println(); //Serial debugging stuff
  65. Serial.println();
  66. Serial.println();
  67. }