First let me just say I am a complete noob here and this is my first project.
I am trying to build a simple project that utilizes a piezo transducer to sense vibration and send a signal (RF 433mhz) to a second unit to flash an LED. I am using 5v Pro Micros as my processors, a disk type piezo transducer, LED for visual indicator, and 433mhx tx and rx units. The expected results are that when the piezo senses vibration the Tx unit will turn on/flash the LED and transmit a signal to the Rx unit which will turn on/flash its LED. The actual results are the tx/sensing unit operate as planned, sending the signal to the Rx unit which indeed flashes its LED, the problem is that the LED on the RX unit flashes constantly, (rapid strobe like), in the absence of a signal. I have swapped out Pro Micros and Rx units on the receive side with no change in the problem, I have also removed the RF unit from that setup with the same rapid flash so I don’t think I am getting RF noise. Perhaps I have made a mistake in my physical set up or in the code. I am open to any suggestions or code rewrites.
The sensing/Tx unit is configured as follows Pro Micro powered by 9vdc batt to gnd and RAW, Piezo connected to gnd and pin A0 with a 1 Mohm resistor, There is an LED connected to gnd and pin 9 (led has resistor on the + lead), 433 mhz Tx unit is connected VCC to VCC, DATA to pin 4, GND to GND.
The Rx/Notification unit is configured as follows Pro Micro powered by 9vdc batt to gnd and RAW, LED connected to gnd and pin 9 (led has resistor on the + lead), 433 Mhz Rx unit connected DATA to Pin A0, GND to GND, and VCC to VCC.
Sensing/Tx unit code
/*
Target Sensor Transmit module.
------------------------------------------------------------- */
const int ledPin = 9; // led connected to digital pin 9
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int rfTransmitPin = 4; // rf transmitter connected to pin 4
const int threshold = 400; // threshold value to decide when the detected sound is a knock or not
int sensorReading = 0; // variable to store the value read from the sensor pin
int ledState = LOW; // variable used to store the last LED status, to toggle the light
unsigned long lastPeakTime = 0; //used for calculating peak
int currentPeak = 0;
int blinkCounter = 0;
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
pinMode(rfTransmitPin, OUTPUT);
pinMode(knockSensor, INPUT);
Serial.begin(9600); // use the serial port for debugging
}
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);
if (sensorReading > currentPeak) {
currentPeak = sensorReading;
}
// if the sensor reading is greater than the threshold:
if (sensorReading >= threshold) {
Serial.print(sensorReading);
Serial.println(" Knock! ");
while (blinkCounter <= 3)
{
Serial.println(blinkCounter);
digitalWrite(rfTransmitPin, HIGH); //Transmit a HIGH signal
digitalWrite(ledPin, HIGH); //Turn the LED on
delay(500); //Wait
digitalWrite(rfTransmitPin, LOW); //Transmit a LOW signal
digitalWrite(ledPin, LOW); //Turn the LED off
delay(50);
digitalWrite(ledPin, LOW);
delay(50);
blinkCounter++;
}
blinkCounter = 0;
delay(50);
}
//DEBUG CODE THAT GETS WRITTEN TO SERIAL, NOT NECESSARY TO LEAVE IN
if (millis() - lastPeakTime > 2000) {
Serial.print("Peak value: ");
Serial.println(currentPeak);
lastPeakTime = millis();
currentPeak = 0;
}
}
Rx/Signaling Unit Code
/*
Target Sensor recieve module.
------------------------------------------------------------- */
#define rfReceivePin A0 //RF Receiver pin = Analog pin 0
#define ledPin 9 //Onboard LED = digital pin 9
unsigned int data = 0; // variable used to store received data
const unsigned int upperThreshold = 70; //upper threshold value
const unsigned int lowerThreshold = 50; //lower threshold value
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
data = analogRead(rfReceivePin); //listen for data on Analog pin 0
if (data > upperThreshold) {
digitalWrite(ledPin, LOW); //If a LOW signal is received, turn LED OFF
Serial.println(data);
}
if (data < lowerThreshold) {
digitalWrite(ledPin, HIGH); //If a HIGH signal is received, turn LED ON
Serial.println(data);
}
}