Ok so I am making a RC car lighting set for a custom made RC car!
I have both left and right blinkers working... aswell as
the reverse light BUT I can't get the braking light to work. it is reading my ESC's PWM and out putting a number.
and then its reading that number at the start of the cycle and the ending of the cycle.
then adding 3 to the first reading.... so that it can tell if I am really lowering it or not...
and then seeing if the first reading is higher than the second reading..
if it is it turns on the braking light! Simple!
But it keeps stuttering for no apparent reason!
double channellr;
double channelfb;
const int numReadingslr = 10;
const int numReadingsfb = 15;
const int ledPin = 4;// the number of the LED pin
const int ledPin2 = 5;// the number of the LED pin
const int ledPin3 = 6;// the number of the LED pin
const int ledPin4 = 7;// the number of the LED pin
const int brakinglight = 12;
const int brakinglight2 = 13;
const long interval = 500;
unsigned long previousMillis = 0;
int readingslr[numReadingslr];
int readingsfb[numReadingsfb]; // the readings from the analog input
int ledState = LOW;
int readIndexlr = 0;
int readIndexfb = 0; // the index of the current reading
int totallr = 0;
int totalfb = 0;// the running total
int ch4 = 0;
int ch3 = 0;
int lr = 1;
int fb = 1;
int braking1 = 0;
int braking2 = 0;
int braking = 0;
void setup() {
pinMode(2, INPUT);
pinMode(8, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(brakinglight, OUTPUT);
pinMode(brakinglight2, OUTPUT);
Serial.begin(2000000);
for (int thisReading = 0; thisReading < numReadingslr; thisReading++) {
readingslr[thisReading] = 0;
}
}
void loop() {
braking1 = ch3;
braking1 = braking1 + 3;
unsigned long currentMillis = millis();
//Serial.println(lr);
channellr = pulseIn(2, HIGH);
totallr = totallr - readingslr[readIndexlr];
readingslr[readIndexlr] = channellr;
totallr = totallr + readingslr[readIndexlr];
readIndexlr = readIndexlr + 1;
// if we're at the end of the array...
if (readIndexlr >= numReadingslr) {
// ...wrap around to the beginning:
readIndexlr = 0;
}
// calculate the average:
ch4 = totallr / numReadingslr;
Serial.println(braking1);
Serial.print(" .");
//Serial.println(drivingspeed[0]);
Serial.print(braking2);
//Serial.print(" .");
//Serial.print(drivingspeed[2]);
//Serial.print(" .");
//Serial.print(drivingspeed[3]);
//Serial.print(" .");
//Serial.print(drivingspeed[4]);
//Serial.print(" .");
if (braking1 < braking2) {
//Serial.print(" .");
Serial.print("Braking");
digitalWrite(brakinglight, HIGH);
digitalWrite(brakinglight2, HIGH);
braking = 1;
//fb = 0;
}
else if (braking1 > braking2) {
braking = 0;
digitalWrite(brakinglight, LOW);
digitalWrite(brakinglight2, LOW);
}
channelfb = pulseIn(8, HIGH);
totalfb = totalfb - readingsfb[readIndexfb];
readingsfb[readIndexfb] = channelfb;
totalfb = totalfb + readingsfb[readIndexfb];
readIndexfb = readIndexfb + 1;
// if we're at the end of the array...
if (readIndexfb >= numReadingsfb) {
// ...wrap around to the beginning:
readIndexfb = 0;
}
// calculate the average:
ch3 = totalfb / numReadingsfb;
//Serial.println(ch3);
// send it to the computer as ASCII digits
//turn lights on or off depending on average------------
if (ch4 > 1602) {
lr = 2;
}
if (ch4 < 1450) {
lr = 0;
}
if (ch4 == 1526) {
lr = 1;
}
//if (lr == 1 && fb == 1) {
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
//}
if (lr == 0) {
digitalWrite(ledPin, LOW);
digitalWrite(ledPin3, LOW);
}
if (lr == 2) {
digitalWrite(ledPin, ledState);
digitalWrite(ledPin2, ledState);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
}
else if (lr == 0) {
digitalWrite(ledPin3, ledState);
digitalWrite(ledPin4, ledState);
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
}
if (ch3 < 1488) {
fb = 0;
}
else if (ch3 > 1488) {
fb = 1;
}
if (fb == 0 && lr == 1) {
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin3, HIGH);
}
if (fb == 0 && lr == 2) {
digitalWrite(ledPin3, HIGH);
}
if (fb == 0 && lr == 0) {
digitalWrite(ledPin, HIGH);
}
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
}
else {
ledState = LOW;
}
braking2 = ch3;
//delay a bit for stabability
//set the LED with the ledState of the variable:
// digitalWrite(ledPin, ledState);
}
}
thats my code and when I run it and slowly lower the throttle I get this
That is the Serial monitor the first number is the first reading and the second number is
the second reading. if you see braking it is applying the brake lights....
But see how it stutters every so often? I don't know why??? I am so confused.. I am also a Beginner
so I don't know what to do! Please help!
