Advertisement
pleasedontcode

Flight Controller rev_01

Mar 14th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Flight Controller
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-03-14 07:44:18
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* brushed motors flight contoller */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24.  
  25. /****** FUNCTION PROTOTYPES *****/
  26. void setup(void);
  27. void loop(void);
  28.  
  29. /***** DEFINITION OF ANALOG INPUT PINS *****/
  30. const uint8_t nn_Potentiometer_Vout_PIN_A0 = A0;
  31.  
  32. /****** SYSTEM REQUIREMENTS *****/
  33. /****** SYSTEM REQUIREMENT 1 *****/
  34. /* brushed motors flight controller */
  35.  
  36. /* Define the Pins for Motor Control */
  37. const uint8_t motor1Pin1 = 2;
  38. const uint8_t motor1Pin2 = 3;
  39. const uint8_t motor2Pin1 = 4;
  40. const uint8_t motor2Pin2 = 5;
  41.  
  42. void setup(void)
  43. {
  44.   // put your setup code here, to run once:
  45.  
  46.   pinMode(nn_Potentiometer_Vout_PIN_A0, INPUT);
  47.  
  48.   // Setup motor control pins as outputs
  49.   pinMode(motor1Pin1, OUTPUT);
  50.   pinMode(motor1Pin2, OUTPUT);
  51.   pinMode(motor2Pin1, OUTPUT);
  52.   pinMode(motor2Pin2, OUTPUT);
  53. }
  54.  
  55. void loop(void)
  56. {
  57.   // put your main code here, to run repeatedly:
  58.  
  59.   // Read the potentiometer value
  60.   int potValue = analogRead(nn_Potentiometer_Vout_PIN_A0);
  61.  
  62.   // Map the potentiometer value to motor speed range
  63.   int motorSpeed = map(potValue, 0, 1023, -255, 255);
  64.  
  65.   // Set the motor speeds based on the potentiometer value
  66.   if (motorSpeed >= 0)
  67.   {
  68.     analogWrite(motor1Pin1, motorSpeed);
  69.     analogWrite(motor1Pin2, 0);
  70.     analogWrite(motor2Pin1, motorSpeed);
  71.     analogWrite(motor2Pin2, 0);
  72.   }
  73.   else
  74.   {
  75.     analogWrite(motor1Pin1, 0);
  76.     analogWrite(motor1Pin2, abs(motorSpeed));
  77.     analogWrite(motor2Pin1, 0);
  78.     analogWrite(motor2Pin2, abs(motorSpeed));
  79.   }
  80.  
  81.   delay(100);
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement