A sketch needs to detect a change to a analog input on pin 5, activate power to a HC12 transmitter (digital output pin8), send a one-time transmission burst (the analog value converted to a string plus some other string data), and then inhibit any further transmissions for a time interval2=2000ms. The power to the HC12 has to remain on during a time interval1=4000ms.
If a change to the analog input occurs within interval1 then the power on time has to be extended and the count-down timer with interval1 is to be reset.
What happens is that the power-on output pin8 produces a glitch (see picture, yellow trace) of 2ms.
(The blue trace is the output signal at the receiver end)
What in this sketch causes this glitch?
// http://forum.arduino.cc/index.php?topic=499210.msg3407044#msg3407044
// http://forum.arduino.cc/index.php?topic=396450.0
// HC12 Communication between Arduinos
/*
This program serves to send one integer (or byte) only from two different HC12
transmitters at one sensor each (A and B)to one HC12 receiver at the central
controller Arduino. The receiving HC12 has to know from which transmitter A or B
the value comes.
The data are sent with start- and endmarkers
v5: two integers used, one for the sensor values (integer1FromPC & integer2FromPC) and
one char for the sensor identifier (A or B). At the transmitter the sensors A or B are
identiefied by reading two digital inputs connected to a dipswitch with
2 switches (max: 4 different sensor/transmitters).
// TRANSMITTER PART
*/
#include <SoftwareSerial.h>
const byte HC12RxdPin = 4; // Recieve Pin on HC12
const byte HC12TxdPin = 5; // Transmit Pin on HC12
SoftwareSerial HC12(HC12TxdPin, HC12RxdPin); // Create Software Serial Port
int analogValue5, val5;
int previousAnalogValue5;
int HC12power = 8;
const int interval1 = 4000; // HC12 power-on time in milliseconds
const int interval2 = 2000; // HC12 retransmission time delay
static unsigned long previousMillis1; // timer1
static unsigned long previousMillis2; // timer2
boolean HC12ResendReady = true;
void setup()
{
Serial.begin(9600); // Open serial port to computer
HC12.begin(9600); // Open serial port to HC12
pinMode(HC12power, OUTPUT);
}
void loop()
{
// read analog pin 5
analogValue5 = analogRead(5);
// remap values from the analogValue5 variable to 0 / 255
val5 = map(analogValue5, 0, 1023, 0, 255);
// detect if there is a change in value, if so: switch on HC12
if (analogValue5 > (previousAnalogValue5 + 10) || analogValue5 < (previousAnalogValue5 - 10))
{
previousAnalogValue5 = analogValue5;
if (digitalRead(HC12power) == LOW)
{
digitalWrite(HC12power, HIGH);
delay(50);
}
if (HC12ResendReady == true)
{
HC12.print('<');
HC12.print(val5);
HC12.print(',');
HC12.print(val5);
HC12.print(",B>");
HC12ResendReady = false;
}
previousMillis2 = millis();
previousMillis1 = millis(); //reset timer as long as changes are detected within interval window
}
if ((millis() - previousMillis2) >= interval2) {
previousMillis2 = millis();
HC12ResendReady = true;
}
else {
//when no more change in value for analogValue5, then start timer before switching off HC12
if ((millis() - previousMillis1) >= interval1) {
digitalWrite(HC12power, LOW);
previousMillis1 = millis();
}
}
}