I am using a potentiometer and a PWM signal to make a variable speed DC motor. As our code works now, we have full speed forward, and variable speed backwards. The variable speed is VERY slow. We only want the motor to go forward, and a range of variable speed.
Our current code is:
int pot = 0;
int relay = 3;
int motorPin = 11;
int val = 0;
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
pinMode(pot, INPUT);
pinMode(relay, OUTPUT);
pinMode(motorPin, OUTPUT);
}
int getPot() {
int v;
v = analogRead(pot);
v /= 4;
v = max(v, 90);
v = min(v, 255);
return v;
}
int motorFoward() {
analogWrite(motorPin, getPot());
delayMicroseconds(250000);
digitalWrite(motorPin, LOW);
delayMicroseconds(250000);
digitalWrite(relay, HIGH);
delayMicroseconds(250000);
}
int motorBackward() {
analogWrite(motorPin, getPot());
delayMicroseconds(250000);
digitalWrite(motorPin, LOW);
delayMicroseconds(250000);
digitalWrite(relay, HIGH);
delayMicroseconds(250000);
}
int value() {
val = analogRead(pot);
val = map(val, 90, 255, 100, 140);
analogWrite(0, val);
if (val = map(val, 90, 255, 100, 140))
{
analogWrite(motorPin, 0);
}
else
{}
}
void loop() // run over and over again
{
value();
motorFoward();
motorBackward();
}
Is there anything we are doing wrong; that can be improved?
Thanks!