Hello Code experts The code in the attachment works very well, there is only one problem, I couldn't solve it, I need your help
The code in the attachment is the code that works with ACS712 20 amps
The code problem is
for example, I set it to 3 amps, when it goes over 3 amps, it should close the relay, but it keeps on turning on and off
I am waiting for your help
Thank you
const int sensorPin = A0; // ACS712 çıkışı
const int relayPin = 2; // Röle kontrol pini
const int buttonIncreasePin = 3; // + buton pini
const int buttonDecreasePin = 4; // - buton pini
const int buttonActivatePin = 5; // Üçüncü buton pini
float referenceVoltage = 5.0; // Arduino için referans voltaj
float sensitivity = 0.100; // ACS712 için hassasiyet (20A model için)
float currentLimit = 2.0; // Başlangıç amper sınırı
const float adjustmentStep = 0.5; // Ayar adımı
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
pinMode(buttonIncreasePin, INPUT_PULLUP); // + buton için pull-up
pinMode(buttonDecreasePin, INPUT_PULLUP); // - buton için pull-up
pinMode(buttonActivatePin, INPUT_PULLUP); // Üçüncü buton için pull-up
digitalWrite(relayPin, HIGH); // Röleyi başlangıçta aç
}
void loop() {
// Butonlara basılıp basılmadığını kontrol et
if (digitalRead(buttonIncreasePin) == LOW) { // + butona basıldıysa
currentLimit += adjustmentStep; // Akım limitini artır
delay(200); // Butona basım debouncing için kısa bekleme
}
if (digitalRead(buttonDecreasePin) == LOW) { // - butona basıldıysa
currentLimit -= adjustmentStep; // Akım limitini azalt
if (currentLimit < 0) currentLimit = 0;
delay(200); // Butona basım debouncing için kısa bekleme
}
// Akımı filtreleyelim
float filteredCurrent = 0;
int sampleCount = 10; // Örnek sayısı
for (int i = 0; i < sampleCount; i++) {
int sensorValue = analogRead(sensorPin);
float voltage = sensorValue * (referenceVoltage / 1023.0);
float current = (voltage - (referenceVoltage / 2)) / sensitivity;
filteredCurrent += current;
delay(100); // Küçük gecikme
}
// Filtrelenmiş akımı ortalayalım
filteredCurrent /= sampleCount;
Serial.print("Filtered Current: ");
Serial.print(filteredCurrent);
Serial.print(" | Current Limit: ");
Serial.println(currentLimit);
// Akım sınırını kontrol et
if (filteredCurrent > currentLimit) {
digitalWrite(relayPin, LOW); // Röleyi kapat
Serial.println("Current exceeded! Relay is OFF.");
} else if (digitalRead(buttonIncreasePin) == HIGH && digitalRead(buttonDecreasePin) == HIGH) {
digitalWrite(relayPin, HIGH); // Röleyi sadece butona basılmadığında aç
}
// Röleyi üçüncü butonla yönetmek için:
if (digitalRead(buttonActivatePin) == LOW) {
digitalWrite(relayPin, HIGH); // Üçüncü butona basılınca röleyi aç
Serial.println("Relay is ON.");
while (digitalRead(buttonActivatePin) == LOW) {
delay(10); // Butona basılı kaldığında bekle
}
}
delay(1000); // 1 saniye bekle
}
kodu buraya yazın veya yapıştırın