Advertisement
MrLunk

Arduino + 1Sheeld + IBT_2 RC-Car Control

Jan 26th, 2018
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.60 KB | None | 0 0
  1. #define CUSTOM_SETTINGS
  2. #define INCLUDE_GAMEPAD_SHIELD
  3.  
  4. /* Include 1Sheeld library. */
  5. #include <OneSheeld.h>
  6. #include <Servo.h>
  7. #include <Wire.h>
  8.  
  9. Servo SteeringServo;
  10. int MaxSteerAngle = 30;
  11. int Speed = 0;
  12. int MaxSpeed = 100;
  13.  
  14. int RPWM_Output = 5; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
  15. int LPWM_Output = 6; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)
  16.  
  17. void setup() {
  18.   Serial.begin(115200);
  19.   OneSheeld.begin();
  20.  
  21.   pinMode(RPWM_Output, OUTPUT);
  22.   pinMode(LPWM_Output, OUTPUT);
  23.  
  24.   SteeringServo.attach(7);
  25. }
  26.  
  27. void loop() {
  28.  
  29.  
  30.  
  31.   if (GamePad.isUpPressed()) {            // Forwards
  32.     analogWrite(LPWM_Output, 0);
  33.     analogWrite(RPWM_Output, Speed);
  34.   }
  35.  
  36.   if (GamePad.isDownPressed()) {
  37.     analogWrite(LPWM_Output, Speed);          // Backwards
  38.     analogWrite(RPWM_Output, 0);
  39.   }
  40.  
  41.   if (GamePad.isRightPressed()) {         // Steer Left
  42.     SteeringServo.write(90 + MaxSteerAngle);
  43.   }
  44.  
  45.   if (GamePad.isLeftPressed()) {          // Steer Right
  46.     SteeringServo.write(90 - MaxSteerAngle);
  47.   }
  48.  
  49.   if (GamePad.isRedPressed()) {           // Stop all Engines
  50.     analogWrite(LPWM_Output, 0);
  51.     analogWrite(RPWM_Output, 0);
  52.   }
  53.  
  54.   if (GamePad.isBluePressed()) {           // Steer Straight
  55.     SteeringServo.write(90);
  56.   }
  57.  
  58.   if (GamePad.isOrangePressed()) {        // Speed +
  59.     Speed = Speed + 10;
  60.     if (Speed >= MaxSpeed) {
  61.       Speed = MaxSpeed;
  62.     }
  63.     delay(100);
  64.   }
  65.  
  66.   if (GamePad.isGreenPressed()) {         // Speed -
  67.     Speed = Speed - 10;
  68.     if (Speed <= 0) {
  69.       Speed = 0;
  70.     }
  71.     delay(100);
  72.   }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement