Arduino Forum

Using Arduino => Programming Questions => Topic started by: knightridar on Aug 20, 2017, 02:54 am

Title: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: knightridar on Aug 20, 2017, 02:54 am
HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER


I am having trouble converting my analog values from my transmitter converted back to an integer on the receiving end.

The problem is I don't know how to convert the values I'm receiving
back to an integer so that I don't get an undeclared variable error on my receiving end program

Code: [Select]

// Special thanks to Geo Bruce on instructables.com for his version of the code for dual axis solar tracking.
// Special thanks to Dejan Nedelkovski for his version of the code for wireless transfer of data.

#include <SoftwareSerial.h>

SoftwareSerial HC12(0, 1); //RX, TX

byte incomingByte;
String readBuffer = "";

void setup()
{
  Serial.begin(9600); //SERIAL PORT TO PC
  HC12.begin(9600); //SERIAL PORT TO HC12
}

void loop()
{
  // LIGHT SENSOR (in this case a Light Dependent Resistor) connected to analog pin on respective arduino board
  int TOPRIGHT = analogRead(1);
  int BOTTOMRIGHT = analogRead(0);
  int TOPLEFT = analogRead(2);
  int BOTTOMLEFT = analogRead(3);
  int delaytime = analogRead(A5) * 4; // control delay time in milliseconds of LIGHT SENSOR readings
  int tolerance = analogRead(A7) / 4; // set range of tolerance between LIGHT SENSOR readings

// ==== Storing the incoming data into a String variable

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
  }
  delay(delaytime);
 
// ==== Sending data from one HC-12 to another via the Serial Monitor
while (Serial.available())
  {
    HC12.write(Serial.read());
  }
  //print LIGHT SENSOR values to serial monitor for debugging
  Serial.println();
  Serial.println(TOPLEFT);
  Serial.println(BOTTOMLEFT);
  Serial.println(TOPRIGHT);
  Serial.println(BOTTOMRIGHT);
  Serial.println();
  Serial.println(delaytime);
  Serial.println(tolerance);
  Serial.println();
}








For the first version of the receiving end of my code I am able to get values transmitted successfully to the receiver

Code: [Select]

//RECEIVE

#include <SoftwareSerial.h>

SoftwareSerial HC12(0, 1); //RX, TX

byte incomingByte;
String readBuffer = "";


void setup()
{
  Serial.begin(9600); //SERIAL PORT TO PC
  HC12.begin(9600); //SERIAL PORT TO HC12
}

void loop()
{

// ==== Storing the incoming data into a String variable

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
  }
  delay(1000);
 
// ==== Sending data from one HC-12 to another via the Serial Monitor
while (Serial.available())
  {
    HC12.write(Serial.read());
  }

Serial.println(readBuffer);

}



For the second revision of my code I am not able to receive values and interpret them so that I can get my motors moving
(I get an undeclared variable error, which is understandable since the values are no longer integers, correct?):

Code: [Select]

// Special thanks to Geo Bruce on instructables.com for his version of the code for dual axis solar tracking.
// Special thanks to Dejan Nedelkovski for his version of the code for wireless transfer of data.

#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 the 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
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
  }
  delay(1000);
 
// ==== Sending data from one HC-12 to another via the Serial Monitor
while (Serial.available())
  {
    HC12.write(Serial.read());
  }

Serial.println(readBuffer);
 
  int count = 0; //start millisecond count of LIGHT SENSOR readings
  count++; //incremental count increase, continues to show LIGHT SENSOR results

  int avt = (TOPRIGHT + TOPLEFT) / 2; // average value top
  int avd = (BOTTOMLEFT + BOTTOMRIGHT) / 2; // average value down
  int avl = (TOPLEFT + BOTTOMLEFT) / 2; // average value left
  int avr = (TOPRIGHT + BOTTOMRIGHT) / 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 * tolerance > dh || dh > tolerance) // 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 * tolerance < dh || dh < tolerance) //if difference is smaller than tolerance, STOP azimuth motor
  {
    digitalWrite(AREN, LOW);  digitalWrite(ALEN, LOW);
    Serial.println("AZIMUTH MOTOR STOPS");
    Serial.println("   ");
  }

  if (-1 * tolerance > dv || dv > tolerance) // 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 * tolerance < dv || dv < tolerance) // if difference is smaller than tolerance, STOP elevation motor
  {
    digitalWrite(EREN, LOW);  digitalWrite(ELEN, LOW);
    Serial.println("ELEVATION MOTOR STOPS");
    Serial.println("   ");
  }
  delay(delaytime);
}



A friend of mine suggested the following:

Do it this way(TRANSMITTER):

Get the value for Analog pins for different variables. For example:

Int TOPLEFT= analogRead(2);

Char charTOPLEFT= "TL" + char(TOPLEFT);
(Things error out for me right here. So I just used one char command and things still error out)

Send data as : "TL<value>"
Send data as : "BL<value>"
Send data as : "TR<value>"
Send data as : "BR<value>"

HC12.write("TL267");


====================================================

Received Value: For example---- readbuffer = "TL267"
Now you need to write a code like this.

Not sure whether split() is available in arduino's C. :-p

If (readbuffer.startsWith("TL")) {
   charTOPLEFT=readbuffer.split("TL")[1];
}
Else If (readbuffer.startsWith("TR")) {
   charTOPRIGHT=readbuffer.split("TR")[1];
}
Else If (readbuffer.startsWith("BL")) {
   charBOTTOMLEFT=readbuffer.split("BL")[1];
}
Else If (readbuffer.startsWith("BR")) {
   charBOTTOMRIGHT=readbuffer.split("BR")[1];
}

TOPLEFT=atoi(charTOPLEFT);



Any suggestions??? Thanks.
I think the atoi command has more potential on its own versus the char command.
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: jremington on Aug 20, 2017, 03:00 am
You shouldn't be using pins 0 and 1 for Software serial, they are for program uploading and hardware serial.

See Serial Input Basics (http://forum.arduino.cc/index.php?topic=396450) for an overview on how to send and receive data; works for either software or hardware serial.

Finally, avoid using String objects. They will eventually cause the Arduino to crash due to memory problems.
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: knightridar on Aug 20, 2017, 03:02 am
Attached is a screenshot of the successful transmission using the first version of the receiving code.


Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: knightridar on Aug 20, 2017, 03:10 am
@jremington thanks ah i see....


Although I can successfully upload right now via usb on my arduino uno and use pins 0 and 1 for software serial.
I am guessing this will be a problem for uploading code and using my arduino pro mini to accomplish the same task?


I think I understand what you mean by avoiding the string command.

https://arduino.stackexchange.com/questions/1013/how-do-i-split-an-incoming-string


The response by one of the members makes references to memory buildup as an issue.


Since I'm not too code savvy the string function (using Dejan Nedelkovski's code) is the only way I have been able to successfully transmit these values.
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: jremington on Aug 20, 2017, 03:52 am
Quote
Since I'm not too code savvy
You will learn quickly by following the examples in Serial Input Basics.
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: knightridar on Aug 22, 2017, 09:27 am
I am taking a look at the serial input basics guide....

http://forum.arduino.cc/index.php?topic=396450


Reading up on parsing data.
Since a few people have mentioned to avoid using the string method.

I am taking the void functions on that guide and inputting it into my code.

I have converted all of the following values to char:

 
Code: [Select]


 // LIGHT SENSOR (in this case a Light Dependent Resistor) connected to analog pin on respective 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(A5) * 4; // control delay time in milliseconds of LIGHT SENSOR readings
  int TE = analogRead(A7) / 4; // set range of tolerance between LIGHT SENSOR readings

  char CTR = "TR" + char(TR);
  char CBR = "BR" + char(BR);
  char CTL = "TL" + char(TL);
  char CBL = "BL" + char(BL);
  char CDT = "DT" + char(DT);
  char CTE = "TE" + char(TE);

Code: [Select]


I've already tried inputting these values i.e. ctr, cbr, etc
into a serial.println command in the void showParseData() function.

I get a "CTR" not declared in this scope error.

Can someone point me in the right direction as to where I should be inputting these variables?

My next guess is they will have to be input into the strtokIndx variable?

Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: Robin2 on Aug 22, 2017, 09:41 am
I am taking the void functions on that guide and inputting it into my code.
If you want help you must post the complete program that you have tried.

