- CODE: SELECT_ALL_CODE
float Kp=0,Ki=0,Kd=0;
float error=0, P=0, I=0, D=0, PID_value=0;
float previous_error=0, previous_I=0;
int sensor[5]={0, 0, 0, 0, 0};
int initial_motor_speed=50;
void read_sensor_values(void);
void calculate_pid(void);
void motor_control(void);
void setup()
{
pinMode(11,OUTPUT); //PWM Pin 1
pinMode(10,OUTPUT); //PWM Pin 2
pinMode(13,OUTPUT); //Left Motor Pin 1
pinMode(8,OUTPUT); //Left Motor Pin 2
pinMode(12,OUTPUT); //Right Motor Pin 1
pinMode(9,OUTPUT); //Right Motor Pin 2
Serial.begin(9600) ; //Enable Serial Communications
}
void loop()
{
read_sensor_values();
calculate_pid();
motor_control();
}
void read_sensor_values()
{
sensor[0]=digitalRead(0);
sensor[1]=digitalRead(1);
sensor[2]=digitalRead(2);
sensor[3]=digitalRead(3);
sensor[4]=digitalRead(4);
sensor[5]=digitalRead(5);
sensor[6]=digitalRead(6);
sensor[7]=digitalRead(7);