Hello, I am building an electric skateboard using a BLDC motor and two HC-12 for the wireless control. The problem is that when I start sending values from the potentiometer the motor starts spinning very roughly. I think the values being sent are unstable making it hard for the motor to spin smoothly, if so, is there a way I can smooth out the values being sent?
here is the code for the transmitter:
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
int pot = A2;
void setup() {
Serial.begin(9600); // Serial port to computer
HC12.begin(9600); // Serial port to HC12
pinMode(pot,INPUT);
}
void loop() {
int val = map(analogRead(pot),0,1024,0,180);
HC12.write(val); // Send that data to HC-12
Serial.println(val); a
}
And here is the code for the receiver:
#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
int LED = 3;
Servo myservo;
void setup() {
Serial.begin(9600); // Serial port to computer
HC12.begin(9600); // Serial port to HC12
myservo.attach(9, 1000, 2000);;
}
void loop() {
while (HC12.available()) { // If HC-12 has data
int val = HC12.read();
Serial.println(HC12.read()); // Send the data to Serial monitor
myservo.write(val);
}
}
jremington:
Are the values that arrive at the receiver what you expect?
Why does the receiver read two values?
The values increase as I turn the knob but they keep oscillating and changing but staying close to the expected value. The receiver is only reading it once and then printing it on the serial monitor for troubleshooting.
Not sure why you have used a radio link with a 1km range to control a skateboard :o
Could reduce transmit power in setup(). See HC-12 datasheet for the AT commands.
You seem to write the pot value to the transmitter continuously.
Both seem hard on transmitter and battery life.
The attached transmitter code only sends when the pot value changes (not tested with HC-12).
I also added a safety feature.
Leo..
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
const byte potPin = A2;
int potValue = 0;
int previousPotValue; // deadband
byte servoValue = 0;
byte previousServoValue; // write once
void setup() {
Serial.begin(9600);
Serial.print("Servovalue: ");
Serial.print(servoValue);
Serial.println("\tThrottle down to start...");
HC12.begin(9600);
HC12.write(servoValue); // write 0
while (analogRead(potPin) > 0); // wait
}
void loop() {
potValue = analogRead(potPin);
if (potValue < (previousPotValue - 2) or potValue > (previousPotValue + 2)) { // some deadband
previousPotValue = potValue; // update
servoValue = map(previousPotValue, 0, 1020, 0, 180); // cut pot extreme
if (previousServoValue != servoValue) { // only print changes
HC12.write(servoValue);
Serial.print("Servovalue: ");
Serial.println(servoValue);
previousServoValue = servoValue; // update
}
}
}
Wawa:
Not sure why you have used a radio link with a 1km range to control a skateboard :o
Could reduce transmit power in setup(). See HC-12 datasheet for the AT commands.
You seem to write the pot value to the transmitter continuously.
Both seem hard on transmitter and battery life.
The attached transmitter code only sends when the pot value changes (not tested with HC-12).
I also added a safety feature.
Leo..
For safety its essential to constantly transmit the settings, and have the receiver cut out the motor if incoming
messages are lost for more than some time-threshold. Runaways are never good... That long range is also bad news for this reason.