#define OUTPUT_PIN_0 D2 // Bit 0 de salida digital
#define OUTPUT_PIN_1 D3 // Bit 1 de salida digital
#define OUTPUT_PIN_2 D4 // Bit 2 de salida digital
#define OUTPUT_PIN_3 D5 // Bit 3 de salida digital
#define OUTPUT_PIN_4 D5 // Bit 4 de salida digital
#define OUTPUT_PIN_5 D7 // Bit 5 de salida digital
#define OUTPUT_PIN_6 D8 // Bit 6 de salida digital
#define OUTPUT_PIN_7 D9 // Bit 7 de salida digital
#define OUTPUT_PIN_8 D10 // Bit 7 de salida digital
float filteredValue = 0;
const int squareWaveHigh = 1023;
const int squareWaveLow = 0;
const int period = 500;
unsigned long previousMillis = 0;
bool squareWaveState = false;
const float alpha = 0.1;
void setup() {
pinMode(OUTPUT_PIN_0, OUTPUT);
pinMode(OUTPUT_PIN_1, OUTPUT);
pinMode(OUTPUT_PIN_2, OUTPUT);
pinMode(OUTPUT_PIN_3, OUTPUT);
pinMode(OUTPUT_PIN_4, OUTPUT);
pinMode(OUTPUT_PIN_5, OUTPUT);
pinMode(OUTPUT_PIN_6, OUTPUT);
pinMode(OUTPUT_PIN_7, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Generar la señal cuadrada con un periodo
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= period / 2) { // Alternar cada medio periodo
previousMillis = currentMillis;
squareWaveState = !squareWaveState;
}
int squareWaveValue = squareWaveState ? squareWaveHigh : squareWaveLow;
filteredValue = (alpha * squareWaveValue) + ((1 - alpha) * filteredValue);
int mappedValue;
mappedValue = map(filteredValue, 0, 1023, 0, 255); // Mapear de 10 bits a 8 bits
int squareWaveValueMap;
squareWaveValueMap = map(squareWaveValue, 0, 1023, 0, 255);
Serial.print(mappedValue); //mostrar la senal filtrada
Serial.print(", ");
Serial.println(squareWaveValueMap); // mostrar senal sin filtrar
digitalWrite(OUTPUT_PIN_0, (mappedValue & 0x01) ? HIGH : LOW); // Bit 0
digitalWrite(OUTPUT_PIN_1, (mappedValue & 0x02) ? HIGH : LOW); // Bit 1
digitalWrite(OUTPUT_PIN_2, (mappedValue & 0x04) ? HIGH : LOW); // Bit 2
digitalWrite(OUTPUT_PIN_3, (mappedValue & 0x08) ? HIGH : LOW); // Bit 3
digitalWrite(OUTPUT_PIN_4, (mappedValue & 0x10) ? HIGH : LOW); // Bit 4
digitalWrite(OUTPUT_PIN_5, (mappedValue & 0x20) ? HIGH : LOW); // Bit 5
digitalWrite(OUTPUT_PIN_6, (mappedValue & 0x40) ? HIGH : LOW); // Bit 6
digitalWrite(OUTPUT_PIN_7, (mappedValue & 0x80) ? HIGH : LOW); // Bit 7
}
Loading
st-nucleo-l031k6
st-nucleo-l031k6