HC-12 Servo help needed.

Hello,
Thanks in advance. My setup is as follows:

2 Arduino Mega 2560’s
2 HC-12 modules
2 9g servos

I need the data being written to MyServo2 to be only the incoming data ranging from 180-360 then subtracting 180 from that data and writing it to that said servo. This is what I came up with and it is not filtering the data properly When it goes to write to either servo.

Slave

#include <SoftwareSerial.h>
#include <Servo.h>
Servo MyServo;
Servo MyServo2;

SoftwareSerial MySerial(2, 3); // RX, TX

int input = 0;
int input2 = 0;

void setup() 
{
  MySerial.begin(9600);
  MyServo.attach(11);
  MyServo2.attach(10);
   
}

void loop() 
{   
   
   if(MySerial.available() < 180)   
{  
    int input = MySerial.parseInt();
      MyServo.write(input);      
      delay(5);  
}

  if(MySerial.available() > 180)   
{      
    int input2 = MySerial.parseInt();      
      MyServo2.write(input2);  
      delay(5);  
}
}

Master

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#include <Servo.h>

Servo myservo;

LiquidCrystal_I2C lcd(0x3F,20,4); 
SoftwareSerial mySerial(2,3); // 2 rx  

int AnalogInputPin = 11;
int AnalogInputPin2 = 12;
int OutputValue = 0;
int OutputValue2 = 0;
 
void setup()

{
  mySerial.begin(9600);
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("Jello, world!");
  lcd.setCursor(0,1);
  lcd.print("HArduino!"); 
 
}

void loop()
{
  
{      
  int AnalogInputValue = analogRead(AnalogInputPin);
  OutputValue = map(AnalogInputValue, 0, 1023, 0, 180);
  mySerial.println(OutputValue);
  Serial.println(OutputValue);
  delay(230);
} 

{      
  int AnalogInputValue2 = analogRead(AnalogInputPin2);
  OutputValue2 = map(AnalogInputValue2, 0, 1023, 180, 360);
  mySerial.println(OutputValue2);
  Serial.println(OutputValue2);
  delay(230);
}
 
}
if(MySerial.available() < 180)

Zero is less than 180, and the serial buffer is only 64bytes long, so available is never going to be be greater than 180.

Edit: and don’t crosspost. I hates crossposting and crossposters.

I was told to repost. So your saying the slave side serial buffer can only go from 0-180?

No, I'm saying the serial buffer can only hold up to 64 characters, so the available method() can only return a value less than 64.
Mostly, it'll return zero, assuming at some point you remove the contents correctly.
What those characters in the buffer represent is a different matter :wink:

I see. Sounds like I need a new method. Any suggestions?

You could see if there is anything there to read, and then read it.
Robin2 has an excellent serial basics thread

Hi,
Does softwareserial library have any examples?
Look in Files, Examples.

Tom.. :slight_smile:

great help there ya thanks.

The thread title doesn't really reflect the subject - can you change it please?

@jgreene33, do not cross-post. Replies go here...