我可以看到电位器值读取和发送和接收正确。但是,当我尝试将 analogWrite 包含到 PWM 和 led 时,它不起作用。
发射器 Arduino 的代码。 当我转动电位器时,我可以看到串行监视器中的数字正确变化。
#include <VirtualWire.h>
int potPin = A0;
int txPin = 12; // data pin for 433MHz transmitter
void setup() {
// Initialize the 433MHz transmitter
vw_set_tx_pin(txPin);
vw_setup(2000); // Bits per second
// Initialize serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Read the potentiometer value
int potValue = analogRead(potPin);
// Scale the value to a range of 0-255
int scaledValue = map(potValue, 0, 1023, 0, 255);
// Print the received value for debugging
Serial.println(scaledValue);
// Send the value wirelessly
vw_send((uint8_t *)&scaledValue, sizeof(scaledValue));
vw_wait_tx(); // Wait for the transmission to complete
delay(100); // Wait for some time before sending the next value
}
接收器Arduino的代码。 如果我删除 analogWrite 行。然后串行监视器显示正确接收的值,并随着我转动电位器与变送器 Arduino 一致而变化。然而,随着 analogWrite 线的添加,串行监视器显示锅的初始值并设置导致亮度但在我转动锅时不会改变。
如果我加载一个简单的淡入淡出示例来测试 PWM 引脚,它工作正常。
#include <VirtualWire.h>
int ledPin = 3;
int rxPin = 11; // data pin for 433MHz receiver
void setup() {
vw_setup(2000); // Bits per second
vw_set_rx_pin(rxPin);
vw_rx_start(); // Start the receiver
pinMode(ledPin, OUTPUT);
// Initialize serial communication for debugging
Serial.begin(9600);
}
void loop() {
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) {
// Convert the received data back to an integer value
int receivedValue = *((int*)buf);
// Scale the value back to the original range of 0-1023
int potValue = map(receivedValue, 0, 255, 0, 1023);
// Print the received value for debugging
Serial.println(receivedValue);
// Fade the LED based on the potentiometer value
analogWrite(ledPin, receivedValue);
}
}
如果我错过了一些简单的事情,我深表歉意。我希望我已经提供了足够的信息。任何帮助将不胜感激。提前致谢。