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
// 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
//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?):
// 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”
Send data as : “BL”
Send data as : “TR”
Send data as : “BR”
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.