Hi
I was looking for code to drive wiper motor with a potentiometer attached to it in such a manner that pot would give position feedback to board where it stands
My search end to this sweet code written by AS motion Lab
byte motor_p1 = 4;
byte motor_p2 = 5;
byte pwmPin = 6;
int currentAngle;
int requiredAngle;
int errorAmount;
int remappedErrorAmount;
byte acceptableError = 4; //for smoothing
void setup()
{
Serial.begin(9600);
pinMode(motor_p1, OUTPUT);
pinMode(motor_p2, OUTPUT);
pinMode(pwmPin, OUTPUT);
}
void loop()
{
readAndConditionAngle();
readAndConditionRequiredAngle();
if (currentAngle<requiredAngle){
digitalWrite(motor_p1, HIGH);
digitalWrite(motor_p2, LOW);
} if (currentAngle>requiredAngle){
digitalWrite(motor_p1, LOW);
digitalWrite(motor_p2, HIGH);
}
calculateErrorAmount(currentAngle,requiredAngle);
analogWrite(pwmPin,remappedErrorAmount);
Serial.println(currentAngle);
}
void calculateErrorAmount(int currentAngle,int requiredAngle){
errorAmount=abs(currentAngle-requiredAngle);
remappedErrorAmount=map(errorAmount, 0, 1000, 0, 255);
if (remappedErrorAmount<acceptableError){
remappedErrorAmount=0;
}
}
void readAndConditionAngle() {
currentAngle = analogRead(0);
if (currentAngle<200){
currentAngle=200;
} if (currentAngle>800) {
currentAngle=800;
}
}
void readAndConditionRequiredAngle() {
requiredAngle = analogRead(1);
if (requiredAngle<200){
requiredAngle=200;
} if (requiredAngle>800) {
requiredAngle=800;
}
}
I found a small error and corrected minor was no PWM to make motor move.
remappedErrorAmount=map(errorAmount, 0, 1000, 0, 255);
with
remappedErrorAmount=map(errorAmount, 0, 1000, 240, 255);
as there was no PWM to make motor move.
Everything is good in this code except that now I want to make some modifications and instead input by potentiometer (requiredAngle) I want to give input by temperature.
I intend to install wiper motor over a valve which would turn valve on / off or proportionate by temperature. If temperature is high it would open valve to full and like wise.
I used to be good programmer a year ago but haven't touch my board and forgot most of it.
Would appreciate if someone could guide me how to modify code to perform such action
Temperature sensor would be DS18S20
Regards
Salman