So here is another shot at creating a realistic brake light effect on an RC car. It is based on the reading of the throttle- channel and detects the start of a braking, then the idea is to keep the light on for, let's say 2 secs, then turn it off again automatically.
Can you post your code that you have so far?
The idea that I find convincing is, that there is a bool PreFW that detects if you are coming out of a forward movement, so that the brakelight isn't active when the throttle is on neutral. It is supposed to work as a "button" - if it is pressed, the whole braking action can take place, if it isn't, the light will remain tuned off.
This is my code so far, based on a lot of different sources from the web and quite a bit of studying, but without really having a clue why it doesn't work for me... mus have to dowith the millis- function and the timestamp, but I am quite at a loss. Please help!!!
int GasPin = 2;
int LEDPin = 3;
// Timer- Variablen
unsigned long stoplightTime = 1000;
unsigned long stoplightTimer = 0;
unsigned long systemTime;
// Flags zum Erkennen von Zuständen
bool preFW = false;
bool FW;
bool OFF;
bool BW;
bool Bremslicht;
// Grenzwerte zum Erkennen von Zuständen
const int FWon = 2;
const int BWon = -2;
void setup() {
pinMode (GasPin, INPUT);
pinMode (LEDPin, OUTPUT);
Serial.begin(115200);
systemTime = millis();
}
void loop() {
int Gas = pulseIn(GasPin, HIGH);
Gas = map(Gas, 994, 1987, -100, 100);
Serial.print("Gas___");
Serial.print(Gas);
Serial.println();
if (Gas > FWon) preFW = true;
// else preFW = false; // Flag setzen für Vowärtsfahrt}
if (preFW == true && Gas < FWon) {
stoplightON();
stoplightTimer = millis();}
if (systemTime > stoplightTimer + stoplightTime){
stoplightOFF();
preFW = false;}
} ///////////////////////////////////////////////////////////////////////////////////////////////////////////// Ende der Void Loop
void stoplightON(){
digitalWrite(LEDPin, HIGH);}
void stoplightOFF(){
digitalWrite (LEDPin, LOW);}
Wow, you're quick -I was still typing...
You are not updating systemTime in loop().
Also, this line of code:
if (systemTime > stoplightTimer + stoplightTime){
should be:
if (systemTime - stoplightTimer > stoplightTime){
Electrically driven car or otherwise?
If electrically driven, brushed or brushless motor?
Hi bluejets, it is going to be for electric driven cars with either brushed or brushless motors, but I don't think it changes anything for that particular problem - I only want to grab the throttle channel signal, which controls forward movement, braking and backward movement. Right now I am working on making the brakelight go off 2 sec after being triggered. Later on, when my skills will hopefully have evolved, I am planning on adding further stuff, like having it go off earlier, when back in forward movement or adding a reverse drive light that would only work when the car is actually moving backwards (which is going to be tricky, as in most escs, one backward throttle input means braking, the second one means the motor starts turning backwards) - but that is for months to come...
Hey guys, so I've implemented your suggestions and I get some LED action sometimes, which is already a massive improvement. However, it seems like my "button" (the preFW bool) doesn't quite work as I imagined it would: it is being turned on when the throttle value goes up and stays on until, yeah, eternity I suppose. The idea was to switch it off after the designated brake light time, but that doesn't work - why actually? If I don't find a way to switch the preFW off, the condition (preFW && Gas < FWon) is always true excep when driving forward, which is exactly what it was supposed to avoid in the first place. D'oh!!! Here's what I got so far:
int GasPin = 2;
int LEDPin = 3;
// Timer- Variablen
unsigned long stoplightTime = 1000;
unsigned long stoplightTimer = 0;
// Flags to detect status
bool preFW = false;
bool FW;
bool OFF;
bool BW;
bool brakelight;
// Definition of forward/ backward movement
const int FWon = 2;
const int BWon = -2;
void setup() {
pinMode (GasPin, INPUT);
pinMode (LEDPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
int Gas = pulseIn(GasPin, HIGH);
Gas = map(Gas, 994, 1987, -100, 100);
Serial.print("Gas___");
Serial.print(Gas);
Serial.println();
if (Gas > FWon) preFW = true; // Flag: forward motion (= press button to enable braking function)
Serial.print(preFW);
if (Gas >BWon && Gas <FWon)OFF = true; else OFF = false; // Flag: motor stopped
if (preFW && Gas < FWon) {
stoplightON();
stoplightTimer = millis();} // turn on brake light when coming out of forward movement
if (millis() - stoplightTimer > stoplightTime) {
stoplightOFF(); // turn off brake light after specified time
preFW = false;} // reset button
} ///////////////////////////////////////////////////////////////////////////////////////////////////////////// Ende der Void Loop
void stoplightON(){
digitalWrite(LEDPin, HIGH);}
void stoplightOFF(){
digitalWrite (LEDPin, LOW);}
I believe you should set preFW to false whenever you turn the brake light on. That way you don't continually turn it back on. Like this:
if (preFW && Gas < FWon) {
stoplightON();
stoplightTimer = millis();
preFW = false;
} // turn on brake light when coming out of forward movement
if (millis() - stoplightTimer > stoplightTime) {
stoplightOFF(); // turn off brake light after specified time
}
Well that's where you are wrong.
Brake lights can be driven without any microcontroller.
The way it used to be done was use the regenerative characteristic of an electric motor under braking.
Hi bluejets, I agree, this is certainly a more elegant way of doing it, but the thing is, I wanted to have one system that does everything - but I have to concede, that it isn't as easy as I thought. So thanks for the consideration, I'll definitely look into that - any tips where I might start? I imagine it is simpler with brushed motors?
Thanks! This is one of the few places where I haven't tried that line yet...
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.