...R
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: edgemoron on Aug 22, 2017, 10:03 am
Code: [Select]
while (Serial.available())
Try:
Code: [Select]
while (Serial.available() > 0)
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: PaulS on Aug 22, 2017, 10:51 am
Quote
Since a few people have mentioned to avoid using the string method.
NO one has suggested not using strings.

EVERYONE suggests not using Strings.

Reading comprehension IS important.
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: knightridar on Aug 23, 2017, 08:16 am
@PaulS

I see thanks.
I am using the string command again since it's easier for me to comprehend that code and apply it.
I've attached an image of the serial data I am sending and receiving.


Objective:

On the receiving HC-12 transmitter I want to split my incoming values back into integers.
I want those integer values to be reassociated with my global variables.

i.e.

TR= 1203
TL= 906
DT = 4096  etcc......


This way the rest of the receiving code can continue through the void loop and complete its function.


For my receiving code R4, (below)

I get the following error:

exit status 1
cannot convert 'String' to 'int' in initialization

I am trying to use the substring command to capture portions of the string readBuffer and convert it back to an int value.


TRANSMITTING:

Code: [Select]

// Special thanks to Geo Bruce on instructables.com and Dejan Nedelkovski from howtomechatronics.com

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

byte incomingByte;
String readBuffer = "";

void setup()
{
  Serial.begin(9600); //SERIAL PORT TO PC
  HC12.begin(9600); //SERIAL PORT TO HC12
}
void loop()
{
  // LIGHT SENSOR (in this case a Light Dependent Resistor) connected to analog pin on respective 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(A5) * 4; // control delay time in milliseconds of LIGHT SENSOR readings
  int TE = analogRead(A7) / 4; // set range of tolerance between LIGHT SENSOR readings

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

// ==== Storing the incoming data into a String variable
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
  }
// ==== Sending data from one HC-12 to another via the Serial Monitor
while (Serial.available())
  {
    HC12.write(Serial.read());
  }
}



RECEIVING CODE R1 THAT WORKS:

Code: [Select]

//RECEIVE

#include <SoftwareSerial.h>

SoftwareSerial HC12(0,1); //RX, TX

byte incomingByte;
String readBuffer = "";

void setup()
{
  Serial.begin(9600); //SERIAL PORT TO PC
  HC12.begin(9600); //SERIAL PORT TO HC12
}
void loop()
{
// ==== Storing the incoming data into a String variable

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
  }
Serial.println(readBuffer);
delay(4000);
}




RECEIVING CODE R4 (WHERE I AM STUCK):

Code: [Select]

