const int ctSensorPin = A0; // Connect the CT sensor output to digital pin 2
const int threshold = 500; // Set your current threshold value (adjust as needed)
const int ledPin = 3; // Connect an LED or use the built-in LED on pin 13
int sensorValue ;
int count;
int value ;
int avgdreading;
void setup() {
Serial.begin(9600);
pinMode(ctSensorPin, INPUT);
pinMode(ledPin, OUTPUT);
count=0;
value = 0;
avgdreading=0;
}
void loop() {
sensorValue = analogRead(ctSensorPin);
Serial.println(sensorValue);
avgValue();
// Check if the current exceeds the threshold
if (sensorValue > threshold) {
digitalWrite(ledPin, HIGH); // Turn on the LED or send a digital signal
delay(1000); // Wait for 1 second (adjust as needed)
digitalWrite(ledPin, LOW); // Turn off the LED or reset the digital signal
}
delay(1000); // Adjust the delay as needed for your application
}
void avgValue(){
value = value + analogRead(ctSensorPin);
count++;
if(count==10){
avgdreading=value/10;
Serial.print("Avgd Value=") ;
Serial.println(avgdreading) ;
//resseting data
count=-1;
avgdreading=0;
value=0;
}
}
/*void avgValue(){
int i;
int value = 0;
int numReadings = 10;
int avgdreading=0;
unsigned long previousMillis = 0;
const int INTERVAL_MILLIS = 3000;
for (i = 0; i < numReadings; i++){
// Read light sensor data.
value = value + analogRead(ctSensorPin);
// 1ms pause adds more stability between reads.
delay(1);
}
// Take an average of all the readings.
value = value / numReadings;
if( (millis() - previousMills) >= INTERVAL_MILLIS){
Serial.print("Avgd Value=") ;
Serial.println(value) ;
}
}*/