#include <LiquidCrystal_I2C.h>
#include <Servo.h>
Servo ESC;
LiquidCrystal_I2C lcd(0X27, 16, 2);
#define button 8
#define yled 13
#define kled 6
unsigned long newTime;
unsigned long oldTime = 0;
unsigned long startTime;
unsigned long endTime;
unsigned long duration;
int durum = 0;
int b_durum = 0;
int potValue;
int currentValue;
int BuzzerPin = 7;
int ledState = LOW;
int esc_pwm ;
int last_pwm;
int a=10;
int b=5;
long previousMillis = 0;
long interval = 1000;
bool initial = false;
bool starting_new = false ;
void setup() {
lcd.begin(16,2);
lcd.clear();
ESC.attach(9, 900, 2000); // set the PWM range to 900-2000
pinMode(button, INPUT);
pinMode(kled, OUTPUT);
pinMode(yled, OUTPUT);
pinMode(BuzzerPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// lcdde gösterilen değerin düzgün bir şekilde gösterilmesini sağlıyor yani lcd görüntüleme için yetişmeye çalışıyor.
newTime = millis();
if (newTime - oldTime > 250) {
lcd.clear();
oldTime = newTime;
}
b_durum = digitalRead(button);
if(initial == false){
if (digitalRead(button) == LOW){ // button pressed & timer not running already
startTime = millis();
Serial.println("Bp");
Serial.println(startTime);
}
if (digitalRead(button) == HIGH){ // timer running, button released
endTime = millis();
Serial.println("Br");
Serial.println(endTime);
duration = endTime - startTime; //ne kadar süre basılı kaldı
Serial.print ("button press time in milliseconds: ");
Serial.println (duration);
if (duration >= 2000)
initial = true;
else
initial = false;
}
}
if (initial == true && b_durum == 1)
{
durum = !(durum);
tone(BuzzerPin, 1000); // 1000 Hz frekansında ses çal
while (digitalRead(button) == 1)
{
delay(1);
}
noTone(BuzzerPin);
Serial.println("DİLARA");
starting_new = true;
potValue = analogRead(A0);
currentValue = map(potValue, 0, 1023, 900, 2000);
if(starting_new == true && durum == 1){
for( esc_pwm = 900; esc_pwm<=currentValue;){
esc_pwm = esc_pwm +10;
delay(50);
Serial.print("esc_pwm: ");
Serial.print(esc_pwm);
Serial.print(" currentValue: ");
Serial.println(currentValue);
lcd.setCursor(0, 0);
lcd.print("Sistem Basladi");
lcd.setCursor(0, 1);
lcd.print("Deger: ");
lcd.print(currentValue);
ESC.writeMicroseconds(esc_pwm);
last_pwm=esc_pwm;
Serial.println("last_pwm");
Serial.println(last_pwm);
}
//if ( abs(currentValue - potValue) < 11) {
// starting_new = false;
Serial.println("ENESssss");
}
if(starting_new == true && durum == 0){
for(last_pwm=esc_pwm; a>b;){
esc_pwm = esc_pwm -10;
delay(50);
Serial.print("esc_pwm: ");
Serial.print(esc_pwm);
Serial.print(" last_pwm: ");
Serial.println(last_pwm); lcd.setCursor(0, 0);
lcd.print("Sistem Basladi");
lcd.setCursor(0, 1);
lcd.print("Deger: ");
lcd.print(currentValue);
ESC.writeMicroseconds(esc_pwm);
if(esc_pwm==900){
break;
}
}
}
}
if (initial == true && durum == 1) {
digitalWrite(13, HIGH);
digitalWrite(6, LOW);
potValue = analogRead(A0);
currentValue = map(potValue, 0, 1023, 900, 2000); // map the pot value to the PWM range
lcd.setCursor(0, 0);
lcd.print("Sistem Basladi");
lcd.setCursor(0, 1);
lcd.print("Deger: ");
lcd.print(currentValue);
ESC.writeMicroseconds(currentValue); // use writeMicroseconds to set the PWM value
}
else{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval){
previousMillis = currentMillis;
if(ledState==LOW)
ledState=HIGH;
else
ledState=LOW;
digitalWrite(kled,ledState);
}
digitalWrite(13, LOW);
potValue = analogRead(A0);
currentValue = map(potValue, 0, 1023, 900, 2000); // map the pot value to the PWM range
lcd.setCursor(0, 0);
lcd.print("Sistem Beklemede");
lcd.setCursor(0, 1);
lcd.print("Deger: ");
lcd.print(currentValue);
ESC.writeMicroseconds(900);
}
}