Advertisement
Guest User

odbiornik

a guest
Aug 19th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define ledPin 11
  2. #define enA 9
  3. #define in1 4
  4. #define in2 5
  5. #define enB 10
  6. #define in3 6
  7. #define in4 7
  8. int xAxis, yAxis, state;
  9. unsigned int  x = 0;
  10. unsigned int  y = 0;
  11. int motorSpeedA = 0;
  12. int motorSpeedB = 0;
  13.  
  14. void setup() {
  15.   pinMode(enA, OUTPUT);
  16.   pinMode(enB, OUTPUT);
  17.   pinMode(in1, OUTPUT);
  18.   pinMode(in2, OUTPUT);
  19.   pinMode(in3, OUTPUT);
  20.   pinMode(in4, OUTPUT);
  21.   pinMode(ledPin, OUTPUT);
  22.   digitalWrite(ledPin, LOW);
  23.   Serial.begin(38400); // Default communication rate of the Bluetooth module
  24. }
  25. void loop() {
  26.   // Default value - no movement when the Joystick stays in the center
  27.   x = 510 / 4;
  28.   y = 510 / 4;
  29.   // Read the incoming data from the Joystick, or the master Bluetooth device
  30.   while (Serial.available() >= 3) {
  31.     x = Serial.read();
  32.     delay(10);
  33.     y = Serial.read();
  34.     delay(10);
  35.   state = Serial.read();
  36.    
  37.   }
  38.   delay(10);
  39.   // Convert back the 0 - 255 range to 0 - 1023, suitable for motor control code below
  40.   xAxis = x*4;
  41.   yAxis = y*4;
  42.   // Y-axis used for forward and backward control
  43.   if (yAxis < 470) {
  44.     // Set Motor A backward
  45.     digitalWrite(in1, HIGH);
  46.     digitalWrite(in2, LOW);
  47.     // Set Motor B backward
  48.     digitalWrite(in3, HIGH);
  49.     digitalWrite(in4, LOW);
  50.     // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
  51.     motorSpeedA = map(yAxis, 470, 0, 0, 255);
  52.     motorSpeedB = map(yAxis, 470, 0, 0, 255);
  53.   }
  54.   else if (yAxis > 550) {
  55.     // Set Motor A forward
  56.     digitalWrite(in1, LOW);
  57.     digitalWrite(in2, HIGH);
  58.     // Set Motor B forward
  59.     digitalWrite(in3, LOW);
  60.     digitalWrite(in4, HIGH);
  61.     // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
  62.     motorSpeedA = map(yAxis, 550, 1023, 0, 255);
  63.     motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  64.   }
  65.   // If joystick stays in middle the motors are not moving
  66.   else {
  67.     motorSpeedA = 0;
  68.     motorSpeedB = 0;
  69.   }
  70.   // X-axis used for left and right control
  71.   if (xAxis < 470) {
  72.     // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
  73.     int xMapped = map(xAxis, 470, 0, 0, 255);
  74.     // Move to left - decrease left motor speed, increase right motor speed
  75.     motorSpeedA = motorSpeedA - xMapped;
  76.     motorSpeedB = motorSpeedB + xMapped;
  77.     // Confine the range from 0 to 255
  78.     if (motorSpeedA < 0) {
  79.       motorSpeedA = 0;
  80.     }
  81.     if (motorSpeedB > 255) {
  82.       motorSpeedB = 255;
  83.     }
  84.   }
  85.   if (xAxis > 550) {
  86.     // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
  87.     int xMapped = map(xAxis, 550, 1023, 0, 255);
  88.     // Move right - decrease right motor speed, increase left motor speed
  89.     motorSpeedA = motorSpeedA + xMapped;
  90.     motorSpeedB = motorSpeedB - xMapped;
  91.     // Confine the range from 0 to 255
  92.     if (motorSpeedA > 255) {
  93.       motorSpeedA = 255;
  94.     }
  95.     if (motorSpeedB < 0) {
  96.       motorSpeedB = 0;
  97.     }
  98.   }
  99.   // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  100.   if (motorSpeedA < 70) {
  101.     motorSpeedA = 0;
  102.   }
  103.   if (motorSpeedB < 70) {
  104.     motorSpeedB = 0;
  105.   }
  106.   analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  107.   analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
  108.  
  109.   if (state == '1') {
  110.   digitalWrite(ledPin, HIGH); // LED ON
  111.  
  112.  }
  113.  else if (state == '0') {
  114.   digitalWrite(ledPin, LOW); // LED ON
  115.  
  116.  }
  117.  delay(20);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement