This project uses the new ARDUINO UNO R4.
In this case, we send a signal to an ultrasonic transmitter and analyze the amplitude of the received signal. (Similar things can be done with many other systems.) The Serial Plotter of the "old" IDE 1.8.19 not only shows this amplitude, it also displays the actual frequency in the legend in the top of the plotter window, it also reports all results sent to it. (The numbers shown at the x-axis do not show anything related to time, they only count the numbers of samples sent to the plotter since the plotter window was activated.) Unfortunately, nothing of this can be done with the plotter of the "new" IDE. You can still use IDE 2.x.x to edit, compile and upload your sketch. (In case you are using IDE 1.8.19 and IDE 2.x.x. at the same time make sure that not both of them are connected with the COM-Port of your board.) The reason why this is possible with the old plotter is that it checks its input lines for pure numerical data. If they contain something like "A123" or "456B", these are not treated as numbers but as legend descriptions, and these lines can be sent at any time. If you are not happy with the plotters autoscaling, just define a maximum value and include it to the set of values sent to the plotter each time.
Please use the translator of your choice to read the comments included in the sketch.
/*
Sweep
Automatische Suche nach der Frequenz, bei
das Signal am Empfaenger am groessten ist.
Programm geht NUR mit dem Plotter von IDE 1.8.19!
Der Plotter kann benutzt werden, um in
der Legende im Kopf des Plotter-Fensters
wahlfreie Informationen anzuzeigen.
Die Legende kann jederzeit aktualisiert werden.
Bedingung: diese duerfen nichts enthalten,
was als gueltige Zahl identifizierbar ist,
und es ein Leerzeichen weniger enthalten
sein als Funktionen dargestellt werden.
*/
// output:
#include "pwm.h"
int pwmPin = 10;
PwmOut pwm(pwmPin);
const int f1 = 37500;
const int f2 = 42500;
const int divisor1 = F_CPU / f1;
const int divisor2 = F_CPU / f2;
// input:
#include <OPAMP.h>
const int reso = 14;
const int MAX_ADC = (1 << reso) - 1;
void setup() {
delay(2000);
long t1 = millis() + 2000;
Serial.begin(9600);
while (!Serial & millis() < t1)
;
Serial.println(__FILE__);
#ifndef ARDUINO_ARCH_RENESAS
Serial.println("nur fuer ARDUINO MINIMA oder WiFi");
while (true);
#endif
// activate OPAMP, default channel 0
// Plus: Analog A1
// Minus: Analog A2
// Output: Analog A3
if (!OPAMP.begin(OPAMP_SPEED_HIGHSPEED))
Serial.println("Failed to start OPAMP!");
bool const isRunning = OPAMP.isRunning(0);
if (isRunning) Serial.println("OPAMP_running_on_channel_0!");
else Serial.println("OPAMP channel 0 is not running!");
analogReadResolution(reso);
// erzeuge y-Achse:
printValues(0, 0, MAX_ADC);
}
void loop() {
printLegend("suche_Resonanz_von " + String(f1), "_bis_", String(f2) + " Hz");
delay(5000); // Zeit zum Lesen
int nextTick = f1;
int maxValue = 0;
int maxFreq;
boolean state;
// --- Mess-Schleife --- :
for (int i = divisor1; i >= divisor2; i--) {
// indicate measuring by flashing LED:
state = i & 2;
pinMode(LED_BUILTIN, state); // you need this
digitalWrite(LED_BUILTIN, state);
int freq = F_CPU / i;
printLegend("---> f=", String(freq), "_Hz <---");
int mini = 1024;
int maxi = 0;
// --- Beginn der Messungen ---
pwm.begin(freq, 50.0f);
delay(20);
const int N = 9600;
int16_t a[N];
for (int j = 0; j < N; j++)
a[j] = analogRead(A3);
pwm.end();
// --- Ende der Messungen, Auswertung --- :
for (int j = 0; j < N; j++) {
mini = min(mini, a[j]);
maxi = max(maxi, a[j]);
}
int diff = maxi - mini;
// bestimme aktuelles Maximum
// und zugehoerige Frequenz:
if (diff > maxValue) {
maxValue = diff;
maxFreq = freq;
}
// --- Darstellung --- :
int tick = 0;
if (freq > nextTick) {
nextTick = nextTick + 500;
tick = diff;
}
Serial.println();
printValues(diff, tick, MAX_ADC);
}
// Mess-Schleife beendet
digitalWrite(LED_BUILTIN, LOW);
// schiebe alles nach links:
for (int i = 0; i < 25; i++)
printValues(0, 0, 0);
if (maxValue >= MAX_ADC - 1)
printLegend("Signal uebersteuert", " ", "Abstand_vergroessern!");
else printLegend("maxFreq =_", String(maxFreq), " Hz");
delay(5000); // Zeit zum Lesen
}
void printLegend(String pre, String mid, String post) {
// Alle Strings zusammen muessen GENAU ZWEI Leerzeichen enthalten!
Serial.print(pre);
Serial.print(mid);
Serial.println(post);
}
void printValues(int v1, int v2, int v3) {
Serial.println();
Serial.print(v1);
Serial.print(",");
Serial.print(v2);
Serial.print(",");
Serial.println(v3);
}The end.


Comments