jokester4u
Junior Member level 2
Im using atmega ardunio board and my topic is speed control motor i would like to know how to
how to implement PID in this code ?
- - - Updated - - -
I know this code is done with just with one gain integral please if someone can add the other two gains
how to implement PID in this code ?
Code:
#include <LiquidCrystal.h>
//#include <SoftPWM.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 22);
#define set digitalRead(10)
#define mov digitalRead(9)
#define inc digitalRead(8)
#define dec digitalRead(7)
#define ent digitalRead(6)
const int highestPin = 13;
#define pluse1 14
volatile int state = LOW;
unsigned char a[5],i,val,count,sec1,temp1,temp0;
unsigned int set_speed,output_speed, interr,sec2,usec_time,Frequency,pwmm;
void setup()
{
lcd.begin(16, 2);
lcd.print("DC MOTOR CONTRwe");
pinMode(10, INPUT);
pinMode(9, INPUT);
pinMode(8, INPUT);
pinMode(7, INPUT);
pinMode(6, INPUT);
pinMode(14, OUTPUT);
pinMode(highestPin, OUTPUT);
// SoftPWMBegin();
// SoftPWMSet(13, 0);
// SoftPWMSetFadeTime(13, 1000, 1000);
Frequency=50;
TCCR1A = 0;
TCCR1B = 0;
TCNT1H=0xFF;
TCNT1L= 0xEA;
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
interrupts(); // enable all interrupts
pinMode(2, INPUT); // Pin 3 is input to which a switch is connected = INT1
//conversion();
delay(500);
lcd.setCursor(0, 0);
lcd.print("ENTER THE SPEED ");
attachInterrupt(0, blink1, RISING);
keypad_fun();
// analogWrite(highestPin, set_speed/10);
lcd.clear();
}
void loop()
{
// analogWrite(highestPin, 200);
//lcd.setCursor(0, 1);
//lcd.print(millis()/1000);
if(inc==LOW)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ENTER THE SPEED ");
while(inc==LOW);
keypad_fun();
}
lcd.setCursor(0, 0);
lcd.print("INPUT SPD:");
decimal4(11,0, set_speed);
lcd.setCursor(0, 1);
lcd.print("OUTPUT SPD:");
// decimal4(11,1,interr);
//analogWrite(highestPin, 250);
// SoftPWMSet(13, 150);
// analogWrite(highestPin, 100);
if(sec1>1)
{
decimal4(11,1,output_speed);
output_speed=interr*5;
interr=0;
if(output_speed<set_speed){pwmm++;}
if(output_speed>set_speed){pwmm--;}
if(output_speed==set_speed){pwmm=pwmm;}
analogWrite(highestPin, pwmm);
sec1=0;
//conversion();
//Setpoint(output_speed,set_speed);
}
}
void blink1()
{
interr++;
}
void keypad_fun()
{
while(ent==HIGH)
{
if(inc==LOW)
{
while(inc==LOW);
val++;
lcd.setCursor(0+i,1);
lcd.write(val+0x30);
a[i]=val;
if(val>9){val=0;}
}
if(dec==LOW)
{
while(dec==LOW);
val--;
a[i]=val;
lcd.setCursor(0+i,1);
lcd.write(val+0x30);
if(val<=0){val=9;}
}
if(mov==LOW)
{
while(mov==LOW);
i++;
lcd.setCursor(0+i, 1);
if(i>3){i=0;}
}
}
if(ent==LOW)
{
set_speed=(a[0]*1000)+(a[1]*100)+(a[2]*10)+(a[3]*1);
pwmm=set_speed/10;
lcd.setCursor(0, 1);
lcd.print(" ");
}
}
void decimal4(unsigned int col,unsigned int row,unsigned int value)
{
lcd.setCursor(col+0, row);
lcd.print((value%10000)/1000);
lcd.setCursor(col+1, row);
lcd.print((value%1000)/100);
lcd.setCursor(col+2, row);
lcd.print((value%100)/10);
lcd.setCursor(col+3, row);
lcd.print((value%10)/1);
}
ISR(TIMER1_OVF_vect) // interrupt service routine
{
TCNT1H=0xFF;
TCNT1L= 0xEA;
count++;
usec_time++;
if(usec_time>620)sec1++,usec_time=0;
}
- - - Updated - - -
I know this code is done with just with one gain integral please if someone can add the other two gains