Emita el módulo inalámbrico HC12 con Arduino

0

Estoy intentando enviar datos de forma inalámbrica utilizando un módulo de radio HC12 (transmisor) y recopilarlos desde otro HC12 (receptor) conectado al 2do Arduino. Luego, en el receptor, los escribo en una hoja de Excel. Actualmente, he hecho 1 segundos de actualización en la hoja de Excel. ¿Cómo acelerar este proceso por el código?

Transmisor

#include <SoftwareSerial.h>
int analogPin = 14;
int val = 0; 
SoftwareSerial HC12(0,1); //HC TX, RX

void setup() {
Serial.begin(9600);
HC12.begin(19200);
analogReadResolution(16);
delay(20);
}

void loop() {

val = analogRead(analogPin);
if(HC12.available() > 1){//Read from serial monitor and send over HC-12
String input = Serial.readString();
HC12.println(val);  delay(50);  
}
delay(20);
}

Receptor

#include <rExcel.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,11); //HC TX, RX

long          idx = 0;                // index
int           outputTiming = 100;     // packet sending timing in ms                          
important: this dermines the output timing
float         a0;                     // A0 pin reading
float         volt_3v3;               // random number


char          value[16];              // written or read value

rExcel        myExcel;                // class for Excel data exchanging
String input1;
String input2;
float         a1;                     // A0 pin reading

void setup(){

Serial.begin(115200);
mySerial.begin(19200);
delay(20);



// rx buffer clearing
myExcel.clearInput();

// if analog input pin 5 is unconnected, random analog noise will cause the     call to randomSeed() to generate different seed numbers each time the sketch     runs
randomSeed(analogRead(5));
}


void loop() {

static unsigned long  loopTime = 0;
static unsigned long  time1 = 0;
int ret;


if(Serial.available() > 0){//Read from serial monitor and send over HC-12
   input1 = Serial.readString();
  mySerial.println(input1);    
}
if(mySerial.available() > 1){//Read from HC-12 and send to serial monitor
   input2 = mySerial.readString();
Serial.println(input2);  
}



loopTime = millis();


if ((loopTime - time1) >= outputTiming) {

time1 = loopTime;
a1 =  input2.toFloat();
volt_3v3 = a1/65536*3.0;

myExcel.write("Example", "B5", a1, 2);                  // write the value from A0 pin to worksheet 'Example' cell 'B5' with two digits as decimals
myExcel.write("Example", "B6", volt_3v3, 2);                 // write a random value to worksheet 'Example' cell 'B6' with two digits as decimals
myExcel.write("Example", "B7", idx, 2);                 // write a random value to worksheet 'Example' cell 'B6' with two digits as decimals

 myExcel.writeIndexed("Example", idx+11, 1,"%date%");    // write %date% (that will be converted in current date) to worksheet 'Example' row 'idx+11' column '1' 
myExcel.writeIndexed("Example", idx+11, 2,"%time%");    // write %time% (that will be converted in current time) to worksheet 'Example' row 'idx+11' column '2' 
myExcel.writeIndexed("Example", idx+11, 3, idx);        // write idx to worksheet 'Example' row 'idx+11' column '3' 
myExcel.writeIndexed("Example", idx+11, 4, a1, 2);      // write the value from A0 pin to worksheet 'Example' row 'idx+11' column '4' with two digits as decimals
myExcel.writeIndexed("Example", idx+11, 5, volt_3v3, 2);     // write a random value to worksheet 'Example' row 'idx+11' column '5' with two digits as decimals

idx++;

if (idx > 100) {
  myExcel.clear("Example","A11:F70");                   // clear cells in A11:F70 area of 'Example' worksheet
  //myExcel.clear("Test","C5:C64");                     // clear cells in C5:C64 area of 'Test' worksheet
  //myExcel.save();                                     // save the Excel file (useful for activites over a long time)
  idx = 0;
}
}
    
pregunta oppo

0 respuestas

Lea otras preguntas en las etiquetas