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: PWM Mapping
- - Source Code NOT compiled for: Arduino Pro Mini 5V
- - Source Code created on: 2025-12-21 16:29:28
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* make it 3 analog input generate 3 pwm output with */
- /* same code function */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);\nvoid loop(void);\n
- // Pin definitions for the analog inputs and PWM outputs
- const int analogPin1 = A0;
- const int analogPin2 = A1;
- const int analogPin3 = A2;
- const int pwmPin1 = 9; // Can use any PWM-capable pin (3, 5, 6, 9, 10, 11)
- const int pwmPin2 = 10;
- const int pwmPin3 = 11;
- // Setup function
- void setup() {
- // Initialize PWM output pins as outputs
- pinMode(pwmPin1, OUTPUT);
- pinMode(pwmPin2, OUTPUT);
- pinMode(pwmPin3, OUTPUT);
- // Initialize serial communication for debugging
- Serial.begin(9600);
- }
- // Loop function
- void loop() {
- // Read analog inputs
- int sensorValue1 = analogRead(analogPin1);
- int sensorValue2 = analogRead(analogPin2);
- int sensorValue3 = analogRead(analogPin3);
- // Calculate PWM values for each sensor
- int pwmOutput1 = calculatePWM(sensorValue1);
- int pwmOutput2 = calculatePWM(sensorValue2);
- int pwmOutput3 = calculatePWM(sensorValue3);
- // Write PWM outputs
- analogWrite(pwmPin1, pwmOutput1);
- analogWrite(pwmPin2, pwmOutput2);
- analogWrite(pwmPin3, pwmOutput3);
- // Optional: print sensor values for debugging
- Serial.print("Sensor 1: "); Serial.print(sensorValue1); Serial.print(\" | PWM: \"); Serial.println(pwmOutput1);
- Serial.print("Sensor 2: "); Serial.print(sensorValue2); Serial.print(\" | PWM: \"); Serial.println(pwmOutput2);
- Serial.print("Sensor 3: "); Serial.print(sensorValue3); Serial.print(\" | PWM: \"); Serial.println(pwmOutput3);
- delay(10); // Short delay for stability
- }
- // Function to calculate PWM duty cycle based on sensor value
- int calculatePWM(int sensorValue) {
- int pwmOutput;
- if (sensorValue < 170) {
- pwmOutput = 0; // Below minimum range - 0%
- } else if (sensorValue >= 170 && sensorValue <= 190) {
- pwmOutput = 26; // 10% duty cycle (255 * 0.1 ≈ 26)
- } else if (sensorValue > 190 && sensorValue < 335) {
- // Map from 191-334 to 26-128 (10%-50%)
- pwmOutput = map(sensorValue, 190, 335, 26, 128);
- } else if (sensorValue >= 335 && sensorValue <= 360) {
- pwmOutput = 128; // 50% duty cycle (255 * 0.5 ≈ 128)
- } else if (sensorValue > 360 && sensorValue < 490) {
- // Map from 361-489 to 128-230 (50%-90%)
- pwmOutput = map(sensorValue, 361, 490, 128, 230);
- } else { // 490 and above
- pwmOutput = 230; // 90% duty cycle (255 * 0.9 ≈ 230)
- }
- pwmOutput = constrain(pwmOutput, 0, 255); // Ensure within PWM bounds
- return pwmOutput;
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment