Hi guys I have a question regarding the serial input.
I have an analog input of an Hall sensor. And now I want to specify the sampling frequency and the timespan of the recording.
I managed to get the right outputs when I implemented one of the variables (time, frequency) already in the code.
But I want to be able to change the frequency and timespan via serial comands.
I though it would work if I had just two seperate commands with ("Serial.parseFloat()"). But it didnt gave me any outputs.
[code]
double volt; //output variable which is current voltage
int sensor; //variable to hold voltage sensor value
double x = (5.0/1023.0); ////converting sensor value to voltage
double freq = 0; //chosen frequency for data acquisition
double interval; //interval between two samples in micros
int rectime = 0; //time span for recordings
uint32_t starttime = 0;
uint32_t endtime;
uint32_t lastMicros = 0;
void setup()
{
pinMode(A0,INPUT);//sensor connected to analog 0
pinMode(13,OUTPUT);
digitalWrite(13, HIGH); //Convert digitaloutput pin for using it as power supply
analogReference(DEFAULT);
Serial.begin(230400);
}
void loop()
{
inputFreq(); //first input read for frequency
inputTime(); //second input read for Timespan
recdata();
}
void inputFreq()
{
if (Serial.available() > 0)
{
//reads value for sampling frequency
freq = Serial.parseFloat();
interval = (1/freq) * 1000000;
}
}
void inputTime()
{
if (Serial.available() > 0 && freq > 0)
{
//reads value for timespan
rectime = Serial.parseFloat();
}
}
void recdata()
{
if (freq > 0 && rectime > 0)
{
starttime = millis();
endtime = starttime;
while( endtime-starttime < rectime)
{
if (micros() - lastMicros > interval)
{
lastMicros = micros();
sensor = analogRead(A0); //analog reading sensor values
volt = sensor * x;
Serial.println(volt, 3); //print voltage with 3 significant figures
}
endtime = millis();
}
}
}
[/code]
Also I would be happy over suggestions for my code. I am a bit new to programming.
Cheers