hello,
it is my first time coding on wireless communication and I decided to write the code for it before I purchase it, but doing this means I cant troubleshoot it. I am building a servo arm that I can control wirelessly with several potentiometers
i would like to know how to send all of the analog signals to the radio receiver and be able to interpret each one individually (so potentiometers will only control servo1) , I have watched tutorials on how the HC-12 works and it says it only send bytes of data , I would need to turn those bytes into a char and then into a string but I get error messages.
this is the code for the transmitter
#include <SoftwareSerial.h>
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //i2c pins
//define potentiometers only used for memory mode
const int pot1 = A1;
const int pot2 = A2;
const int pot3 = A3;
const int pot4 = A4;
//define variable for values of the potentiometers
int pot1Val;
int pot2Val;
int pot3Val;
int pot4Val;
//define variable for angles of the potentiometer
int pot1Angle;
int pot2Angle;
int pot3Angle;
int pot4Angle;
//define variable for saved position of the servos
int servo1PosSave[]={1,1,1,1,1,1,1,1};
int servo2PosSave[]={1,1,1,1,1,1,1,1};
int servo3PosSave[]={1,1,1,1,1,1,1,1};
int servo4PosSave[]={1,1,1,1,1,1,1,1};
//the signals that are going to be transmite wirelessly
char sig1[];
char sig2[];
char sig3[];
char sig4[];
void setup() {
HC12.begin(9600); // Serial port to HC12
lcd.begin(16,2); //WE define our LCD 16 columns and 2 rows
lcd.backlight(); //Power on the back light
lcd.backlight(); //Power off the back light
//defining outputs and inputs
// pinMode(soundSensor, INPUT);
// pinMode(memoryButton, INPUT);
// pinMode(replayButton, INPUT);
//establish communication with the user
lcd.setCursor(0,0);
lcd.print("B.M.O is online");
delay(1000);
lcd.clear();
}
void loop() {
//----------------------------------------------------------------------------------------------------------------------------//
// Manual Control Mode //
//----------------------------------------------------------------------------------------------------------------------------//
//read the potentiometer values and define the servo angle to
//the potentiometer value with the map function
pot1Val = analogRead(pot1);
pot1Angle = map (pot1Val, 0, 1023, 10, 180);
pot2Val = analogRead(pot2);
pot2Angle = map (pot2Val, 0, 1023, 10, 180);
pot3Val = analogRead(pot3);
pot3Angle = map (pot3Val, 0, 1023, 10, 180);
pot4Val = analogRead(pot4);
pot4Angle = map (pot4Val, 0, 1023, 10, 180);
//creating the signals that will be transmitted
sig1[] = "A" + pot1Angle;
sig2[] = "B" + pot2Angle;
sig3[] = "C" + pot3Angle;
sig4[] = "D" + pot4Angle;
//servos move to mapped angles
HC12.write(sig1[]);
HC12.write(sig2[]);
HC12.write(sig3[]);
HC12.write(sig4[]);
and this is the code for the receiver
#include <Servo.h>
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
byte incomsig;
char readbuffer[] = "";
//define the servos
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
//define variable for values of the button
int button1Pressed = 0;
boolean button2Pressed = false;
//define variable for saved position of the servos
int servo1PosSave[]={1,1,1,1,1,1,1,1};
int servo2PosSave[]={1,1,1,1,1,1,1,1};
int servo3PosSave[]={1,1,1,1,1,1,1,1};
int servo4PosSave[]={1,1,1,1,1,1,1,1};
void setup() {
//define attached pins of the servos
servo1.attach(3);
servo2.attach(4);
servo3.attach(5);
servo4.attach(6);
servo1.write(90);
servo2.write(90);
servo3.write(90);
servo4.write(90);
HC12.begin(9600); // Serial port to HC12
}
void loop() {
while(HC12.available()) { // If HC-12 has data
//----------------------------------------------------------------------------------------------------------------------------//
// Manual Control Mode //
//----------------------------------------------------------------------------------------------------------------------------//
incomsig = HC12.read();
readbuffer += char(incomsig);
}
if(readbuffer[0] == "A"){
readbuffer[] = readbuffer[] - readbuffer[0];
servo1.write(readbuffer);
}
// servo2.write(pot2Angle);
// servo3.write(pot3Angle);
// servo4.write(pot4Angle);
it is not finished yet as I do not know where to go next with the code as soon I would like to implement a memory function so the robot arm can replay saved positions but I can't even begin to imagin how I would send all of that data wirelessly