Go Down

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

knightridar

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.

jremington

You shouldn't be using pins 0 and 1 for Software serial, they are for program uploading and hardware serial.

See Serial Input Basics 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.

knightridar

Attached is a screenshot of the successful transmission using the first version of the receiving code.


knightridar

@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.

jremington

Quote
Since I'm not too code savvy
You will learn quickly by following the examples in Serial Input Basics.

knightridar

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?


Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

edgemoron

Code: [Select]
while (Serial.available())
Try:
Code: [Select]
while (Serial.available() > 0)

PaulS

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.
The art of getting good answers lies in asking good questions.

knightridar

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





Robin2

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 - char arrays terminated with 0.

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

Robin2

Images from Reply #9 so we don't have to download them. See this  Image Guide





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

Robin2

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 - simple reliable ways to receive data.

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

PaulS

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.
The art of getting good answers lies in asking good questions.

knightridar

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


Go Up