Hi,
I am using wireless hc12 module, for transmitting distance data from ultrasonic sensor and to receive it on the receiver side. All said and done, project works extremely well, and now problem arises if there is any power cut or power glitch in receiver side, and receiver when retrieving the power doesn't pick up signals from transmitter as it was doing just before the power cut condition. Tried a lot of way somewhat similar question was asked before went through it and nothing worked in my favour. If anyone could please help me in this regard, it would be of great help
Components used
- Arduino Uno for Rx and Tx( two arduino)
- 2 hc 12 module
- ultrasonic module hcsr 04
Here is my transmitter code
#define max_distance 200
//These are the two libraries that are needed
#include <avr/interrupt.h>
#include <avr/sleep.h>
/* Here we set up our inputs and outputs. LEDs connected to pins 10 and 13 and pushbuttons attached to 2 and 12 */ //interrupt pin is attached to digital pin 3.
#define ledPin 13// pb5
#define sleepPin 12 //pb4
#define interruptPin 11 // pb3
#define wakePin 2 // pd2
//sleepStatus is set up to keep track of the button input on pin 12.
int sleepStatus = 0;
#define trigPin 9 //pb1
#define echoPin 10//pb2
long duration;
unsigned long distance;
unsigned long length_of_interval = 1000;
unsigned long timer1;
void setup()
{
Serial.begin(9600);
//pinMode(trigPin, OUTPUT);// DDR MEANS PIN MODE, PORTB = DIGITALWRITE, PIN = DIGITAL READ
//pinMode(echoPin, INPUT);
DDRB |= B00101010;
DDRD |= B00000000;
PORTB |= B00010000;
PORTD |= B00000100;
timer1 = millis();
}
void loop()
{
timer1 = millis();
// Write a pulse to the HC-SR04 Trigger Pin
PORTB |= B00000000;//digitalWrite(trigPin, LOW);
delayMicroseconds(2);
PORTB |= B00000010;//digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
PORTB &= B11111101;//digitalWrite(trigPin, LOW);
// Measure the response from the HC-SR04 Echo Pin
duration = pulseIn(echoPin, HIGH);
distance = (duration * 0.036) / 2;
//Serial.println(password);
Serial.print(distance);
Serial.print("$");
//Serial.println();
while ( millis() - timer1 < length_of_interval )
{
cli();
sleep_enable();
sei();
sleep_cpu();
sleep_disable();
}
}
while my receiver code
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
unsigned int a, b;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
void loop() {
recvWithEndMarker();
showNewData();
//Serial.println("entered main loop");
if (a > 120)
{
Serial.println("entered");
}
else
{
Serial.println("exit");
}
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '$';
char rc;
// if (Serial.available() > 0) {
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
a = atoi(receivedChars);
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
Serial.print("checked for long, it works dude ");
Serial.println(a);
//chkfn();
newData = false;
}
}
everything is working fine, until receiver side arduino, resets or switches off,
and if that happens serial receiver doesn't pick up signals and display the numbers as it was before