Arduino wireless

I am making a wireless robot with only two hands. It will be controlled wirelessly with two HC-12 modules.

Two potentiometer and a hc-12 and a arduino on the transmitting side.
Two servo motors and a HC-12 and a rduino on the receiving side.

My doubt is how will I send the positioning of both the potentiometer at the same time and how will it be differentiated in the receiving side.

I saw many examples but none could be modified to my use.
I would appreciate if I could get like a simple example program just to send two variables across the HC-12.

Thanks.

I assume you want to map the pot positions to the 'arm ' position? That is some position of the pot should correspond to some position of the 'hand'. This cannot be two variables since for each move you need.. And then can both hands move simultaneously?

  • which arm
  • which position

So exactly where are you stuck? How to map or how to send? How is hardware set up?

mugambi:
I assume you want to map the pot positions to the 'arm ' position? That is some position of the pot should correspond to some position of the 'hand'. This cannot be two variables since for each move you need.. And then can both hands move simultaneously?

  • which arm
  • which position

So exactly where are you stuck? How to map or how to send? How is hardware set up?

I am done with mapping the pot values and printing it on the serial monitor to check.

What I am stuck now it how to send both the position of the potentiometer simultaneously and be differentiated by the receiving Arduino.

I read the way rc's communicate is by sending the position of all inputs at the same time. So I am confused how you do that in a Arduino especially with a hc-12.

Thanks

You can post the transmit and receive code ..

Send a message like this:

<123,334>

With 123 and 334 the values of the two pots. Easy to create, easy to decode.

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.

Serial input basics.

HC-12 is no more than wireless Serial.

You will have problems with your hardware. The datasheet for an Hc-12 specifies minimum 200mA supply.
Arduino starts to fail after 40mA draw.

from Hc-12 datasheet:

The HC-12 module can be patch soldered, or have a 2.54mm-spacing pin header
attached and directly inserted onto the user’s PCB. The module has nine pins in
total, and one RF antenna socket (ANT1), with definitions as shown in the table
below:
Pin Definition I/O direction Notes
1 Vcc
Power supply input, DC3.2V-5.5V, with load
capacity not less than 200mA. Note: if the
module is working in the transmitting state
for an extended time, it is suggested that a
1N4007 diode be connected in series if the
supply voltage is greater than 4.5V, so as to
avoid overheating the onboard LDO regulator

From Arduino:

If you draw too much current (40mA or more) from an I/O pin, it will damage the pin. There are no fuses on the I/O pins

As for the passing of data packets wvmarie is correct the simplest way is to send a data pack with opening and closing characters that can be captured/identified as start and end of packets. such as suggested

mySerial.print('<');mySerial.print(potval1);.....mySerial.print('>');

This is a very simple version with some tweaks to your transmitter code. I commented where I made some changes.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(11, 10); //RX, TX

//int vcc = 8;   //this needs to go, i suggest at min wiring it to the actual vcc/gnd pins on you arduino
//int gnd = 9;

int pot1=A0;         //pin the pot is connected to
int pot2=A1;        // these also needed to change to specify a variable type ie; int

int pot1val;         //values to store pots value
int pot2val;         // as do these ones


void setup() {
  Serial.begin(9600);        
  mySerial.begin(9600);          //serial communication of hc12
  //pinMode(vcc,OUTPUT);                //powering hc12with pin 8, 9
  //pinMode(gnd,OUTPUT);          // again look at a different way to power this

  pinMode(pot1,INPUT);
  pinMode(pot2,INPUT);             // had to change these to the actual variable name
}



void loop() {
 // digitalWrite(vcc,HIGH);     // powering thing still wont work like this
 // digitalWrite(gnd,LOW);

pot1val=analogRead(pot1);
pot2val=analogRead(pot2);
// pot2val=pot2val+2000;           //we will use something different to identify the numbers

mySerial.print('*');                     // use single quotes '*' to signify start of data packet
mySerial.print('<');                    // use single quotes '<' to signify start of each data set
mySerial.print(pot1val);
mySerial.print('>');                    // use single quote '>' to signify end of data set
mySerial.print('<');                    // Start second data set
mySerial.print(pot2val);            // also removed the .println statement to simply .print
mySerial.print('>');                  // send '>' to signify end of second data set

delay(200);
}

To receive it properly will require the data to be evaluated a bit closer like this

#include <SoftwareSerial.h>
#include<Servo.h>
SoftwareSerial mySerial(11, 10); //RX, TX

int pot1Input;
int pot2Input;
int fieldIndex = 0;    // the current field being RECEIVED on serial
int index;                        // Variable for incoming array
char inData[10];                      // Variable for incoming messages

boolean started = false;   // State to determine if transmition is complete
boolean ended = false;


void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);

}


void loop() {

while (mySerial.available() > 0) {
   char achar = mySerial.read();
    if (achar == '*'){

      fieldIndex=0;
   
    } else if (achar == '<') {
      started=true;
      index =0;
      inData[index] = '\0';
    }  else if (achar == '>') {
      ended = true;
      fieldIndex++;
    } else if(started) {
      inData[index] = achar;
      index ++;
      inData[index] = '\0';
    }
}

if (started && ended){
    int inint = atoi(inData);
    
 if (fieldIndex==0){           // if it is the first number recieved it is pot 1
  pot1Input = inint;
} else {                       // otherwise it is pot 2
  pot2Input = inint;
}

}
Serial.print("POT 1 is ");Serial.println(pot1Input);
Serial.print("POT 2 is ");Serial.println(pot2Input);
}

TrevorWilson:
You will have problems with your hardware. The datasheet for an Hc-12 specifies minimum 200mA supply.
Arduino starts to fail after 40mA draw.

Then you're doing it wrong.

For starters, an Arduino output pin is NOT a source of power.

You normally simply power the HC-12 from the same 5V source that powers the Arduino.

Thanks everyone. The code and the different method of power supply was used. The communication worked well.

Thanks all.
:slight_smile:

wvmarle:
Then you're doing it wrong.

For starters, an Arduino output pin is NOT a source of power.

You normally simply power the HC-12 from the same 5V source that powers the Arduino.

Absolutely. I think that TrevorWilson has picked up on this in the OP's code:

PROBOT135:
The vcc and gnd pins mention is that i made my hc-12 to directly fit in my arduino.

Here is my transmitting code:

....

int vcc=8;
int gnd=9;
....

Just out of interest, if you derive the arm delta after receiving the two values, why not calculate that before sending ?