Hi There. I am new at this and looking for help to write code for the following. I have played around trying to write my own code using examples from the digital pot product info but struggling.
Some more background
I purchased a speed Pedal for a kids go cart that we are building that outputs a variable voltage and also purchased a motor speed controller that uses a potentiometer to control the speed. I am using a Arduino nano to convert the voltage input from this speed pedal to a variable resistance 100k digital pot to interface into the motor speed controller to replace the normal 100k pot.
Requirement
Write Arduino code for Arduino Nano to use a X9C104S 100K Digital Potentiometer Module to convert a "Foot Pedal Speed Controller" that outputs a variable voltage of 0.82 Volts to 2.52 Volts input into the Arduino. As the input voltage changes from 0.82V to 2.52V then proportionally change the X9C104S 100K Digital Potentiometer's variable resistance from 100k ohms to 20k ohms. 0.8 Volts is the slowest speed and 2.52 Volts is the Maximum Speed. Include the X9C10X library Write input voltage to serial monitor. Include a "govern speed" button. If "govern speed" button is on then limit the X9C104S 100K Digital Potentiometer's variable resistance 100k ohm to 60k ohm where 100k is the slowest and 60k ohm is the fastest. If "govern speed" button is off then change the X9C104S 100K Digital Potentiometer's variable resistance range of 100k ohm to 20k ohm where 100k ohm is the slowest and 20k ohm is the fastest. Include a "govern speed" LED. If "govern speed" button is on then turn on "govern speed" LED. If "govern speed" button is off then turn off "govern speed" LED.
Here’s some sample code, if anyone can improve on it to get it to work.
#include <X9C10X.h>
#define CS_PIN 10
#define INC_PIN 9
#define UD_PIN 8
#define PEDAL_PIN A0
#define GOVERN_SPEED_BUTTON_PIN 2
#define GOVERN_SPEED_LED_PIN 3
X9C10X digiPot(CS_PIN, UD_PIN, INC_PIN); // Initialize the digital potentiometer
bool isSpeedGoverned = false;
int mapResistance(float voltage) {
if (voltage < 0.8) {
voltage = 0.8; // Ensure the minimum voltage is 0.8V
} else if (voltage > 2.52) {
voltage = 2.52; // Ensure the maximum voltage is 2.52V
}
if (isSpeedGoverned) {
return 60000 + (voltage - 0.8) * 40000 / 1.72; // Maps the 0.82V-2.52V input range to 60k-20k ohm resistance range
} else {
return 100000 - (voltage - 0.8) * 80000 / 1.72; // Maps the 0.82V-2.52V input range to 100k-20k ohm resistance range
}
}
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(GOVERN_SPEED_BUTTON_PIN, INPUT_PULLUP);
pinMode(GOVERN_SPEED_LED_PIN, OUTPUT);
digitalWrite(GOVERN_SPEED_LED_PIN, LOW);
}
void loop() {
float voltage = analogRead(PEDAL_PIN) * (5.0 / 1023.0);
int resistanceSetting = mapResistance(voltage);
if (digitalRead(GOVERN_SPEED_BUTTON_PIN) == LOW) {
isSpeedGoverned = true;
digitalWrite(GOVERN_SPEED_LED_PIN, HIGH);
} else {
isSpeedGoverned = false;
digitalWrite(GOVERN_SPEED_LED_PIN, LOW);
}
digiPot.setWiper(resistanceSetting); // Set the resistance on the digital potentiometer
Serial.print("Input Voltage: ");
Serial.print(voltage);
Serial.println(" V");
delay(100); // Adjust the delay time as needed
}