// Special thanks to Geo Bruce on instructables.com and Dejan Nedelkovski from howtomechatronics.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 the 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

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
  }
  Serial.println(readBuffer);
  delay(4000);

  // LIGHT SENSOR (in this case a Light Dependent Resistor) connected to analog pin on respective arduino board
  int TR = readBuffer.substring(1,4)); // TOP RIGHT SENSOR
  int BR = readBuffer.substring(5,8); // BOTTOM RIGHT SENSOR
  int TL = readBuffer.substring(9,12); // TOP LEFT SENSOR
  int BL = readBuffer.substring(13,16); // BOTTOM LEFT SENSOR
  int DT = readBuffer.substring(17,20); // control delay time in milliseconds of LIGHT SENSOR readings
  int TE = readBuffer.substring(21,24); // set range of tolerance between LIGHT SENSOR readings


  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 * tolerance > dh || dh > tolerance) // 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 * tolerance < dh || dh < tolerance) //if difference is smaller than tolerance, STOP azimuth motor
  {
    digitalWrite(AREN, LOW);  digitalWrite(ALEN, LOW);
    Serial.println("AZIMUTH MOTOR STOPS");
    Serial.println("   ");
  }

  if (-1 * tolerance > dv || dv > tolerance) // 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 * tolerance < dv || dv < tolerance) // if difference is smaller than tolerance, STOP elevation motor
  {
    digitalWrite(EREN, LOW);  digitalWrite(ELEN, LOW);
    Serial.println("ELEVATION MOTOR STOPS");
    Serial.println("   ");
  }
  delay(DT);
}





Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: Robin2 on Aug 23, 2017, 08:40 am
I am using the string command again since it's easier for me to comprehend that code and apply it.
You are using the String class (capital S). It is not a good idea to use it on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings (http://www.cplusplus.com/reference/cstring/) - char arrays terminated with 0.

...R
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: Robin2 on Aug 23, 2017, 08:41 am
Images from Reply #9 so we don't have to download them. See this  Image Guide (http://forum.arduino.cc/index.php?topic=364156.0)





...R
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: Robin2 on Aug 23, 2017, 08:42 am
Those images are unreadable. Please don't post text as a picture, Just copy and paste it as text.

Have a look at the examples in Serial Input Basics (http://forum.arduino.cc/index.php?topic=396450.0) - simple reliable ways to receive data.

...R
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: PaulS on Aug 23, 2017, 01:07 pm
Quote
I am using the string command again since it's easier for me to comprehend that code and apply it.
There's no such thing as "the string command", so you're blowing smoke.
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: knightridar on Aug 28, 2017, 09:42 pm
Well I had a friend help me out with the code and I am successfully transferring values and converting them back into integers.

Now I can reinsert those values into the receiving code void loop and it works.

I switched from using an Arduino Uno to an Arduino Pro mini for my transmitting setup because that is what I'm intending to use for my final setup.

The analog values from the sensor are not updating when I powered the HC-12 module
using my 3.3 V, 8 mhz arduino pro mini.
I am guessing it may be a power issue.
The values to update from one arduino uno to another arduino uno.

I am open to making the code more efficient if anyone has any input.

Here is the code for reference in case anyone wants to transmit analog values and receive them and convert them back into variables.

Transmit 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+="&";
 
// ==== 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);
   }
}



