I recently made an electric skateboard using hc-12 for the comunication but if i fall off the board and it gets too far away the signal dosent get received from the skateboard and I cant stop it. I want to add something to the code that if there is nothing received to turn off the motor.
Thanks in advance.
Transmitter code:
#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
}
}
}
Receiver code:
#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);
}
}
The best way to make a failsafe system is to send a message at regular intervals - perhaps 5 per second. Then if there is a long gap between messages the receiver will know something is wrong and can shut down the motor.
You don't say how the transmitter is controlled. Perhaps it has a switch that is normally held closed by your hand and if you let go it will stop transmitting.
Have you tried modifying the code to get your results?
Your transmitter code looks like it only writes to the HC12 if the trigger value changes. I would change it to write every time through the loop. Or maybe each time through the loop that the value has changed OR a certain time period has elapsed. Maybe that time period can be 1/5 of a second as Robin2 suggests.
Then change your receiver code. If more than your time period has elapsed since the last packet has been received, set the throttle value to zero whatever value means stop and write.
I do not know how accurate HC12 communication is. Does it drop packets? You might go 2 or 3 times the normal frame rate to account for missed packets.
Robin2:
The best way to make a failsafe system is to send a message at regular intervals - perhaps 5 per second. Then if there is a long gap between messages the receiver will know something is wrong and can shut down the motor.
You don't say how the transmitter is controlled. Perhaps it has a switch that is normally held closed by your hand and if you let go it will stop transmitting.
...R
Ok Il try this method out, but how do I code it to check if the specific message is received?
the transmitter is controlled with a potentiometer which doesn't go back to zero if I let go.
vinceherman:
Cool project and congrats on getting it to work.
Have you tried modifying the code to get your results?
Your transmitter code looks like it only writes to the HC12 if the trigger value changes. I would change it to write every time through the loop. Or maybe each time through the loop that the value has changed OR a certain time period has elapsed. Maybe that time period can be 1/5 of a second as Robin2 suggests.
Then change your receiver code. If more than your time period has elapsed since the last packet has been received, set the throttle value to zero whatever value means stop and write.
I do not know how accurate HC12 communication is. Does it drop packets? You might go 2 or 3 times the normal frame rate to account for missed packets.
First of all many thanks
second, the problem with sending in each loop the motor was super rough and gittery so I thought that was the best way to smooth out what's being sent.
mousse_raafat:
Ok Il try this method out, but how do I code it to check if the specific message is received?
the transmitter is controlled with a potentiometer which doesn't go back to zero if I let go.
Let's start with the second part. It's essential to have some system so that the transmitter "knows" whether you are holding or have let go. Maybe that means adding an extra pushbutton or microswitch of the type you have to hold down.
Assuming you have upgraded the transmitter so it can stop sending when you let go then when the receiver gets a message it should save the value of millis() to a variable - let's call it timeLastMessageReceived. Then every time through loop() it must check if it was too long since the value was last saved - something like this
if (millis() - timeLastMessageReceived >= interval) {
// too long
// code to stop the motor
}
Robin2, I do not think that the transmitter needs to know when you have let go and if so, stop sending data.
I do think that it needs to be modified to always send data within a certain time period (1/5 second that you suggested would be fine)
The receiver will quickly stop receiving data when the rider falls off and the board travels out of range of the transmitter.
The new receiver code (again as you suggest) will see this loss of data as a reason to shut off the motor, making it possible for the rider to retrieve to board.
vinceherman:
The receiver will quickly stop receiving data when the rider falls off and the board travels out of range of the transmitter.
I had considered that but it seemed to me that relying on range to stop the thing could result in it running into several children while still powered but not under control. The effective wireless range is very imprecise.
While the rider is still within range, setting the throttle to zero should stop the board.
I assumed (perhaps incorrectly) that the issue was that in an off, the rider did not set the throttle to zero before the board was out of range and it ran away.
I guess I should ask about the transmitter hardware. All of my radio controlled wheeled vehicles have the throttle as a trigger that is spring loaded to return to zero throttle of you let go.
mousse_raafat, can you tell us how your transmitter hardware is set up?
vinceherman:
I assumed (perhaps incorrectly) that the issue was that in an off, the rider did not set the throttle to zero before the board was out of range and it ran away.
That's a reasonable assumption.
I have been suggesting the use of a separate "dead-man" switch but a spring loaded throttle would have the same effect.
Robin2:
The best way to make a failsafe system is to send a message at regular intervals - perhaps 5 per second. Then if there is a long gap between messages the receiver will know something is wrong and can shut down the motor.
You don't say how the transmitter is controlled. Perhaps it has a switch that is normally held closed by your hand and if you let go it will stop transmitting.
...R
Surely that's the only way to be failsafe with wireless control? Modern quadcopters/drones switch to
auto-home if the control packets stop arriving.
You need to switch to a safe deceleration curve too, not just brake abruptly, in case the transmitter has
died - the rider needs to keep their balance if this happens.
You should also be treating packets with incorrect checksum as if they were not received,
as this can be a symptom of inadvertent jamming/corruption of control data. In other words
if you are not using a good checksum you need it.
vinceherman:
While the rider is still within range, setting the throttle to zero should stop the board.
I assumed (perhaps incorrectly) that the issue was that in an off, the rider did not set the throttle to zero before the board was out of range and it ran away.
I guess I should ask about the transmitter hardware. All of my radio controlled wheeled vehicles have the throttle as a trigger that is spring loaded to return to zero throttle of you let go.
mousse_raafat, can you tell us how your transmitter hardware is set up?
My transmitter dosent automaticlly turn back to zero if I let it go, but I think Il add a spring or something so that it does. But another problem is that if the transmitter dies the board will remain on the last value sent, it will not shut off.
mousse_raafat:
My transmitter doesn't automatically turn back to zero if I let it go, but I think Il add a spring or something so that it does.
I would love to see pics of what you do have. If I were tackling something like this, I think I would take an old RC Car transmitter and put my transmitter electronics in it. The trigger throttle is very functional.
But another problem is that if the transmitter dies the board will remain on the last value sent, it will not shut off.
But this will be addressed with the changes discussed above. A. Transmitter sends regularly. B. Receiver sets throttle to zero if elapsed time between packets is too long.
Have you played with that code yet?
MarkT:
Surely that's the only way to be failsafe with wireless control? Modern quadcopters/drones switch to
auto-home if the control packets stop arriving.
You need to switch to a safe deceleration curve too, not just brake abruptly, in case the transmitter has
died - the rider needs to keep their balance if this happens.
You should also be treating packets with incorrect checksum as if they were not received,
as this can be a symptom of inadvertent jamming/corruption of control data. In other words
if you are not using a good checksum you need it.
For the deceleration part its as simple as just shutting off the motor, it doesn't brake so its fine. how do I use a checksum for the packets, or even just for the data being sent (Im still relatively new to arduino so excuse me). the data does get corrupted from time to time but thats not a real issue for me, I just want to shut the motor off if there is nothing received.
mousse_raafat:
For the deceleration part its as simple as just shutting off the motor, it doesn't brake so its fine. how do I use a checksum for the packets, or even just for the data being sent (Im still relatively new to arduino so excuse me). the data does get corrupted from time to time but thats not a real issue for me, I just want to shut the motor off if there is nothing received.
I would put the idea of the checksum aside for the moment and get your system working as I suggested in Replies #1 and #5.
The idea of the checksum is to enable the receiver to ignore erroneous data - for example it might receive 200 when it should have been 87 which would cause the motor to accelerate when it should not. However if messages are sent regularly (as I suggested) the occasional erroneous value is less likely to matter because the next value will correct things.
vinceherman:
I would love to see pics of what you do have. If I were tackling something like this, I think I would take an old RC Car transmitter and put my transmitter electronics in it. The trigger throttle is very functional.
But this will be addressed with the changes discussed above. A. Transmitter sends regularly. B. Receiver sets throttle to zero if elapsed time between packets is too long.
Have you played with that code yet?
I dont really know how to tackle that part yet. As I said im kinda new to arduino so I dont know how to handle data being sent and received very well. Can u give me an example of something like that?
mousse_raafat:
I dont really know how to tackle that part yet. As I said im kinda new to arduino so I dont know how to handle data being sent and received very well.
Write a short program that sends (say) "Hello World" once per second and a separate program that receives the message and displays it on the Serial Monitor.
When you have done that post your two programs and we can take it from there.