-1

this question is regarding how to add turn led signals on arduino powered smart car. I have 4 leds to give turn indications on my smart car(line follower, obstacle avoider, Bluetooth mobile controlled and also IR remote controlled)

can someone help me to modify the code in such a way that I can add led turn signals?(I'm completely new to arduino)

want two front leds high at the same time when the car moves forward and when the car moves backward i need two back leds high at the same time. i also need two right leds high at the same time when it turns right and I need two left leds high at the same time when it turns left .

this is the code

#include <IRremote.h>
#include <Servo.h>

//Pin assignments and global variables per function. Customize if needed
//*******Pin assignments Motor board and IR receiver********************
const int MotorRight1 = 7;
const int MotorRight2 = 8;
const int MotorLeft1 = 9;
const int MotorLeft2 = 10;
const int MotorRightPWM = 6;
const int MotorLeftPWM = 11;
const int irReceiverPin = A5;
const int servoPin = A4; //orange wire
int iSpeed = 255; //speed, range 0 to 255
const int LedPin=13; 

//******Infrared key bindings********************************************
const long IRfront = 16730805;      //go straight: button up
const long IRback = 16718055;       //go back    : button down
const long IRturnright = 16734885;  //turn right : button right
const long IRturnleft = 16716015;   //turn left  : button left
const long IRstop = 16726215;       //stop       : button OK
const long IRcny70 = 16754775;      //CNY70 automatic mode: button EQ
const long IRAutorun = 16750695;    //Ultrasonic mode : button play/pause
//******Track following pin assignments and signals**********************
const int SensorLeft = 5;      //
const int SensorMiddle = 4 ;   //
const int SensorRight = 3;     //
IRrecv irrecv(irReceiverPin);  // IRrecv signal
decode_results infrared;       // decode result
//*******Ultrasonic pin assignments and signals**************************
const int echoPin = 12; // ultrasonic receive=echo pin
const int triggerPin = 13; // ultrasonic send=trigger pin
Servo myservo; // define myservo
const int degreesForward = 130; //nr degrees to look forward
const int degreesLeft = 60; //nr degrees to look left
const int degreesRight = 180; //nr degrees to look right
const int delay_time = 250; // servo motor delay
const int Fgo = 8; // go straight
const int Rgo = 6; // turn right
const int Lgo = 4; // turn left
const int Bgo = 2; // go back
//*****Bluetooth signals**************************************************
char val; //stores received character. Needs to be global to perform continuous movement

//*********General SETUP: activate pins***********************************
void setup() {
  //start receiving serial infor
  Serial.begin(9600);
  //motor connections
  pinMode(MotorRight1, OUTPUT);  //

  pinMode(MotorRight2, OUTPUT);  //

  pinMode(MotorLeft1,  OUTPUT);  //

  pinMode(MotorLeft2,  OUTPUT);  //
  pinMode(MotorRightPWM, OUTPUT); //enable for right side motor
  pinMode(MotorLeftPWM, OUTPUT); //enable for right side motor
  irrecv.enableIRIn();      // start infrared decode
  myservo.write(degreesForward);       // will make head look in front

  //black track following
  pinMode(SensorLeft, INPUT);
  pinMode(SensorMiddle, INPUT);
  pinMode(SensorRight, INPUT);

  //Ultra sonic
  //digitalWrite(2,HIGH); //what is this pin for?
  pinMode(echoPin, INPUT);
  pinMode(triggerPin, OUTPUT);
  myservo.attach(servoPin);
}

//**************Movement functions******************************
void advance(int d) { //go straight
  digitalWrite(MotorRight1, HIGH);
  digitalWrite(ledrightfrontindicator, HIGH);
  digitalWrite(MotorRight2, LOW);
  digitalWrite(ledrightfrontindicator, LOW);
  digitalWrite(MotorLeft1, HIGH);
  digitalWrite(ledleftfrontindicator, HIGH);
  digitalWrite(MotorLeft2, LOW);
  digitalWrite(ledleftfrontindicator, LOW);
  analogWrite(MotorRightPWM, iSpeed);
  analogWrite(MotorLeftPWM, iSpeed);
  delay(d * 10);
}
void right(int d) { //turn right (single wheel)
  digitalWrite(MotorLeft1, LOW);
  digitalWrite(MotorLeft2, HIGH);
  digitalWrite(MotorRight1, LOW);
  digitalWrite(MotorRight2, LOW);
  analogWrite(MotorRightPWM, iSpeed);
  analogWrite(MotorLeftPWM, iSpeed);
  delay(d * 10);
}
void left(int d) {//turn left(single wheel)
  digitalWrite(MotorRight1, LOW);
  digitalWrite(MotorRight2, HIGH);
  digitalWrite(MotorLeft1, LOW);
  digitalWrite(MotorLeft2, LOW);
  analogWrite(MotorRightPWM, iSpeed);
  analogWrite(MotorLeftPWM, iSpeed);
  delay(d * 10);
}
void turnR(int d) {//turn right (two wheels)
  digitalWrite(MotorRight1, HIGH);
  digitalWrite(MotorRight2, LOW);
  digitalWrite(MotorLeft1, LOW);
  digitalWrite(MotorLeft2, HIGH);
  analogWrite(MotorRightPWM, iSpeed);
  analogWrite(MotorLeftPWM, iSpeed);  
  delay(d * 10);
}
void turnL(int d) {//turn left (two wheels)
  digitalWrite(MotorRight1, LOW);
  digitalWrite(MotorRight2, HIGH);
  digitalWrite(MotorLeft1, HIGH);
  digitalWrite(MotorLeft2, LOW);
  analogWrite(MotorRightPWM, iSpeed);
  analogWrite(MotorLeftPWM, iSpeed);
  delay(d * 10);
}
void stopp(int d) { //stop
  digitalWrite(MotorRight1, LOW);
  digitalWrite(MotorRight2, LOW);
  digitalWrite(MotorLeft1, LOW);
  digitalWrite(MotorLeft2, LOW);
  analogWrite(MotorRightPWM, iSpeed);
  analogWrite(MotorLeftPWM, iSpeed);  
  delay(d * 10);
}
void back(int d) { //go back
  digitalWrite(MotorRight1, LOW);
  digitalWrite(MotorRight2, HIGH);
  digitalWrite(MotorLeft1, LOW);
  digitalWrite(MotorLeft2, HIGH);
  analogWrite(MotorRightPWM, iSpeed);
  analogWrite(MotorLeftPWM, iSpeed);  
  delay(d * 10);
}

//************Ultrasonic distance calculator*************************************
//detect distance for given angles and print char + direction
float getDistance(int degrees, char dir) {
  myservo.write(degrees);
  digitalWrite(triggerPin, LOW); // ultrasonic echo low level in 2us
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH); // ultrasonic echo high level in 10us, at least 10us
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW); // ultgrasonic echo low level
  float distance = pulseIn(echoPin, HIGH); // read time
  distance = distance / 5.8 / 10; // turn time to distance
  Serial.print(dir); // 
  Serial.print(" distance: "); // 
  Serial.print(int(distance)); //  output distance (mm)
  Serial.print("\n");
  return distance;
}
//*************Ultrasonic direction decision making******************************
//measurements three angles (front, left, right
int getDirectionFromdetection() {
  int Fspeedd = 0; // front distance
  int Rspeedd = 0; // right distance
  int Lspeedd = 0; // left distance
  int delay_time = 250; //
  int directionn =0;

  //get front distance
  Fspeedd = getDistance(degreesForward, 'F');

  // if distance is less than 10mm
  if (Fspeedd < 10) {
    stopp(1); //  clear output
    directionn = Bgo;
  }
  // if distance less than 25 mm
  else if (Fspeedd < 25) {
    stopp(1); // clear output
    Lspeedd = getDistance(degreesLeft, 'L'); // detection distance on left side
    delay(delay_time); // waiting for the servo motor to become stable

    Rspeedd = getDistance(degreesRight, 'R'); // detection distance on right side
    delay(delay_time); // waiting for servo motor to be stable

    // if left distance greater than right
    if (Lspeedd > Rspeedd) {
      directionn = Lgo; // go left
    }

    if (Lspeedd <= Rspeedd) {//if left distance less than right
      directionn = Rgo; //go right
    }

    if (Lspeedd < 15 && Rspeedd < 15) { //if distance less 10mm both right and left
      directionn = Bgo; //go back
    }
  }
  else {
    directionn = Fgo; //go straight
  }
  return directionn;
}

void autoRunUsingUltraSonic() {
  bool stopPressed;
  int directionn = 0; // front=8, back=2, left=4, right=6
  while (IRAutorun) {
    myservo.write(80); // make the servo motor reset
    directionn = getDirectionFromdetection();
    stopPressed = stopCommandPressed();
    if (stopPressed) {
      break;
    }
    else if (directionn == Fgo) { //go straight
      infrared.value = 0;
      advance(5); //
      Serial.print(" Advance "); //
      Serial.print(" ");
    }
    else if (directionn == Bgo) { //go back
      infrared.value = 0;
      back(8); //
      turnL(3); //
      Serial.print(" Reverse "); //
    }
    else if (directionn == Rgo) { //turn right
      infrared.value = 0;
      back(1);
      turnR(60); //
      Serial.print(" Right "); //
    }
    else if (directionn == Lgo) { //turn left
      infrared.value = 0;
      back(1);
      turnL(60);
      Serial.print(" Left ");
    }
  }
  infrared.value = 0;
}

//*************************Bluetooth functionality***********************
//Bluetooth commands
void bluetoothCommand() {
  if (Serial.available()) { //check if bluetooth command available
    val = Serial.read();
    Serial.write(val);
  }
  if (val == 'F') { // Forward
    advance(10);
  }
  else if (val == 'S') { // Stop Forward
    stopp(10) ;
    val = Serial.read(); //read value again, otherwise can't continu with infrared
  }
  else if (val == 'B') { // Backwards
    back(10);
  }
  else if (val == 'R') { // Right
    turnL(10);
  }
  else if (val == 'L') { // Left
    turnR(10);
  }
  else if (val == 's') { // Stop, not used though
    stopp(10 ) ;
  }
  else if (int(val) >= 49 && int(val) <= 57) { //set speed
    iSpeed = (int(val)-48)*26;
    Serial.println("Speed set to: " + iSpeed); 
  }
  else if (val == 'q') { //set speed
    iSpeed = 255;
    digitalWrite(LedPin,HIGH);   
    Serial.println("Speed set to: " + iSpeed);  
  }
  else if (val == 'W') {
    digitalWrite(LedPin,HIGH);
  }
  else if (val == 'w') {
    digitalWrite(LedPin,LOW);
  }
}

//Check if stop command on remote is pressed (button 5)
bool stopCommandPressed(){
  bool stopPressed = false;
  if (irrecv.decode(&infrared)) {
    irrecv.resume();
    Serial.println(infrared.value, HEX);
    if (infrared.value == IRstop) {
      stopp(10);
      stopPressed = true;
    }
  }
  return stopPressed;
}

void followBlackLine() {
  bool stopPressed;
  int SL;  //sensor left
  int SM;  //sensor middle
  int SR;  //sensor right
  while (IRcny70) {
    SL = digitalRead(SensorLeft);
    SM = digitalRead(SensorMiddle);
    SR = digitalRead(SensorRight);

    if (SM == HIGH) {//middle sensor in black area
      if (SL == LOW & SR == HIGH) {//left sensor in black area, right sensor in white, turn left
        left(0);
      }
      else if (SR == LOW & SL == HIGH) {//left white, right black, run right
        right(0);
      }
      else { // left and right both in white, go straight
        advance(0);
      }
    }
    // middle sensor in white area
    else {
      if (SL == LOW & SR == HIGH) { // left black ,right white, turn left
        left(0);
      }
      else if (SR == LOW & SL == HIGH) {
        right(0);
      }
      else {  //left and right both in white, stop
        stopp(0);
      }
    }
    stopPressed = stopCommandPressed();
    if (stopPressed) {
      break;
    }
  }
  infrared.value = 0;  
}

//**************************************MAIN LOOP***************************************
void loop() {
  //bluetooth commands
  bluetoothCommand();
  //************************************normal remote control mode ********
  // decoding success 'receive infrared signal
  if (irrecv.decode(&infrared))  {         
    if (infrared.value == IRfront) {
      advance(0); //go straigt
    }
    else if (infrared.value ==  IRback) {
      back(0); //go back
    }
    else if (infrared.value == IRturnright) {
      turnR(0); // go right
    }
    else if (infrared.value == IRturnleft) {
      turnL(0); // go left
    }
    else if (infrared.value == IRstop) {//stop
      stopp(0);
    }
    //********************follow black line********cny70 automatic mode********
    else if (infrared.value == IRcny70) {
      followBlackLine();
    }
    //***********************ultrasonic automatic mode***************************
    else if (infrared.value == IRAutorun ) {
      autoRunUsingUltraSonic();
      myservo.write(degreesForward);       // will make head look in front
    }
    //********************wait a little before continuing**************************
    irrecv.resume();
    delay(300);
  }
  else {
    stopp(0);
  }
}
  • 1
    This is quite a lot of code. Did you write it yourself? What exactly prevents you from adding LEDs, which you turn on and off with digitalWrite(), just like you activate the motors. – chrisl Nov 24 '18 at 14:12
  • Why do you have this loop in your followBlackLine() function: while (IRcny70) ? `IRcny70 is a constant with a value greater than zero, so this is essentially an infinite loop, thats difficult to read. – chrisl Nov 24 '18 at 14:15
  • The same goes for autoRunUsingUltraSonic() – chrisl Nov 24 '18 at 14:17
  • I got the code online – NaveenVelusamy Nov 24 '18 at 14:21
  • 1
    So no blinking or stuff like that? Then you can simply turn on the corresponding LEDs in your moving functions ( back(), advance()) with a digitalWrite() and turn them off after the delay() with the same function. Have you looked at the many examples, that come with the Arduino IDE? They can teach you how to control LEDs right good. – chrisl Nov 24 '18 at 14:33
0

Like chrisl suggested, add the led code to the functions advance, back, left, and right

const int LedFrontRight = 21;//change pin numbers
const int LedFrontLeft = 22;
const int LedBackRight = 23;
const int LedBackLeft = 24;

// ...

void setup() {
  // ...
  pinMode(LedFrontRight, OUTPUT);
  pinMode(LedFrontLeft , OUTPUT);
  pinMode(LedBackRight , OUTPUT);
  pinMode(LedBackLeft  , OUTPUT);
}

//**************Movement functions******************************
void advance(int d) { //go straight
  digitalWrite(LedFrontRight, HIGH);
  digitalWrite(LedFrontLeft , HIGH);
  digitalWrite(LedBackRight , LOW);
  digitalWrite(LedBackLeft  , LOW);
  //... 
}
void right(int d) { //turn right (single wheel)
  digitalWrite(LedFrontRight, HIGH);
  digitalWrite(LedFrontLeft , LOW);
  digitalWrite(LedBackRight , HIGH);
  digitalWrite(LedBackLeft  , LOW);
  //... 
}
void left(int d) {//turn left(single wheel)
  digitalWrite(LedFrontRight, LOW);
  digitalWrite(LedFrontLeft , HIGH);
  digitalWrite(LedBackRight , LOW);
  digitalWrite(LedBackLeft  , HIGH);
  //... 
}
void turnR(int d) {//turn right (two wheels)
  digitalWrite(LedFrontRight, HIGH);
  digitalWrite(LedFrontLeft , LOW);
  digitalWrite(LedBackRight , HIGH);
  digitalWrite(LedBackLeft  , LOW);
  //... 
}
void turnL(int d) {//turn left (two wheels)
  digitalWrite(LedFrontRight, LOW);
  digitalWrite(LedFrontLeft , HIGH);
  digitalWrite(LedBackRight , LOW);
  digitalWrite(LedBackLeft  , HIGH);
  //... 
}
void stopp(int d) { //stop
  digitalWrite(LedFrontRight, LOW);
  digitalWrite(LedFrontLeft , LOW);
  digitalWrite(LedBackRight , LOW);
  digitalWrite(LedBackLeft  , LOW);
  //... 
}
void back(int d) { //go back
  digitalWrite(LedFrontRight, LOW);
  digitalWrite(LedFrontLeft , LOW);
  digitalWrite(LedBackRight , HIGH);
  digitalWrite(LedBackLeft  , HIGH);
  //... 
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.