RC car lights!

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!

can you explain what ch3, ch3, braking1 and braking2 represent?

ch3 is the reading on PWM channel 3
ch4 is the reading on PWM channel 4

braking1 is the reading of ch3 and saving it for sensing if the number has lowered braking1 is taken at the start of the code
braking2 is the reading of ch3 and saving it for sensing if the number has lowered braking 2 is taken at the end of
the code
they are tested to see if braking1 is higher then braking2 and if so it knows I am lowering the throttle!
when it detects I am lowering the throttle it turns on brakinglight, brakinglight2, and brakinglight3

if ch3 and ch4 are the left and right wheel speeds, shouldn't a speed value (i.e. "braking") be their average?

braking2 is updated inside the block of code invoked every "interval". braking2 may have been updated just before those conditional tests (e.g. braking1 < braking2) and there may not have been much time for any speed change to occur.

if you want braking2 to represent the previous braking value, why not update them both inside the "interval" block; set braking2 to braking1 and braking1 to (ch3+ch4)/2. braking2 then represents the "braking" an "interval" before braking1 and their difference more reliably represents a change in speed.

That fixed it!
It turns out my code was messed up and scanning to fast.. So I put my code that checks to see if it is lower into a 500 ms delay I had going that uses Millis! it works now flawlessly!
Thank you for your help and I really appreciate it!

Here is the code that I used to fix it!

double channellr;
double channelfb;
const int numReadingslr = 10;
const int numReadingsfb = 15;
const int ledPin = A0;// the number of the LED pin
const int ledPin2 = A1;// the number of the LED pin
const int ledPin3 = A2;// the number of the LED pin
const int ledPin4 = A3;// the number of the LED pin
const int brakinglight = A4;
const int brakinglight2 = A5;
const int brakinglight3 = 3;
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);
  pinMode(brakinglight3, 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);
    digitalWrite(brakinglight3, HIGH);
    braking = 1;
    fb = 0;
  }
  else if (braking1 > braking2) {
    braking = 0;
    digitalWrite(brakinglight, LOW);
    digitalWrite(brakinglight2, LOW);
    digitalWrite(brakinglight3, LOW);
  }
  braking2 = braking1 - 2;
  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 (braking == 0) {
  // digitalWrite(brakinglight, HIGH);
  // digitalWrite(brakinglight2, HIGH);
  //}
  //else if (braking == 1) {
  //digitalWrite(brakinglight, LOW);
  //digitalWrite(brakinglight2, LOW);
  //}

  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;
      braking1 = ch3;
    }
    else {
      ledState = LOW;
    }
    //braking2 = ch3;
    //delay(5);//delay a bit for stabability
    //set the LED with the ledState of the variable:
    // digitalWrite(ledPin, ledState);
  }

}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.