The vcc and gnd pins mention is that i made my hc-12 to directly fit in my arduino.
Here is my transmitting code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11, 10); //RX, TX
int vcc=8;
int gnd=9;
pot1=A0; //pin the pot is connected to
pot2=A1;
pot1val; //values to store pots value
pot2val;
void setup() {
Serial.begin(9600);
mySerial.begin(9600); //serial communication of hc12
pinMode(vcc,OUTPUT); //powering hc12with pin 8, 9
pinMode(gnd,OUTPUT);
pinMode(but1,INPUT);
pinMode(but2,INPUT);
}
void loop() {
digitalWrite(vcc,HIGH);
digitalWrite(gnd,LOW);
pot1val=analogRead(pot1);
pot2val=analogRead(pot2);
pot2val=pot2val+2000; //incresing its value to be differentiated
mySerial.println(pot1val);
mySerial.println(pot2val);
delay(200);
}
Here is my receiving code
#include <SoftwareSerial.h>
#include<Servo.h>
SoftwareSerial mySerial(11, 10); //RX, TX
int vcc=8;
int gnd=9;
int input;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
pinMode(vcc,OUTPUT);
pinMode(gnd,OUTPUT);
}
void loop() {
digitalWrite(vcc,HIGH);
digitalWrite(gnd,LOW);
input=mySerial.parseInt();
Serial.println(input);
if(input<1999){ //if input<2000 its the value from pot 1
Serial.print("pot1 ");
Serial.println(input);
}
else{
Serial.print("pot2 ");
Serial.println(input);
}
delay(20);
}
Receiving part is actually not complete
i used a basic technique where if the signal is from the pot2 its made higher and sent, so when received its differentiated.
But I think there is a better way of tackling this situation.
Send a message like this:
<123,334>
With 123 and 334 the values of the two pots. Easy to create, easy to decode.
How am I supposed to put this in a program ? Can you give a example ? Will it work with a hc-12?
Please give a example of using the above technique to send and decode it.
Thanks.