The Output from sensor is ok.
The code is:
#include<PID_v1.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define Vent 9
char Data;
String serialData;
String serialData_D;
double Setpoint; //požadovaná vzdálenost
double Input;
double Output; // ventilátor
int setPoint;
double Distance;
//konstanty PID
double Kp = 50;
double Ki = 100000;
double Kd = 0;
//SENZOR
#define EchoPin 12
#define TrigPin 13
unsigned long start_Time;
unsigned long int count;
unsigned long int RPM;
unsigned long strTime;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
void setup() {
// Timer1 nastavení pro 25KHz
TCCR1A = 0; // undo the configuration done by...
TCCR1B = 0; // ...the Arduino core library
TCNT1 = 0; // reset timer
TCCR1A = _BV(COM1A1) // non-inverted PWM on ch. A
| _BV(COM1B1) // same on ch; B
| _BV(WGM11); // mode 10: ph. correct PWM, TOP = ICR1
TCCR1B = _BV(WGM13) // ditto
| _BV(CS10); // prescaler = 1
ICR1 = 320; // TOP = 320
// Set the PWM pins as output.
pinMode( Vent, OUTPUT);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
Serial.begin(9600);
myPID.SetMode(AUTOMATIC);
myPID.SetOutputLimits(160, 320);
myPID.SetTunings(Kp, Ki, Kd);
myPID.SetSampleTime(1000);
Input = Distance;
///////////////LCD////////////////////////////////////////////
lcd.init(); // initialize the lcd
lcd.init();
lcd.backlight();
attachInterrupt(digitalPinToInterrupt(2), RPM_fun, RISING);
}
void loop() {
/*
///////////////////SERIOVÁ KOMUNIKACE////////////////////////////
Receive_Serial_Data();
Setpoint = serialData.toDouble();
Serial.print("setpoint= ");
Serial.println(serialData);
delay(750);
*/
///////////////////SENZOR HCSR04/////////////////////////////////////////////////
long Duration; // doba trvání zvukové vlny
double Distance; // vzdálenost
//SENZOR
// vyčistí trigger pin
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
//unsigned long Duration_prum = 0;
//unsigned long Prumer_mereni;
//// for(int i=0; i<10; i++) {
//
// Čte echo a vrací dobu odrazu zvukové vlny v micro s.
Duration = pulseIn(EchoPin, HIGH, 6000);
//delay(10);
//
// Duration_prum += Duration;
// }
//
//Prumer_mereni = Duration_prum/10;
//delay(10);
Distance = 47 - (Duration * 0.0343 / 2); // rychlost zvukové vlny /2 tam a zpět
if (Distance > 45) {
Distance = 47;
}
else if (Distance < 0) {
Distance = 0;
}
int distance = Distance;
lcd.setCursor(0, 1);
lcd.print("Distance:");
lcd.print(distance);
lcd.print("cm");
Serial.println(Distance);
/*
Serial.print("čas = ");
Serial.println( millis());
Serial.print (" Vzdálenost = ");
Serial.println( Distance);
*/
Setpoint = map(analogRead(A0), 0, 1023, 20, 40);
setPoint = Setpoint;
lcd.setCursor(0, 0);
lcd.print("Setpoint:");
lcd.print(setPoint);
//lcd.print("cm");
/*
float hod = analogRead(A0);
analogWrite25k(9,map(hod, 0, 1023, 0, 320));
hod = (hod *5 /1023);
Serial.print(" Napětí pot = ");
Serial.print(hod);
Serial.print(" V, ");
*/
Input = Distance;
myPID.Compute();
analogWrite25k(Vent, Output);
if (Distance <= 17) {
analogWrite25k(Vent, 160);
}
/*
analogWrite25k(Vent,map(analogRead(A0),0,1023,0,320));
Serial.print("A0: ");
Serial.print(analogRead(A0));
Serial.print("pwm: ");
Serial.println(map(analogRead(A0),0,1023,0,320));
*/
/*
Serial.print("Input: ");
Serial.print(Input);
Serial.print(" ");
Serial.print("Output: ");
Serial.println(Output);
Serial.print(" ");
Serial.print("Setpoint: ");
Serial.print(Setpoint);
Serial.print(" ");
*/
/*
for(int pwm=0; pwm<=320; pwm+=32) {
OCR1A = pwm;
delay(5000);
}
*/
start_Time = millis();
count = 0;
while ((millis() - start_Time) <= 1000) {
RPM = (count * 60) / 4;
/*
Serial.print(" otáčky = " );
Serial.print(RPM);
Serial.println("rpm, ");
*/
}
}
/*
void Receive_Serial_Data(){
while(Serial.available()>0){
Data = Serial.read();
serialData += Data;
if (Data == "c") {
serialData += Data;
}
}
}
*/
void analogWrite25k(int pin, int value)
{
switch (pin)
{
case 9:
OCR1A = value;
break;
case 10:
OCR1B = value;
break;
default: // no other pin will work
break;
}
}
void RPM_fun() {
count++;
}
And my setup is: