#define PIN_TO_SAMPLE A0
#define LED_PIN_OVERVOLT 2
#define SAMPLE_TIME_US 50
#define NUM_OF_SAMPLES 30
#define MAX_MEAN_MV 3000
float voltage;
float mean_voltage;
float voltage_accumulator=0;
int acq_counter=0;
void introduction_prints(){
const float fcamp=(1.0/float(SAMPLE_TIME_US))*1000000;
Serial.println("Welcome to our Oscilloscope project.");
Serial.print("Sampling on pin ");
Serial.println(PIN_TO_SAMPLE);
Serial.print("Sampling frequency set @ ");
Serial.print(fcamp);
Serial.println("Hz");
}
float perform_acquisition(){
int ADRES;
float value_mv; //in mV
ADRES=analogRead(PIN_TO_SAMPLE);
value_mv=(ADRES*(5.0/1024.0))*1000.0; //ADC has n=10bit, FSR=5V
return(value_mv);
}
void setup(){
pinMode(PIN_TO_SAMPLE, INPUT);
pinMode(LED_PIN_OVERVOLT, OUTPUT);
Serial.begin(9600);
introduction_prints();
delay(4000);
}
void loop(){
voltage=perform_acquisition();
Serial.print("Measured voltage is: ");
Serial.print(voltage);
Serial.println("mV");
voltage_accumulator=voltage_accumulator+voltage;
acq_counter++;
if(acq_counter==NUM_OF_SAMPLES){
mean_voltage=voltage_accumulator/float(NUM_OF_SAMPLES);
voltage_accumulator=0;
acq_counter=0;
if(mean_voltage>MAX_MEAN_MV){
digitalWrite(LED_PIN_OVERVOLT, HIGH);
}
else{
digitalWrite(LED_PIN_OVERVOLT, LOW);
}
}
delayMicroseconds(SAMPLE_TIME_US);
}