zurück
Master
/*
* RS485 with MAX485 for control servo
*/
#include <SoftwareSerial.h>
#define SSerialRX 10 // Serial Receive pin
#define SSerialTX 11 // Serial Tansmit pin
#define SSerialTxControl 3 // RS485 Deriection Control
#define RS485Transmit HIGH
#define RS485Receive LOW
/*
* Declare objects
*/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX);
/*
* Declare Variables
*/
int byteReceived;
int deg = 0;
void setup()
{
pinMode(Pin13LED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Transmit); // Init Tranceiver - receive
RS485Serial.begin(4800); // set data rate on the serial port
}
void loop()
{
RS485Serial.write(deg);
delay(250);
deg += 2;
if (deg >= 180)
{
deg = 0;
}
}
Slave
#include <SoftwareSerial.h>
#include <Servo.h>
#define SSerialRX 10 // Serial Receive pin
#define SSerialTX 11 // Serial Tansmit pin
#define SSerialTxControl 3 // RS485 Deriection Control
#define RS485Transmit HIGH
#define RS485Receive LOW
#define Pin13LED 13
SoftwareSerial RS485Serial(SSerialRX, SSerialTX);
Servo myservo;
int angle = 0;
int byteReceived;
void setup()
{
myservo.attach(9, 450, 2100);
pinMode(Pin13LED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Receive); // Init Tranceiver - receive
RS485Serial.begin(4800); // set data rate on the serial port
Serial.begin(9600);
}
void loop()
{
digitalWrite(SSerialTxControl, RS485Receive);
if (RS485Serial.available())
{
//digitalWrite(Pin13LED, HIGH);
byteReceived = RS485Serial.read();
angle = byteReceived;
Serial.println(angle);
}
myservo.write(angle);
delay(10);
}
zurück