pleasedontcode

PWM Mapping rev_01

Dec 21st, 2025
33
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: PWM Mapping
  13.     - Source Code NOT compiled for: Arduino Pro Mini 5V
  14.     - Source Code created on: 2025-12-21 16:29:28
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* make it 3 analog input generate 3 pwm output with */
  21.     /* same code function */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);\nvoid loop(void);\n
  31. // Pin definitions for the analog inputs and PWM outputs
  32. const int analogPin1 = A0;
  33. const int analogPin2 = A1;
  34. const int analogPin3 = A2;
  35. const int pwmPin1 = 9;  // Can use any PWM-capable pin (3, 5, 6, 9, 10, 11)
  36. const int pwmPin2 = 10;
  37. const int pwmPin3 = 11;
  38.  
  39. // Setup function
  40. void setup() {
  41.   // Initialize PWM output pins as outputs
  42.   pinMode(pwmPin1, OUTPUT);
  43.   pinMode(pwmPin2, OUTPUT);
  44.   pinMode(pwmPin3, OUTPUT);
  45.   // Initialize serial communication for debugging
  46.   Serial.begin(9600);
  47. }
  48.  
  49. // Loop function
  50. void loop() {
  51.   // Read analog inputs
  52.   int sensorValue1 = analogRead(analogPin1);
  53.   int sensorValue2 = analogRead(analogPin2);
  54.   int sensorValue3 = analogRead(analogPin3);
  55.  
  56.   // Calculate PWM values for each sensor
  57.   int pwmOutput1 = calculatePWM(sensorValue1);
  58.   int pwmOutput2 = calculatePWM(sensorValue2);
  59.   int pwmOutput3 = calculatePWM(sensorValue3);
  60.  
  61.   // Write PWM outputs
  62.   analogWrite(pwmPin1, pwmOutput1);
  63.   analogWrite(pwmPin2, pwmOutput2);
  64.   analogWrite(pwmPin3, pwmOutput3);
  65.  
  66.   // Optional: print sensor values for debugging
  67.   Serial.print("Sensor 1: "); Serial.print(sensorValue1); Serial.print(\" | PWM: \"); Serial.println(pwmOutput1);
  68.  Serial.print("Sensor 2: "); Serial.print(sensorValue2); Serial.print(\" | PWM: \"); Serial.println(pwmOutput2);
  69.  Serial.print("Sensor 3: "); Serial.print(sensorValue3); Serial.print(\" | PWM: \"); Serial.println(pwmOutput3);
  70.  
  71.  delay(10); // Short delay for stability
  72. }
  73.  
  74. // Function to calculate PWM duty cycle based on sensor value
  75. int calculatePWM(int sensorValue) {
  76.  int pwmOutput;
  77.  
  78.  if (sensorValue < 170) {
  79.    pwmOutput = 0; // Below minimum range - 0%
  80.  } else if (sensorValue >= 170 && sensorValue <= 190) {
  81.    pwmOutput = 26; // 10% duty cycle (255 * 0.1 ≈ 26)
  82.  } else if (sensorValue > 190 && sensorValue < 335) {
  83.    // Map from 191-334 to 26-128 (10%-50%)
  84.    pwmOutput = map(sensorValue, 190, 335, 26, 128);
  85.  } else if (sensorValue >= 335 && sensorValue <= 360) {
  86.    pwmOutput = 128; // 50% duty cycle (255 * 0.5 ≈ 128)
  87.  } else if (sensorValue > 360 && sensorValue < 490) {
  88.    // Map from 361-489 to 128-230 (50%-90%)
  89.    pwmOutput = map(sensorValue, 361, 490, 128, 230);
  90.  } else { // 490 and above
  91.    pwmOutput = 230; // 90% duty cycle (255 * 0.9 ≈ 230)
  92.  }
  93.  pwmOutput = constrain(pwmOutput, 0, 255); // Ensure within PWM bounds
  94.  return pwmOutput;
  95. }
  96.  
  97. /* END CODE */
  98.  
Advertisement
Add Comment
Please, Sign In to add comment