RECEIVE 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="";
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(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);
}

Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: PaulS on Aug 29, 2017, 12:13 am
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.
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: knightridar on Aug 29, 2017, 12:26 am
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.
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: PaulS on Aug 29, 2017, 12:34 am
Quote
I can't figure out what's wrong.
Probably your wiring.
Definitely your code.
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: knightridar on Aug 29, 2017, 01:44 am
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 ??? ...
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: knightridar on Aug 29, 2017, 08:40 am
@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);
}




Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: PaulS on Aug 29, 2017, 11:22 am
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.
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: knightridar on Aug 30, 2017, 09:53 am
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);
}




Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: PaulS on Aug 30, 2017, 12:05 pm
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...
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: knightridar on Aug 31, 2017, 08:50 am
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\'?


Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: Robin2 on Aug 31, 2017, 10:23 am
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
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: knightridar on Sep 07, 2017, 06:04 am
@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
}
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: Robin2 on Sep 07, 2017, 09:28 am
if there is part of my Serial Input Basics (http://forum.arduino.cc/index.php?topic=396450.0) 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

Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: PaulS on Sep 07, 2017, 11:11 am
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?
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: knightridar on Sep 07, 2017, 10:49 pm
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();



Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: PaulS on Sep 08, 2017, 02:42 am
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.
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: Robin2 on Sep 08, 2017, 09:08 am
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.
If this was my project I would use the code in my Serial Input Basics (http://forum.arduino.cc/index.php?topic=396450.0) and I believe it would be working inside an hour.

Start by just receiving the data and printing it to the Serial Monitor.

When that works extend it to parse the data.

...R
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: knightridar on Sep 09, 2017, 11:15 am
@Robin2 Thanks. I read through the guide more thoroughly.

The code is working for data transmission from one Arduino Uno to another Arduino Uno.

When I hook up the HC-12 to my Arduino Pro Mini for transmission I am able to see that the data is properly being printed on the serial monitor on the transmitter side.

However, on the receiver end the Arduino Uno doesn't get data. The serial monitor shows a blank screen.

My guess is it's not a program issue at this point now but probably
an insufficient power provided to the HC-12 issue. Not 100% sure.
The Arduino Pro Mini is the 3.3V, 8 Mhz version.

I got the completed code for anyone that may need to refer to it or use it written below.

Here is what data looks like on the transmission end:

Code: [Select]

<380,148,224,260,1196,79>


Here is what successful reception of data looks like (between two Arduino Uno's):

Code: [Select]

<732,180,264,290,1272,82>
AZIMUTH MOTOR MOVES COUNTERCLOCKWISE
   
ELEVATION MOTOR MOVES CLOCKWISE



TRANSMITTER 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() {
  HC12.begin(9600); //SERIAL PORT TO HC12
}

void loop() {
  //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

  HC12.print("<"); //SENDS ANALOG VALUES IN THIS FORMAT: I.E. <380,148,224,260,1196,79>
  HC12.print(TR);
  HC12.print(",");
  HC12.print(BR);
  HC12.print(",");
  HC12.print(TL);
  HC12.print(",");
  HC12.print(BL);
  HC12.print(",");
  HC12.print(DT);
  HC12.print(",");
  HC12.print(TE);
  HC12.print(">");
  HC12.println();

  delay(DT);
}




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

// 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

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // TEMPORARY ARRAY FOR USE WHEN PARSING

// VARIABLES TO HOLD THE PARSED DATA
int TR = 0; //TOP RIGHT SENSOR
int BR = 0; //BOTTOM RIGHT SENSOR
int TL = 0; //TOP LEFT SENSOR
int BL = 0; //BOTTOM LEFT SENSOR
int DT = 0; //CONTROL DELAY TIME BETWEEN READINGS OF SENSORS
int TE = 0; //SET TOLERANCE VALUE FOR DIFFERENCE BETWEEN SENSORS

boolean newData = false;

//====================

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() {
  recvWithStartEndMarkers();
  if (newData == true) {
    strcpy(tempChars, receivedChars);
    // THIS TEMPORARY COPY IS NECESSARY TO PROTECT THE ORIGINAL DATA
    // BECAUSE STRTOK() USED IN PARSEDATA() REPLACES THE COMMAS WITH \0
    parseData();
    showParsedData();
    newData = false;
  }

  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);
}

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (HC12.available() > 0 && newData == false) {
    rc = HC12.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // TERMINATE THE STRING
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}

//===================

void parseData() {   // SPLIT THE DATA INTO ITS PARTS

  char * strtokIndx;  // THIS IS USED BY STRTOK() AS AN INDEX

  strtokIndx = strtok(tempChars, ",");     // GET THE FIRST PART - THE STRING
  TR = atoi(strtokIndx);      // CONVERT THIS PART TO AN INTEGER

  strtokIndx = strtok(NULL, ","); // THIS CONTINUES WHERE THE PREVIOUS CALL LEFT OFF
  BR = atoi(strtokIndx);

  strtokIndx = strtok(NULL, ","); //
  TL = atoi(strtokIndx);

  strtokIndx = strtok(NULL, ","); //
  BL = atoi(strtokIndx);

  strtokIndx = strtok(NULL, ","); //
  DT = atoi(strtokIndx);

  strtokIndx = strtok(NULL, ","); //
  TE = atoi(strtokIndx);
}

//======================

void showParsedData() {  // SHOWS PARSED DATA IN SERIAL MONITOR OUTPUT OF PC
  Serial.print("<");
  Serial.print(TR);
  Serial.print(",");
  Serial.print(BR);
  Serial.print(",");
  Serial.print(TL);
  Serial.print(",");
  Serial.print(BL);
  Serial.print(",");
  Serial.print(DT);
  Serial.print(",");
  Serial.print(TE);
  Serial.print(">");
  Serial.println();
}
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: Robin2 on Sep 09, 2017, 12:10 pm
When you have a wireless problem simplify simplify.

Try these
Code: [Select]
// TX
#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() {
  HC12.begin(9600); //SERIAL PORT TO HC12
}

void loop() {
  //LIGHT SENSORS CONNNECTED TO ANALOG PIN ON ARDUINO BOARD
  static int TR = 24;


  HC12.print("<"); //SENDS ANALOG VALUES IN THIS FORMAT: I.E. <380,148,224,260,1196,79>
  HC12.print(TR);
  HC12.print(">");
  HC12.println();

  delay(500);
  TR += 1;
}



and
Code: [Select]
// RX
#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

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // TEMPORARY ARRAY FOR USE WHEN PARSING

boolean newData = false;

//====================

void setup() {
  Serial.begin(9600); //SERIAL PORT TO PC
  HC12.begin(9600); //SERIAL PORT TO HC12

}

void loop() {
  recvWithStartEndMarkers();
  if (newData == true) {
    Serial.println(receivedChars);
    newData = false;
  }
}

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (HC12.available() > 0 && newData == false) {
    rc = HC12.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // TERMINATE THE STRING
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}


Note that I have not tested them - but they are unlikely to be too far wrong.

By the way I notice that you have a delay() in loop() in your Rx code - that's a complete NO NO because it could mean that the Rx misses something. Do the timing on the Tx side only.

...R
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: knightridar on Sep 11, 2017, 01:18 am
I removed the delay time (DT) variable from the receiver code.

Information is easily being transferred from one Arduino Uno to another Arduino Uno.
I then put the transmitter code in the Arduino Pro Mini (3.3 V, 8 Mhz) and
the receiver code into the Arduino Uno (5V, 16 Mhz).
This time around there is no delay time (DT) in the void loop of the code.
Still no transfer of information.

I wanted to see if this was an issue between different models of Arduinos.
So I have the transmitter Arduino Pro Mini (3.3V, 8 Mhz) hooked up outside and transmitting data.
I then uploaded the receiver code to another Arduino Pro Mini (3.3V, 8 Mhz) that's hooked up to my PC and I am monitoring the data through the serial monitor and it works!

Really weird  that two different Arduino models can't talk to each other via the HC-12 modules, but if they are the same model they can talk to each other.
When they are the same models the HC-12 modules are working!
A mystery to me but maybe useful for others if they run into similar issues.

Thanks again everyone!

Here are pictures below:










Here is some sample data from the Receiver Arduino Pro Mini:

Code: [Select]


<964,891,854,972,4056,106>
AZIMUTH MOTOR STOPS
   
ELEVATION MOTOR STOPS
   
AZIMUTH MOTOR STOPS
   
ELEVATION MOTOR STOPS
   
AZIMUTH MOTOR STOPS
   
ELEVATION MOTOR STOPS
   
AZIMUTH MOTOR STOPS
   
ELEVATION MOTOR STOPS
   
AZIMUTH MOTOR STOPS


Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: Robin2 on Sep 11, 2017, 09:09 am
Really weird  that two different Arduino models can't talk to each other via the HC-12 modules, but if they are the same model they can talk to each other.
That makes me wonder how you are powering the HC12.

If you are drawing power from the Arduino's 3.3v pin that could be the problem - they cannot really supply enough current. Try putting a 10µF capacitor across Vcc and GND on the HC12.

Or, better still, try using a separate 3,3v power supply with a common GND to the Arduino. You could probably use a pair of AA alkaline cells (3v) for testing.

...R
Title: [SOLVED] HC-12 TRANSMITTER/HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: knightridar on Sep 12, 2017, 06:32 am
When I tried using the Arduino Pro mini and sending data to the Arduino Uno,
I tried the method suggested on the site below:

https://www.allaboutcircuits.com/projects/understanding-and-implementing-the-hc-12-wireless-transceiver-module/

I used the recommended range capacitor and diode and I used a separate power supply.
It didn't work. Data transmission was still blank on the receiver end. Weird.

Setups that are working: (Arduino Pro Mini to Arduino Pro Mini or Arduino Uno to Arduino Uno)

Current setup:Arduino Pro Mini to Arduino Pro Mini

Right now I am powering the transmitter end via the Vcc pin with a 5V, 200 mA solar panel.
On a good sunny day the voltage can get up to 5.2~5.4 volts but that's fine since I read the Arduino Pro mini can handle up to 12 volts input.

For the receiver end I am just currently powering it via the 5V usb to ftdi adapter with the jumper set to 3.3v settings.


Transmitting data successfully with devices about 15-20 meters apart.
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: elavarasan7092 on Jan 29, 2019, 05:15 am
Can I use other pins
Title: Re: HC-12 TRANSMITTER TO HC-12 RECEIVER, CONVERT ANALOG VALUES BACK TO INTEGER
Post by: PaulS on Jan 29, 2019, 02:14 pm
Can I use other pins
For what?