// https://wokwi.com/projects/409338384750028801
// for https://forum.arduino.cc/t/analogic-and-digitals-pin/1302617/16
// built from:
// https://wokwi.com/projects/409325290405496833
// custom-chip-RCfilter.ino
// An implementation of an RC filter in a Wokwi custom chip
// See https://docs.wokwi.com/chips-api for API
//
// SPDX-License-Identifier: MIT
// Copyright (C) 2024 David Forrest
const int potPin = A0;
const int PwmPin = 9;
const int LCFilterPin = A2;
double f = 1.0; // signal frequency
double t = 0.0; // time variable (updated in the loop)
void setup() {
Serial.begin(115200);
pinMode(A2, INPUT);
pinMode(3,OUTPUT);
pinMode(13,OUTPUT);
Serial.print(F("Wokwi Simulated LC filter \n"
"Enter a number to change the frequency.\n"
"Adjust the R and C parameters on the R-C filter custom chip\n"
"Pin3 PWM is ~490Hz, so you need sub-200us sampling to see unaliased 10% pulse widths\n"
"See also https://wokwi.com/projects/409325290405496833"
));
}
void loop() {
if (Serial.available() > 1)
{
f = (double)Serial.parseFloat(); // to change frequency at runtime
}
t = micros() * 1E-6;
double wt = 2.0 * PI * f * t;
int y = mapd(sin(wt), -1.0, 1.0, 0, 255);
analogWrite(PwmPin, y);
digitalWrite(13,y<4); // create a trigger signal
}
/* map function working with double instead of long */
double mapd(double x, double in_min, double in_max, double out_min, double out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}