Hello !
I am trying to control a DC motor by PWM.
My motor have attached a brake to simulate a mechanical load.
So,
- I set the constant speed by PWM ( for example 1200 rpm);
- i pull the brake (softly) and the motor will slow-down it's speed (for example 800 rpm).
My controller needs to increase the PWM lenght so that even with the load (the brake) the motor should run at 1200 rpm.
Below is my code :
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 13, 6);
byte trigger;
unsigned long t0, t1;
float rpm;
int potPin = 0;
int transistorPin = 9;
int potValue = 0;
void setup()
{
pinMode(transistorPin, OUTPUT);
lcd.begin(20, 4);
attachInterrupt(0, rpm_fun, RISING); // senzorul HALL pe pin 2
t0 = 0;
t1 = 0;
trigger = 0;
lcd.setCursor(9,0);
lcd.print("----------");
lcd.setCursor(0, 3);
lcd.print("---------------- RPM");
lcd.setCursor(0, 1);
lcd.print("Turatie impusa=");
lcd.setCursor(0,2);
lcd.print("RPM acum=");
}
void loop()
{
potValue = analogRead(potPin) / 4;
analogWrite(transistorPin, potValue);
// incercare
//if ( potValue <= 150 && rpm < 2500 )potValue=254;
if (potValue >= 100&& rpm < 2200)
{
analogWrite(transistorPin,255 );
delay(2000);
}
// incercare
float g = 0;
if (trigger != 0) {
g = t1 - t0;
rpm = (1/g) * 60000000;
trigger = 0;
}
//-------- AFISARE PE LCD --------------------
delay(500);
int RPM=rpm;
lcd.setCursor(10,2);
lcd.print(RPM);
if ( RPM < 1000) lcd.print(' ');
lcd.setCursor(0,0);
lcd.print(potValue);
if ( potValue < 100 ) lcd.print(' ');
//-------- AFISARE PE LCD --------------------
}
void rpm_fun()
{
if (t0 == 0)
t0 = micros();
else {
t0 = t1;
t1 = micros();
trigger = 1;
}
}
I tried to do this :
if (potValue >= 100&& rpm < 2200)
{
analogWrite(transistorPin,255 );
delay(2000);
}
event without delay.
It work, but just for almoast a second, until it reached the deasired speed, and then the speed will slow again.
What should i do ?