Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Potentiometer Velocity
- - Source Code NOT compiled for: Arduino Nano
- - Source Code created on: 2025-10-16 18:27:06
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* read velocity speed and and adjust the servo */
- /* position to maintain the same velocity speed of */
- /* 100km/h. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Servo.h> //https://github.com/arduino-libraries/Servo
- #include <Keypad.h> //https://github.com/Chris--A/Keypad
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t keypad_Keypad4x4_R1_PIN_D2 = 2;
- const uint8_t keypad_Keypad4x4_R2_PIN_D4 = 4;
- const uint8_t keypad_Keypad4x4_R3_PIN_D5 = 5;
- const uint8_t keypad_Keypad4x4_R4_PIN_D6 = 6;
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t pot_Potentiometer_Vout_PIN_A0 = A0;
- const uint8_t pot_Potentiometer_Vout_PIN_A1 = A1;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t keypad_Keypad4x4_C1_PIN_D7 = 7;
- const uint8_t keypad_Keypad4x4_C2_PIN_D8 = 8;
- const uint8_t keypad_Keypad4x4_C3_PIN_D9 = 9;
- /***** DEFINITION OF PWM OUTPUT PINS *****/
- const uint8_t actuator_Servomotor_PWMSignal_PIN_D3 = 3;
- /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
- const uint8_t SEGMENT_POINTS_voltage_velocity_PIN_A0 = 5;
- const float voltage_velocity_PIN_A0_lookup[2][SEGMENT_POINTS_voltage_velocity_PIN_A0] =
- {
- {0.2 , 0.8 , 2.0 , 3.0 , 4.0}, //Voltage [V]
- {10.0 , 30.0 , 60.0 , 100.0 , 250.0} //velocity [km/h]
- };
- const uint8_t KEYPAD4X4_ROWS = 4; //four rows
- const uint8_t KEYPAD4X4_COLS = 4; //four columns
- char hexaKeys_Keypad4x4[KEYPAD4X4_ROWS][KEYPAD4X4_COLS] = {
- {'1','2','3','A'},
- {'4','5','6','B'},
- {'7','8','9','C'},
- {'*','0','#','D'},
- };
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Servo object to control the motor
- Servo actuator;
- // Optional keypad instance placeholders (not used in control loop but kept for compatibility)
- // The original project includes keypad libraries; instantiate only if needed.
- float velocityFromVoltageLookup(float voltage){
- static const float volts[SEGMENT_POINTS_voltage_velocity_PIN_A0] = {
- voltage_velocity_PIN_A0_lookup[0][0],
- voltage_velocity_PIN_A0_lookup[0][1],
- voltage_velocity_PIN_A0_lookup[0][2],
- voltage_velocity_PIN_A0_lookup[0][3],
- voltage_velocity_PIN_A0_lookup[0][4]
- };
- static const float vel[SEGMENT_POINTS_voltage_velocity_PIN_A0] = {
- voltage_velocity_PIN_A0_lookup[1][0],
- voltage_velocity_PIN_A0_lookup[1][1],
- voltage_velocity_PIN_A0_lookup[1][2],
- voltage_velocity_PIN_A0_lookup[1][3],
- voltage_velocity_PIN_A0_lookup[1][4]
- };
- if (voltage <= volts[0]) return vel[0];
- for (int i = 0; i < SEGMENT_POINTS_voltage_velocity_PIN_A0 - 1; i++) {
- float v0 = volts[i];
- float v1 = volts[i+1];
- if (voltage <= v1) {
- float t = (voltage - v0) / (v1 - v0);
- return vel[i] + t * (vel[i+1] - vel[i]);
- }
- }
- return vel[SEGMENT_POINTS_voltage_velocity_PIN_A0 - 1];
- }
- float readVelocityFromPotVoltage(){
- int raw = analogRead(pot_Potentiometer_Vout_PIN_A0);
- float voltage = (raw * 5.0f) / 1023.0f; // convert ADC to voltage [V]
- return velocityFromVoltageLookup(voltage);
- }
- void setup(void)
- {
- pinMode(keypad_Keypad4x4_R1_PIN_D2, INPUT_PULLUP);
- pinMode(keypad_Keypad4x4_R2_PIN_D4, INPUT_PULLUP);
- pinMode(keypad_Keypad4x4_R3_PIN_D5, INPUT_PULLUP);
- pinMode(keypad_Keypad4x4_R4_PIN_D6, INPUT_PULLUP);
- pinMode(pot_Potentiometer_Vout_PIN_A0, INPUT);
- pinMode(pot_Potentiometer_Vout_PIN_A1, INPUT);
- pinMode(keypad_Keypad4x4_C1_PIN_D7, OUTPUT);
- pinMode(keypad_Keypad4x4_C2_PIN_D8, OUTPUT);
- pinMode(keypad_Keypad4x4_C3_PIN_D9, OUTPUT);
- pinMode(actuator_Servomotor_PWMSignal_PIN_D3, OUTPUT);
- actuator.attach(actuator_Servomotor_PWMSignal_PIN_D3);
- actuator.write(90);
- }
- void loop(void)
- {
- float measuredVelocity = readVelocityFromPotVoltage();
- const float desiredVelocity = 100.0f; // km/h
- float error = desiredVelocity - measuredVelocity;
- const float Kp = 0.8f; // proportional gain
- float deltaAngle = Kp * error;
- static int servoAngle = 90;
- servoAngle = 90 + (int)deltaAngle;
- if (servoAngle < 0) servoAngle = 0;
- if (servoAngle > 180) servoAngle = 180;
- actuator.write(servoAngle);
- delay(50);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment