Go Down

Topic: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER (Read 6722 times) previous topic - next topic

PaulS

Code: [Select]
// SPECIAL THANKS TO GEO BRUCE ON INSTRUCTABLES.COM

#include <SoftwareSerial.h>
SoftwareSerial HC12(0,1); //RX,TX

You can NOT do software serial on the hardware serial pins.
The art of getting good answers lies in asking good questions.

knightridar

Hi, I know but it's the only way I've been able to get values on my serial monitor on the pc.
I tried changing the pins and all I get is a blank screen on both the transmitter and receiver serial monitors. I can't figure out what's wrong.
Useful CAD/ 3D model files for
Arduino, hobbyist, and engineering projects

https://grabcad.com/has-2

PaulS

Quote
I can't figure out what's wrong.
Probably your wiring.
Definitely your code.
The art of getting good answers lies in asking good questions.

knightridar

The wiring is right for sure.


The code:
I am guessing it's the serial print line series of code
at the end of the code.

While hc-12.available ??? ...
Useful CAD/ 3D model files for
Arduino, hobbyist, and engineering projects

https://grabcad.com/has-2

knightridar

@Paul S...

I apologize, the wiring was wrong.
The wires were numbered backwards such that tx was going to tx instead of rx.

The (transmitter) arduino pro mini serial monitor is showing up but the values remain static.
I hooked the hc-12 module up with a separate power source.

The (receiver) arduino uno values all remain at 0.


transmit code:

Code: [Select]

// SPECIAL THANKS TO GEO BRUCE ON INSTRUCTABLES.COM

#include <SoftwareSerial.h>
SoftwareSerial HC12(13,12); //RX,TX   

void setup()
{
  Serial.begin(9600); //SERIAL PORT TO PC
  HC12.begin(9600); //SERIAL PORT TO HC12
}
void loop()
{
  String sendTR; //STRING TO STORE ANALOG VALUE
  String sendBR;
  String sendTL;
  String sendBL;
  String sendDT;
  String sendTE;
  String ss;
 
  char buff[6]; //BUFFER FOR 6 CHARACTERS

  //LIGHT SENSORS CONNNECTED TO ANALOG PIN ON ARDUINO BOARD
  int TR = analogRead(A1); //TOP RIGHT SENSOR
  int BR = analogRead(A0); //BOTTOM RIGHT SENSOR
  int TL = analogRead(A2); //TOP LEFT SENSOR
  int BL = analogRead(A3); //BOTTOM LEFT SENSOR
  int DT = analogRead(A6) * 4; //CONTROL DELAY TIME BETWEEN READINGS OF SENSORS
  int TE = analogRead(A7) / 4; //SET TOLERANCE VALUE FOR DIFFERENCE BETWEEN SENSORS
 
  sendTR+=itoa(TR,buff,10); //TAKES AN INTEGER AND CONVERTS IT INTO AN ASCII/NUMBER STRING (CONVERT INTEGER TO STRING (NON-STANDARD FUNCTION))
  sendBR+=itoa(BR,buff,10); //char *  itoa ( int value, char * str, int base );
  sendTL+=itoa(TL,buff,10);
  sendBL+=itoa(BL,buff,10);
  sendDT+=itoa(DT,buff,10);
  sendTE+=itoa(TE,buff,10);
  ss+=sendTR; //ss = ss + sentTR
  ss+="&"; //ss = ss + &
  ss+=sendBR;
  ss+="&";
  ss+=sendTL;
  ss+="&";
  ss+=sendBL;
  ss+="&";
  ss+=sendDT;
  ss+="&";
  ss+=sendTE;
  ss+="&";
 
// ==== Storing the incoming data into a String variable
while (HC12.available())  // If HC-12 has data
  {           
    Serial.println(ss.c_str());
    Serial.println();
    delay(DT);
    delay(2000);
   }
}



receive code:


Code: [Select]

// SPECIAL THANKS TO GEO BRUCE ON INSTRUCTABLES.COM

#include <SoftwareSerial.h>
SoftwareSerial HC12(13,12); //RX, TX

byte incomingByte;
String readBuffer = "";

// AZIMUTH AND ELEVATION PWM PINS ON EACH H-BRIDGE MUST BE CONNECTED TO FOUR PWM (PULSE WIDTH MODULATION) PINS ON ARDUINO.
// FOR UNO/MICRO/PRO MINI THEY ARE: 3,5,6,9,10,11.

int AREN = 2; int ARPWM = 3; int ALEN = 4; int ALPWM = 5; // MOTOR AZIMUTH ADJUSTMENT
int EREN = 7; int ERPWM = 6; int ELEN = 8; int ELPWM = 9; // MOTOR ELEVATION ADJUSTMENT

void setup()
{
  Serial.begin(9600); //SERIAL PORT TO PC
  HC12.begin(9600); //SERIAL PORT TO HC12
  pinMode(AREN, OUTPUT); pinMode(ARPWM, OUTPUT); pinMode(ALEN, OUTPUT); pinMode(ALPWM, OUTPUT);  //SET ALL MOTOR CONTROL PINS TO OUTPUTS
  pinMode(EREN, OUTPUT); pinMode(ERPWM, OUTPUT); pinMode(ELEN, OUTPUT); pinMode(ELPWM, OUTPUT);
}
void loop()
{
// ==== STORING THE INCOMING DATA INTO A STRING VARIABLE
readBuffer="";
while (HC12.available()) //IF HC-12 HAS DATA
  {         
    incomingByte = HC12.read();          //STORE EACH INCOMING BYTE FROM HC-12
    readBuffer += char(incomingByte);    //ADD EACH BYTE TO ReadBuffer STRING VARIABLE
  }

//SYNTAX: string.indexOf(val) - LOCATES A CHARACTER OR STRING WITHIN ANOTHER STRING. BY DEFAULT, SEARCHES FROM THE BEGINNING OF THE STRING, BUT CAN ALSO START FROM A GIVEN INDEX, ALLOWING FOR THE LOCATING OF ALL INSTANCES OF THE CHARACTER OR STRING.

int firstAnd= readBuffer.indexOf("&");   
String firstTR = readBuffer.substring(0,firstAnd);
int TR= firstTR.toInt();

int secondAnd= readBuffer.indexOf("&", firstAnd+1);
String firstBR = readBuffer.substring(firstAnd+1,secondAnd);
int BR= firstBR.toInt();

int thirdAnd= readBuffer.indexOf("&", secondAnd+1);
String firstTL = readBuffer.substring(secondAnd+1,thirdAnd);
int TL= firstTL.toInt();
 
int fourthAnd= readBuffer.indexOf("&",thirdAnd+1);
String firstBL = readBuffer.substring(thirdAnd+1,fourthAnd);
int BL = firstBL.toInt();

int fifthAnd= readBuffer.indexOf("&",fourthAnd+1);
String firstDT = readBuffer.substring(fourthAnd+1,fifthAnd);
int DT= firstDT.toInt();

int sixthAnd= readBuffer.indexOf("&", fifthAnd+1);
String firstTE = readBuffer.substring(fifthAnd+1,sixthAnd);
int TE= firstTE.toInt();

Serial.println(TR);
Serial.println(BR);
Serial.println(TL);
Serial.println(BL);
Serial.println(DT);
Serial.println(TE);
Serial.println("========================");
delay(2000);

int count = 0; //START MILLISECOND COUNT OF LIGHT SENSOR READINGS
count++; //INCREMENTAL COUNT INCREASE, CONTINUES TO SHOW LIGHT SENSOR RESULTS

int avt = (TR + TL) / 2; // AVERAGE VALUE TOP
int avd = (BL + BR) / 2; // AVERAGE VALUE DOWN
int avl = (TL + BL) / 2; // AVERAGE VALUE LEFT
int avr = (TR + BR) / 2; // AVERAGE VALUE RIGHT

int dv = avt - avd; // AVERAGE DIFFERENCE OF TOP AND BOTTOM LIGHT SENSORS
int dh = avl - avr;// AVERAGE DIFFERENCE OF LEFT AND RIGHT LIGHT SENSORS

  if (-1 * TE > dh || dh > TE) // CHECK IF THE DIFFERENCE IN LEFT AND RIGHT LIGHT SENSORS IS WITHIN TOLERANCE RANGE
  {
    if (avl > avr) // IF AVERAGE LIGHT SENSOR VALUES ON LEFT SIDE ARE GREATER THAN RIGHT SIDE, AZIMUTH MOTOR ROTATES CLOCKWISE
    {
      digitalWrite(AREN, HIGH); analogWrite(ARPWM, 255); // SET SPEED OUT OF POSSIBLE RANGE 0~255

      digitalWrite(ALEN, HIGH);  digitalWrite (ALPWM, LOW);
      Serial.println("AZIMUTH MOTOR MOVES CLOCKWISE");
      Serial.println("   ");
    }
    else // IF AVERAGE LIGHT SENSOR VALUES ON RIGHT SIDE ARE GREATER THAN ON LEFT SIDE, AZIMUTH MOTOR ROTATES COUNTERCLOCKWISE
    {
      digitalWrite(ALEN, HIGH); analogWrite(ALPWM, 255);
      digitalWrite(AREN, HIGH); digitalWrite(ARPWM, LOW);
      Serial.println("AZIMUTH MOTOR MOVES COUNTERCLOCKWISE");
      Serial.println("   ");
    }
  }
  else if (-1 * TE < dh || dh < TE) //IF DIFFERENCE IS SMALLER THAN TOLERANCE, STOP AZIMUTH MOTOR
  {
    digitalWrite(AREN, LOW);  digitalWrite(ALEN, LOW);
    Serial.println("AZIMUTH MOTOR STOPS");
    Serial.println("   ");
  }

  if (-1 * TE > dv || dv > TE) //CHECK IF THE DIFFERENCE IN TOP/BOTTOM LIGHT SENSORS IS GREATER THAN TOLERANCE
  {
    if (avt > avd) //IF AVERAGE LIGHT SENSOR VALUES ON TOP SIDE ARE GREATER THAN ON BOTTOM SIDE THEN ELEVATION MOTOR ROTATES CLOCKWISE
    {
      digitalWrite(EREN, HIGH); analogWrite(ERPWM, 255);
      digitalWrite(ELEN, HIGH); digitalWrite(ELPWM, LOW);
      Serial.println("ELEVATION MOTOR MOVES CLOCKWISE");
      Serial.println("   ");
    }
    else //IF AVERAGE LIGHT SENSOR VALUES ON BOTTOM SIDE ARE GREATER THAN ON TOP SIDE THEN ELEVATION MOTOR ROTATES COUNTERCLOCKWISE
    {
      digitalWrite(ELEN, HIGH); analogWrite(ELPWM, 255);
      digitalWrite(EREN, HIGH); digitalWrite(ERPWM, LOW);
      Serial.println("ELEVATION MOTOR MOVES COUNTERCLOCKWISE");
      Serial.println("   ");
    }
  }
  else if (-1 * TE < dv || dv < TE) //IF DIFFERENCE IS SMALLER THAN TOLERANCE, STOP ELEVATION MOTOR
  {
    digitalWrite(EREN, LOW);  digitalWrite(ELEN, LOW);
    Serial.println("ELEVATION MOTOR STOPS");
    Serial.println("   ");
  }
  delay(DT);
}




Useful CAD/ 3D model files for
Arduino, hobbyist, and engineering projects

https://grabcad.com/has-2

PaulS

Code: [Select]
// ==== Storing the incoming data into a String variable
while (HC12.available())  // If HC-12 has data
  {           
    Serial.println(ss.c_str());
    Serial.println();
    delay(DT);
    delay(2000);
   }

That code does NOT store incoming data in a String. Your program should have NO Strings. AT ALL.

If there ever is data available, that while loop will never end, since you never actually read the data.

Quote
The (transmitter) arduino pro mini serial monitor is showing up but the values remain static.
Well, the only Serial.print() statement that you have in the code is in that while statement's body, where it prints the String it sent once every 2+ seconds.

You never send a second String, so static is reasonable.
The art of getting good answers lies in asking good questions.

knightridar

Thanks for the clarification....

i got rid of the while loop and turned it into an if statement

i also have the println declared before the if (Serial.available()) statement.

The values on the transmitter serial monitor are now dynamic and I am able to use the code on the arduino pro mini.

For the software serial pins I will change them again
once I get things working like I had before using just pins 0,1.

On the receiver end I know my code is cycling because I get my println statments of
azimuth motor stops
elevation motor stops.

However, I'm getting 0s for all my values. At first i was getting single digits but now I get nothing but 0s.


TRANSMITTER CODE:

Code: [Select]

// SPECIAL THANKS TO GEO BRUCE ON INSTRUCTABLES.COM

#include <SoftwareSerial.h>
SoftwareSerial HC12(0,1); //RX,TX   

void setup()
{
  Serial.begin(9600); //SERIAL PORT TO PC
  HC12.begin(9600); //SERIAL PORT TO HC12
}
void loop()
{
  String sendTR; //STRING TO STORE ANALOG VALUE
  String sendBR;
  String sendTL;
  String sendBL;
  String sendDT;
  String sendTE;
  String ss;
 
  char buff[6]; //BUFFER FOR 6 CHARACTERS

  //LIGHT SENSORS CONNNECTED TO ANALOG PIN ON ARDUINO BOARD
  int TR = analogRead(A1); //TOP RIGHT SENSOR
  int BR = analogRead(A0); //BOTTOM RIGHT SENSOR
  int TL = analogRead(A2); //TOP LEFT SENSOR
  int BL = analogRead(A3); //BOTTOM LEFT SENSOR
  int DT = analogRead(A6) * 4; //CONTROL DELAY TIME BETWEEN READINGS OF SENSORS
  int TE = analogRead(A7) / 4; //SET TOLERANCE VALUE FOR DIFFERENCE BETWEEN SENSORS
 
  sendTR+=itoa(TR,buff,10); //TAKES AN INTEGER AND CONVERTS IT INTO AN ASCII/NUMBER STRING (CONVERT INTEGER TO STRING (NON-STANDARD FUNCTION))
  sendBR+=itoa(BR,buff,10); //char *  itoa ( int value, char * str, int base );
  sendTL+=itoa(TL,buff,10);
  sendBL+=itoa(BL,buff,10);
  sendDT+=itoa(DT,buff,10);
  sendTE+=itoa(TE,buff,10);
 
  ss+=sendTR; //ss = ss + sentTR
  ss+="&"; //ss = ss + &
  ss+=sendBR;
  ss+="&";
  ss+=sendTL;
  ss+="&";
  ss+=sendBL;
  ss+="&";
  ss+=sendDT;
  ss+="&";
  ss+=sendTE;
  ss+="&";

  Serial.println(ss.c_str());
  Serial.println();
  delay(DT);
 
if (Serial.available())  // IF SERIAL MONITOR HAS DATA
  {
  HC12.write(ss.c_str()); //SEND DATA TO HC-12
  delay(DT);
  }
}



RECEIVER CODE:

Code: [Select]

// SPECIAL THANKS TO GEO BRUCE ON INSTRUCTABLES.COM

#include <SoftwareSerial.h>
SoftwareSerial HC12(0,1); //RX, TX

byte incomingByte;
String readBuffer = "";

// AZIMUTH AND ELEVATION PWM PINS ON EACH H-BRIDGE MUST BE CONNECTED TO FOUR PWM (PULSE WIDTH MODULATION) PINS ON ARDUINO.
// FOR UNO/MICRO/PRO MINI THEY ARE: 3,5,6,9,10,11.

int AREN = 2; int ARPWM = 3; int ALEN = 4; int ALPWM = 5; // MOTOR AZIMUTH ADJUSTMENT
int EREN = 7; int ERPWM = 6; int ELEN = 8; int ELPWM = 9; // MOTOR ELEVATION ADJUSTMENT

void setup()
{
  Serial.begin(9600); //SERIAL PORT TO PC
  HC12.begin(9600); //SERIAL PORT TO HC12
  pinMode(AREN, OUTPUT); pinMode(ARPWM, OUTPUT); pinMode(ALEN, OUTPUT); pinMode(ALPWM, OUTPUT);  //SET ALL MOTOR CONTROL PINS TO OUTPUTS
  pinMode(EREN, OUTPUT); pinMode(ERPWM, OUTPUT); pinMode(ELEN, OUTPUT); pinMode(ELPWM, OUTPUT);
}
void loop()
{
// ==== STORING THE INCOMING DATA INTO A STRING VARIABLE
readBuffer="";
if (HC12.available()) //IF HC-12 HAS DATA
  {         
    incomingByte = HC12.read();          //STORE EACH INCOMING BYTE FROM HC-12
    readBuffer += char(incomingByte);    //ADD EACH BYTE TO ReadBuffer STRING VARIABLE
  }

//SYNTAX: string.indexOf(val) - LOCATES A CHARACTER OR STRING WITHIN ANOTHER STRING. BY DEFAULT, SEARCHES FROM THE BEGINNING OF THE STRING, BUT CAN ALSO START FROM A GIVEN INDEX, ALLOWING FOR THE LOCATING OF ALL INSTANCES OF THE CHARACTER OR STRING.

int firstAnd= readBuffer.indexOf("&");   
String firstTR = readBuffer.substring(0,firstAnd);
int TR= firstTR.toInt();

int secondAnd= readBuffer.indexOf("&", firstAnd+1);
String firstBR = readBuffer.substring(firstAnd+1,secondAnd);
int BR= firstBR.toInt();

int thirdAnd= readBuffer.indexOf("&", secondAnd+1);
String firstTL = readBuffer.substring(secondAnd+1,thirdAnd);
int TL= firstTL.toInt();
 
int fourthAnd= readBuffer.indexOf("&",thirdAnd+1);
String firstBL = readBuffer.substring(thirdAnd+1,fourthAnd);
int BL = firstBL.toInt();

int fifthAnd= readBuffer.indexOf("&",fourthAnd+1);
String firstDT = readBuffer.substring(fourthAnd+1,fifthAnd);
int DT= firstDT.toInt();

int sixthAnd= readBuffer.indexOf("&", fifthAnd+1);
String firstTE = readBuffer.substring(fifthAnd+1,sixthAnd);
int TE= firstTE.toInt();

Serial.println(TR);
Serial.println(BR);
Serial.println(TL);
Serial.println(BL);
Serial.println(DT);
Serial.println(TE);
Serial.println("========================");
delay(4000);

int count = 0; //START MILLISECOND COUNT OF LIGHT SENSOR READINGS
count++; //INCREMENTAL COUNT INCREASE, CONTINUES TO SHOW LIGHT SENSOR RESULTS

int avt = (TR + TL) / 2; // AVERAGE VALUE TOP
int avd = (BL + BR) / 2; // AVERAGE VALUE DOWN
int avl = (TL + BL) / 2; // AVERAGE VALUE LEFT
int avr = (TR + BR) / 2; // AVERAGE VALUE RIGHT

int dv = avt - avd; // AVERAGE DIFFERENCE OF TOP AND BOTTOM LIGHT SENSORS
int dh = avl - avr;// AVERAGE DIFFERENCE OF LEFT AND RIGHT LIGHT SENSORS

  if (-1 * TE > dh || dh > TE) // CHECK IF THE DIFFERENCE IN LEFT AND RIGHT LIGHT SENSORS IS WITHIN TOLERANCE RANGE
  {
    if (avl > avr) // IF AVERAGE LIGHT SENSOR VALUES ON LEFT SIDE ARE GREATER THAN RIGHT SIDE, AZIMUTH MOTOR ROTATES CLOCKWISE
    {
      digitalWrite(AREN, HIGH); analogWrite(ARPWM, 255); // SET SPEED OUT OF POSSIBLE RANGE 0~255

      digitalWrite(ALEN, HIGH);  digitalWrite (ALPWM, LOW);
      Serial.println("AZIMUTH MOTOR MOVES CLOCKWISE");
      Serial.println("   ");
    }
    else // IF AVERAGE LIGHT SENSOR VALUES ON RIGHT SIDE ARE GREATER THAN ON LEFT SIDE, AZIMUTH MOTOR ROTATES COUNTERCLOCKWISE
    {
      digitalWrite(ALEN, HIGH); analogWrite(ALPWM, 255);
      digitalWrite(AREN, HIGH); digitalWrite(ARPWM, LOW);
      Serial.println("AZIMUTH MOTOR MOVES COUNTERCLOCKWISE");
      Serial.println("   ");
    }
  }
  else if (-1 * TE < dh || dh < TE) //IF DIFFERENCE IS SMALLER THAN TOLERANCE, STOP AZIMUTH MOTOR
  {
    digitalWrite(AREN, LOW);  digitalWrite(ALEN, LOW);
    Serial.println("AZIMUTH MOTOR STOPS");
    Serial.println("   ");
  }

  if (-1 * TE > dv || dv > TE) //CHECK IF THE DIFFERENCE IN TOP/BOTTOM LIGHT SENSORS IS GREATER THAN TOLERANCE
  {
    if (avt > avd) //IF AVERAGE LIGHT SENSOR VALUES ON TOP SIDE ARE GREATER THAN ON BOTTOM SIDE THEN ELEVATION MOTOR ROTATES CLOCKWISE
    {
      digitalWrite(EREN, HIGH); analogWrite(ERPWM, 255);
      digitalWrite(ELEN, HIGH); digitalWrite(ELPWM, LOW);
      Serial.println("ELEVATION MOTOR MOVES CLOCKWISE");
      Serial.println("   ");
    }
    else //IF AVERAGE LIGHT SENSOR VALUES ON BOTTOM SIDE ARE GREATER THAN ON TOP SIDE THEN ELEVATION MOTOR ROTATES COUNTERCLOCKWISE
    {
      digitalWrite(ELEN, HIGH); analogWrite(ELPWM, 255);
      digitalWrite(EREN, HIGH); digitalWrite(ERPWM, LOW);
      Serial.println("ELEVATION MOTOR MOVES COUNTERCLOCKWISE");
      Serial.println("   ");
    }
  }
  else if (-1 * TE < dv || dv < TE) //IF DIFFERENCE IS SMALLER THAN TOLERANCE, STOP ELEVATION MOTOR
  {
    digitalWrite(EREN, LOW);  digitalWrite(ELEN, LOW);
    Serial.println("ELEVATION MOTOR STOPS");
    Serial.println("   ");
  }
  delay(DT);
}




Useful CAD/ 3D model files for
Arduino, hobbyist, and engineering projects

https://grabcad.com/has-2

PaulS

Quote
i also have the println declared before the if (Serial.available()) statement.
I'm sure that that collection of words meant something to you when you typed them. They mean nothing to me.

Quote
For the software serial pins I will change them again
once I get things working like I had before using just pins 0,1.
When I'm done doing something that can't be done...
The art of getting good answers lies in asking good questions.

knightridar

Code: [Select]

Serial.println(ss.c_str());
Serial.println();
delay(DT);

if (Serial.available())  // IF SERIAL MONITOR HAS DATA
{
HC12.write(ss.c_str()); //SEND DATA TO HC-12
delay(DT);
}
}



I moved these before the if statement:

Serial.println(ss.c_str());
Serial.println();
delay(DT);

{
HC12.write(ss.c_str()); //SEND DATA TO HC-12
delay(DT);
// Serial.println(ss.c_str());
// Serial.println();
// delay(DT);
}

So the output on the transmitting serial monitor shows up as this:




I've been taking a look at the c_str function at:
http://www.cplusplus.com/reference/string/string/c_str/

I'm wondering is the reason why I'm not receiving code properly on the receiver side is because I don't have a proper Null character i.e. '0\'?

Useful CAD/ 3D model files for
Arduino, hobbyist, and engineering projects

https://grabcad.com/has-2

Robin2

So the output on the transmitting serial monitor shows up as this:
In Reply #12 I told you that those images are unreadable.

I also gave you a link to some code that would probably have solved your problem.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

knightridar

@Robin2 I did check out the serial input basics guide a while back.
I read over it again a few times.
Thanks!!!
Examples 4 and 5 seem like the closest to what I'm trying to achieve.
I was confused with parts of the code that didn't have any comments.


FYI... for the images with text try holding CTRL and press + to zoom in.
It works for me. If not I'll start pasting in my outputs anyways.

//************************************//

I found this example online and it accomplishes almost half of what I'm trying to do:

https://www.youtube.com/watch?v=usML5AlrqaA

http://www.botsrule.com/pattonrobotics/Multi%20Analog%20Read%20RF%20transmit%20and%20recieve%20demo.html


//******************************************************************//

The code below was easy for me to understand and implement/modify.
The problem I'm getting is my last two variables are not showing any results on the receiver side serial monitor.

My goal is to convert this received data back into an integer and then use it for the rest of the program


A = 338
B = 409
C = 309
D = 351
E =
F =
 
A = 337
B = 419
C = 313
D = 358
E =
F =
 
A = 356
B = 450
C = 331
D = 380
E =
F =


TRANSMITTER CODE:

Code: [Select]

#include <SoftwareSerial.h>

int HC12RxdPin = 12;                  // Receive Pin on HC12
int HC12TxdPin = 11;                  // Transmit Pin on HC12

SoftwareSerial HC12(HC12TxdPin,HC12RxdPin); // Create Software Serial Port

void setup() {
// initialize serial communications at 9600 bits per second for each port:
  Serial.begin(9600);
  HC12.begin(9600); //SERIAL PORT TO HC12
}
//********************************************************
//    Send Hardware Serial data
//********************************************************
void sendHWSerial() {

  //LIGHT SENSORS CONNNECTED TO ANALOG PIN ON ARDUINO BOARD
  int TR = analogRead(A1); //TOP RIGHT SENSOR
  int BR = analogRead(A0); //BOTTOM RIGHT SENSOR
  int TL = analogRead(A2); //TOP LEFT SENSOR
  int BL = analogRead(A3); //BOTTOM LEFT SENSOR
  int DT = analogRead(A6) * 4; //CONTROL DELAY TIME BETWEEN READINGS OF SENSORS
  int TE = analogRead(A7) / 4; //SET TOLERANCE VALUE FOR DIFFERENCE BETWEEN SENSORS

  Serial.println('A' + String(TR)); // print out the value you read:
  Serial.println('B' + String(BR)); // print out the value you read:
  Serial.println('C' + String(TL)); // print out the value you read:
  Serial.println('D' + String(BL)); // print out the value you read:
  Serial.println('E' + String(DT)); // print out the value you read:
  Serial.println('F' + String(TE)); // print out the value you read:
  Serial.println(" ");
 
  HC12.print(String(TR) + 'A'); // Send data to Hardware Serial
  HC12.print(String(BR) + 'B'); // Send data to Hardware Serial
  HC12.print(String(TL) + 'C'); // Send data to Hardware Serial
  HC12.print(String(BL) + 'D'); // Send data to Hardware Serial
  HC12.print(String(DT) + 'E'); // Send data to Hardware Serial
  HC12.print(String(TE) + 'F'); // Send data to Hardware Serial
  HC12.print(" "); // Send data to Hardware Serial
  delay(4000);
}
//********************************************************
//      Main Loop (loops forever)
//********************************************************
void loop() {
{                   // If Arduino's computer rx buffer has data
   sendHWSerial();            // Send that data to serial
   delay(10);        // delay in between reads for stability
}
}



RECEIVER CODE:


Code: [Select]

#include <SoftwareSerial.h>

const byte HC12RxdPin = 11;                  // Receive Pin on HC12
const byte HC12TxdPin = 12;                  // Transmit Pin on HC12

SoftwareSerial HC12(HC12TxdPin,HC12RxdPin); // Create Software Serial Port

void setup() {
// initialize serial communications at 9600 bits per second for each port:
  Serial.begin(9600);
  HC12.begin(9600); //SERIAL PORT TO HC12
}
//********************************************************
//    Receive Hardware Serial data
//********************************************************
void getHWSerial() {
  while (HC12.available() == 0);  // wait until there is data in the buffer
  if (HC12.available() > 0) { //if there is data in the serial buffer.....
    Serial.read();
    Serial.println("A = " + String(HC12.readStringUntil('A')));    // Print Collected data until a 'A' is detected
    Serial.println("B = " + String(HC12.readStringUntil('B')));   
    Serial.println("C = " + String(HC12.readStringUntil('C')));
    Serial.println("D = " + String(HC12.readStringUntil('D')));
    Serial.println("E = " + String(HC12.readStringUntil('E')));
    Serial.println("F = " + String(HC12.readStringUntil('F')));   
    Serial.println(" ");    //
  }
 
}

//********************************************************
//      Main Loop (loops forever)
//********************************************************
void loop() {
  getHWSerial();
  delay(10);        // delay in between reads for stability
}
Useful CAD/ 3D model files for
Arduino, hobbyist, and engineering projects

https://grabcad.com/has-2

Robin2

if there is part of my Serial Input Basics code that you don't understand then I will try to help if you tell me what it is.

The code you posted in Reply #25 uses the String class which I advised against in Reply #10

...R

Two or three hours spent thinking and reading documentation solves most programming problems.

PaulS

Why are you printing one thing to the hardware serial port and another to the software serial port?

Why the f**k can't you use two print() calls instead of wasting memory on converting an int to a String that the print() method has to then unwrap? The print() method is perfectly capable of converting an int to a string, without your "help".

Why does sendHWSerial() send data to the software serial port?
The art of getting good answers lies in asking good questions.

knightridar

Geez no need for crude language.
I'm not a native programmer by profession or trade.
I'm trying to learn on my own as a hobby.

All I can say it at this point...

1. I see what you mean I can get rid of the strings on the println portion of the code.
   I just copied code from the site mentioned in my previous post and adapted it to my application.
 
   Only reason I'm printing on the hardware serial port so I can view what I am outputting from
   the transmitting Arduino on the serial monitor.
   If I print via software serial I can't view anything on my serial monitor.
   Correct???

2. sendHWSerial() ---> Maybe I just need to rename this, I'm sending data via the software serial port and HC-12 transmitter based on your earlier recommendations that hardware serial ports 0,1
should/can't be used.

3. My main objective is to convert my transmitted analog values
(vary from 0 to 1023 for 4 light dependent resistors,
and for potentiometers it's 0-255, 0 to 4096)) and assign them
to their respective variables on the HC-12 receiver side.

My understanding is that I'm sending my analog values as a string of characters???
(Based on a post I read on the forum if data is sent via bytes my analog sensor values can only be sent between the ranges of 0 to 255.)

I need some sort of delimiter to split the values so that they can be assigned to the proper respective variables on the receiver end of my code.

I am writing up some code using the parse function (i.e example 4 modified <TR, 1023>) in the serial basics guide provided by Robin2 to try to mimic the same functionality as the code below.

My friends add-ons to my code were working at one point on the receiver end but all I get now are  0s printing on the receiver serial monitor.
He is using the "&" character as a delimiter if my understanding is correct.



Code: [Select]

int firstAnd= readBuffer.indexOf("&");   
String firstTR = readBuffer.substring(0,firstAnd);
int TR= firstTR.toInt();

int secondAnd= readBuffer.indexOf("&", firstAnd+1);
String firstBR = readBuffer.substring(firstAnd+1,secondAnd);
int BR= firstBR.toInt();

int thirdAnd= readBuffer.indexOf("&", secondAnd+1);
String firstTL = readBuffer.substring(secondAnd+1,thirdAnd);
int TL= firstTL.toInt();
 
int fourthAnd= readBuffer.indexOf("&",thirdAnd+1);
String firstBL = readBuffer.substring(thirdAnd+1,fourthAnd);
int BL = firstBL.toInt();

int fifthAnd= readBuffer.indexOf("&",fourthAnd+1);
String firstDT = readBuffer.substring(fourthAnd+1,fifthAnd);
int DT= firstDT.toInt();

int sixthAnd= readBuffer.indexOf("&", fifthAnd+1);
String firstTE = readBuffer.substring(fifthAnd+1,sixthAnd);
int TE= firstTE.toInt();



Useful CAD/ 3D model files for
Arduino, hobbyist, and engineering projects

https://grabcad.com/has-2

PaulS

Quote
Only reason I'm printing on the hardware serial port so I can view what I am outputting from
   the transmitting Arduino on the serial monitor.
   If I print via software serial I can't view anything on my serial monitor.
   Correct???
Yes, that is correct. But, the whole idea of printing the serial port is so that you can see what is also being sent to the bluetooth device, so you can parse it on the other end.

It makes no sense to print one thing to the hardware serial port and something else to the softare serial port. You haven't learned a thing about what you need to parse.
The art of getting good answers lies in asking good questions.

Go Up