note: i’m using 433 or 315 MHz transmitters, it will be different for Bluetooth or wifi…
tx:
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
RH_ASK driver(5000, 0, 1 );
int potPin = A3;
int potPosition = 0;
int potPosition1 = 0;
void setup() {
Serial.begin(9600);
if (!driver.init())
Serial.println(“init failed”);
}
void loop() {
potPosition1 = analogRead(potPin);
//potPosition = map(potPosition1, 245, 635, 0, 255);
potPosition = map(potPosition1, 0, 640, 0, 255);
uint8_t values[1];
values[0]=(uint8_t) potPosition;
Serial.print("PP: ");
Serial.print(potPosition);
Serial.print("PP1: ");
Serial.println(potPosition1);
driver.send(values, 1);
driver.waitPacketSent();
delay(2);
}
RX:
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
#include <SoftwareServo.h>
RH_ASK driver(5000, 0, 1 );
SoftwareServo esc;
int SERVO_LOW = 0;
int SERVO_HIGH = 180;
int c1,c2,c3;
int v1=0;
int vs=0;
int vn=0;
unsigned long lastM;
unsigned long currentT;
void setup() {
esc.attach(3);
Serial.begin(9600);
if (!driver.init())
Serial.println(“init failed”);
}
void loop() {
uint8_t buf[4];
uint8_t buflen = sizeof(buf);
esc.refresh();
if (driver.recv(buf, &buflen))
{
c1 = buf[0];
lastM = millis();
v1 = map(c1, 0, 255, SERVO_LOW, SERVO_HIGH);
}
vs = esc.read();
if( v1-vs > 2 || v1-vs<-2)
{
vn = vs + 0.5*(v1-vs);
esc.write(vn);
esc.refresh();
}
delay(50);
currentT = millis();
if((unsigned long)(currentT-lastM) > 1000 && vs!= 0)
{
esc.write(0);
esc.refresh();
v1=0;
vn=0;
}
Serial.print(" v1:");
Serial.print(v1);
Serial.print(" vs:");
Serial.print(vs);
Serial.print(" vn:");
Serial.print(vn);
Serial.print(" currentT:");
Serial.print(currentT);
Serial.print(" lastM:");
Serial.println(lastM);
Serial.print(" c1:");
Serial.println(c1);
}
the TX is straight forward: with the scaling explained above
the RX is messy…
the part with VS was an attempt a removing servo jitter (i was testing with a servo to avoid issues with the ESC…
the part with currentT is the failsafe… it counts the time since last signal received, and if it’s more than 1 second, it puts the ESC to 0…
all the debugging serial print are still in… i