I’m controlling a small 12V motor (start and stop, no reverse). I remember hearing about inrush current so I wrote the code to gradually ramp up using PWM. I just don’t wanna fry anything. However doing that has made the code a lot more complicated. I think its a mess right now. Can anyone help me simplify it? Or should I simply start it at full speed?
The breaking out of the ramping up loop and the steady state loop is really awkward… here’s what i have:
char PLANT1;
const int PUMP1=3;
void setup()
{
Serial.begin(9600);
pinMode(PUMP1, OUTPUT);
}
void loop()
{
if (Serial.available() > 0) //Input
{
PLANT1 = Serial.read(); //Read state of PLANT1
if (PLANT1 == '1') //Plant 1 is thirsty
{
Serial.println("Plant 1 is Thirsty.");
for (int i=0; i<256; i++) //ramp to protect from high inrush
{
analogWrite(PUMP1, i);
delay(10);
PLANT1 = Serial.read();
if (PLANT1 == '2') //break out of ramp
{
Serial.println("Plant 1 is quenched.");
analogWrite(PUMP1, 0);
break;
}
if (i == 256 && PLANT1 == '1') //hold at steady state
{
i = 255;
}
}
else if (PLANT1 == '2') //check for quench
{
Serial.println("Plant 1 is quenched");
analogWrite(PUMP1, 0);
delay(10);
}
delay(1000);
}
}
Thanks!