pleasedontcode

Potentiometer Velocity rev_04

Oct 16th, 2025
429
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: Potentiometer Velocity
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2025-10-16 18:27:06
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* read velocity speed and and adjust the servo */
  21.     /* position to maintain the same velocity speed of */
  22.     /* 100km/h. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Servo.h>  //https://github.com/arduino-libraries/Servo
  30. #include <Keypad.h> //https://github.com/Chris--A/Keypad
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t keypad_Keypad4x4_R1_PIN_D2        = 2;
  38. const uint8_t keypad_Keypad4x4_R2_PIN_D4        = 4;
  39. const uint8_t keypad_Keypad4x4_R3_PIN_D5        = 5;
  40. const uint8_t keypad_Keypad4x4_R4_PIN_D6        = 6;
  41.  
  42. /***** DEFINITION OF ANALOG INPUT PINS *****/
  43. const uint8_t pot_Potentiometer_Vout_PIN_A0     = A0;
  44. const uint8_t pot_Potentiometer_Vout_PIN_A1     = A1;
  45.  
  46. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  47. const uint8_t keypad_Keypad4x4_C1_PIN_D7        = 7;
  48. const uint8_t keypad_Keypad4x4_C2_PIN_D8        = 8;
  49. const uint8_t keypad_Keypad4x4_C3_PIN_D9        = 9;
  50.  
  51. /***** DEFINITION OF PWM OUTPUT PINS *****/
  52. const uint8_t actuator_Servomotor_PWMSignal_PIN_D3      = 3;
  53.  
  54. /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
  55. const uint8_t SEGMENT_POINTS_voltage_velocity_PIN_A0    = 5;
  56. const float voltage_velocity_PIN_A0_lookup[2][SEGMENT_POINTS_voltage_velocity_PIN_A0] =
  57. {
  58.     {0.2    ,   0.8 ,   2.0 ,   3.0 ,   4.0},   //Voltage [V]
  59.     {10.0   ,   30.0    ,   60.0    ,   100.0   ,   250.0}  //velocity [km/h]
  60. };
  61.  
  62.  
  63. const uint8_t KEYPAD4X4_ROWS = 4; //four rows
  64. const uint8_t KEYPAD4X4_COLS = 4; //four columns
  65. char hexaKeys_Keypad4x4[KEYPAD4X4_ROWS][KEYPAD4X4_COLS] = {
  66.     {'1','2','3','A'},
  67.     {'4','5','6','B'},
  68.     {'7','8','9','C'},
  69.     {'*','0','#','D'},
  70. };
  71.  
  72. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  73.  
  74. // Servo object to control the motor
  75. Servo actuator;
  76.  
  77. // Optional keypad instance placeholders (not used in control loop but kept for compatibility)
  78. // The original project includes keypad libraries; instantiate only if needed.
  79.  
  80. float velocityFromVoltageLookup(float voltage){
  81.     static const float volts[SEGMENT_POINTS_voltage_velocity_PIN_A0] = {
  82.         voltage_velocity_PIN_A0_lookup[0][0],
  83.         voltage_velocity_PIN_A0_lookup[0][1],
  84.         voltage_velocity_PIN_A0_lookup[0][2],
  85.         voltage_velocity_PIN_A0_lookup[0][3],
  86.         voltage_velocity_PIN_A0_lookup[0][4]
  87.     };
  88.     static const float vel[SEGMENT_POINTS_voltage_velocity_PIN_A0] = {
  89.         voltage_velocity_PIN_A0_lookup[1][0],
  90.         voltage_velocity_PIN_A0_lookup[1][1],
  91.         voltage_velocity_PIN_A0_lookup[1][2],
  92.         voltage_velocity_PIN_A0_lookup[1][3],
  93.         voltage_velocity_PIN_A0_lookup[1][4]
  94.     };
  95.  
  96.     if (voltage <= volts[0]) return vel[0];
  97.     for (int i = 0; i < SEGMENT_POINTS_voltage_velocity_PIN_A0 - 1; i++) {
  98.         float v0 = volts[i];
  99.         float v1 = volts[i+1];
  100.         if (voltage <= v1) {
  101.             float t = (voltage - v0) / (v1 - v0);
  102.             return vel[i] + t * (vel[i+1] - vel[i]);
  103.         }
  104.     }
  105.     return vel[SEGMENT_POINTS_voltage_velocity_PIN_A0 - 1];
  106. }
  107.  
  108. float readVelocityFromPotVoltage(){
  109.     int raw = analogRead(pot_Potentiometer_Vout_PIN_A0);
  110.     float voltage = (raw * 5.0f) / 1023.0f; // convert ADC to voltage [V]
  111.     return velocityFromVoltageLookup(voltage);
  112. }
  113.  
  114. void setup(void)
  115. {
  116.     pinMode(keypad_Keypad4x4_R1_PIN_D2,     INPUT_PULLUP);
  117.     pinMode(keypad_Keypad4x4_R2_PIN_D4,     INPUT_PULLUP);
  118.     pinMode(keypad_Keypad4x4_R3_PIN_D5,     INPUT_PULLUP);
  119.     pinMode(keypad_Keypad4x4_R4_PIN_D6,     INPUT_PULLUP);
  120.     pinMode(pot_Potentiometer_Vout_PIN_A0,  INPUT);
  121.     pinMode(pot_Potentiometer_Vout_PIN_A1,  INPUT);
  122.  
  123.     pinMode(keypad_Keypad4x4_C1_PIN_D7,  OUTPUT);
  124.     pinMode(keypad_Keypad4x4_C2_PIN_D8,  OUTPUT);
  125.     pinMode(keypad_Keypad4x4_C3_PIN_D9,  OUTPUT);
  126.    
  127.     pinMode(actuator_Servomotor_PWMSignal_PIN_D3,    OUTPUT);
  128.  
  129.     actuator.attach(actuator_Servomotor_PWMSignal_PIN_D3);
  130.     actuator.write(90);
  131. }
  132.  
  133. void loop(void)
  134. {
  135.     float measuredVelocity = readVelocityFromPotVoltage();
  136.     const float desiredVelocity = 100.0f; // km/h
  137.     float error = desiredVelocity - measuredVelocity;
  138.     const float Kp = 0.8f; // proportional gain
  139.     float deltaAngle = Kp * error;
  140.     static int servoAngle = 90;
  141.     servoAngle = 90 + (int)deltaAngle;
  142.     if (servoAngle < 0) servoAngle = 0;
  143.     if (servoAngle > 180) servoAngle = 180;
  144.     actuator.write(servoAngle);
  145.  
  146.     delay(50);
  147. }
  148.  
  149. /* END CODE */
  150.  
Advertisement
Add Comment
Please, Sign In to add comment