Hi for all.
I try to send the distance that the sensor measures via lorawan. With this code I get the distance without problem:
#define ECHOPIN GPIO0// Pin to receive echo pulse
#define TRIGPIN GPIO5// Pin to send trigger pulse
void setup(){
Serial.begin(9600);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
digitalWrite(ECHOPIN, HIGH);
}
void loop() {
digitalWrite(TRIGPIN, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(20);
digitalWrite(TRIGPIN, LOW); // Send pin low again
int distance = pulseIn(ECHOPIN, HIGH,26000); // Read in times pulse
distance= distance/58;
Serial.print(distance);
Serial.println(" cm");
delay(2000);// Wait 50mS before next ranging
}
Blockquote
but when I insert the code in the lorawan section, the result of the measurement is wrong, it only reads it if I insert it in the setup of the code for lorawan. Sorry for my English. Everything compiles fine, but it doesn’t read the value correctly inside the void of “prepareTxFrame”
static void prepareTxFrame( uint8_t port )
{
pinMode(Vext, OUTPUT);
digitalWrite(TRIGPIN, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(20);
digitalWrite(TRIGPIN, LOW); // Send pin low again
digitalWrite(Vext, LOW);
nivel = pulseIn(ECHOPIN, HIGH,26000); // Read in times pulse
nivel= nivel/58;
nivelTX=nivel;
digitalWrite(Vext, HIGH);
digitalWrite(GPIO0, LOW);
pinMode(GPIO0, ANALOG);
uint16_t voltaje = getBatteryVoltage();
unsigned char *puc;
puc = (unsigned char *)(&nivel);
appDataSize = 6;
appData[0] = puc[0];
appData[1] = puc[1];
appData[2] = puc[2];
appData[3] = puc[3];
appData[4] = (uint8_t)(voltaje>>8);
appData[5] = (uint8_t)voltaje;
Serial.print(nivel);
Serial.println(" cm medida primera");
delay(2000);// Wait 50mS before next ranging
Serial.print(nivelTX);
Serial.println(" cm segunda");
}
Can somebody help me ? other sensors work properly within that void.