Some new stuff
  1. Arduino Park Control
  2. Arduino USB2serial programming the bare bone
  3. Arduino Projects Videos added
  4. Arduino Xbee projects
  5. Arduino dB Meter 
  6. Trying to get the IOIO going. 
  7. IOIO is going....bluetooth
  8. Attiny 2313 bluetooth bee RC car
  9. Compass and temperature added
  10. AVR programmer
  11. USB2Serial and Ethernet POE
  12. Arduino LPG Sensor
  13. Program the Arduino Mini 05
  14. Lipo Rider Pro in action
  15. EZ Robot Builder kit arrived 10 Nov 2012
  16. Arduino VA Meter!
  17. 05 Nov 2012 Project Sentry Gun.
  18. Attiny85 PIR Sensor 10 Dec 2012 
  19. 0-30VDC3A and 2-28VDC10A bench PSU's
  20. 19 Dec 2012 Arduino Voltmeter LCD
  21. 29 Dec 2012 Arduino Tachometer&Speedometer LCD
  22. 03 Jan 2013 Arduino AC Phase Control. 
  23. 18 Jan 2013 Arduino Distance Meter
  24. 22 Jan 2013 Arduino Digital Clock and Date
  25. 31 Jan 2013 Arduino Digital Clock and Date 8x2 LCD
  26. Feb 2013 LCD and Backpack
  27. Feb 2013 7 Segment Serial Clock Sparkfun. Distance Sensor with "newping" library
  28. Feb 2013 Lelo Remote
  29. 01/03/2013 Arduino Adafruit 7 Seg Digital Clock with RTC and backpack 
  30. Arduino Frequency Meter 26/03/2013
  31. Another RC  Car controlled by Arduino 06/04/2013
  32. My quadcopter project update 25 April 2013
  33. My Aqua Quad Copter Flying 26 April 2013enlightened
  34. Another monster RC truck;strong one! 11July2013cool
  35. X-Frame Quad Copter 31 July 2013cool
  36. Arduino GSM Sheild added.28/8/2013
  37. 1.8inch TFT screen 13/09/2013cool
  38. 04/11/2014 added PID Soldering Iron Control
  39. Added bluetooth servo control 04/11/2014
  40. ESP8266 first try.16/03/2015
  41. Arduino Uno Quadcopter 19 May 2015
  42. IOT WemosD1 Amp Meter

 

 


Search    

Arduino, android application and SRF02 ultrasonic ping sensor to for obstacle avoidance.

I use the same application on android as before on this website.

This time added obstacle avoidance, voltage monitor and intelligent lighting for the RC car.

Uno/mini 328 used with bluetooth

See the sketch intro for more information

Picture applicable which is on the prototype board contains all sensors, motor, bluetooth, servo and mini arduino328 5v. Ready to built into a RC. 

Code:

//1.0.5 UNO R3
/* This code make use of timedaction.h libraries to accommodate interrupts being the blinking led's.
 The codes play a James Bond melody for intro after switch on of the circuit.
 Also battery voltage monitor included.LED indication.
 Obstacle avoidance(antichrash) with ping only front fixed sensor SRF02 and not on pan and tilt.
 SRF02:PCB back referred: +5VDC left top corner.
 Second pin from top left = A4 : I2C
 Third pin from top       = A5 : I2C
 Fourt pin from top       = Unused for I2C
 Bottom pin               = Ground
 Buzzer for horn (polarity involved).
 8 Ohm speaker for melody.
 Application from the play market with speedcontrol and servo control.Joystick website:
 
 Bluetooth module.
 Motordriverboard.L298 or similar.
 PSU for 5v and 3V3 respectively.Voltage regulated.
 Existing brushed motor. Existing gearbox on stripped RC car.4 wheel drive monster truck HSP stripped from its inside electronics.
 Existing Servo for front steering.
 Existing NiMH battery(   Volts) and switch.
 100K and 10K quarter watt in series resistor devider on A1. Top +5V, lower gnd and middle to A1.Voltage monitor.
 My supply voltage max 14VDC.From here 6VDC for servo!
 Note:Connect servo to its own supply and not from the arduino board.
 LDR:4K7 resistor.A0
 OPEN; A0 and A3 (Can be featured use for extraPin3 and extraPin5 controlled from phone).
 On mini A6 and A7 too.
 */
#include "Wire.h"
#include "SRF02.h"              //Ping library 
#include "pitches.h"            //Library file tone
#include <TimedAction.h>        //Like super loop
#include <Servo.h>              //Servo steering
#define NO_SOUND 0              // make the rests in music
TimedAction timedAction = TimedAction(250, blink); //This initializes a TimedAction class that will change the state of an LED every quarter second
Servo leftRight;                //Front steering servo
boolean ledState = false;
byte commands[3];               //Stores the 3 readings sent by the phone.
//Battery supply voltage monitor
int analogInput = 1;//A1
float vout = 0.0;
float vin  = 0.0;
float R1   = 100000.0; // resistance of R1 (100K) -see text!
float R2   = 10000.0; // resistance of R2 (10K) - see text!
int value  = 0;

int emergencySpeed   =    180;  //set speed to reverse motor for emergency stop.
int extra4           =    13;   //Blue rooftop leds blinking with timedaction blink interrupt
int extra5           =    12;   //Horn buzzer with polarity
int speakerPin       =    9;    //Melody on 8 ohm speaker
int pinForward       =    8;    //from arduino to motordriverboard
int pinBack          =    7;    //from arduino to motordriverboard
int pinSpeedForwardBack = 6;    //Speed control of motor
int voltLedpin       =    5;    //Output for voltage monitor LED.
int extra1           =    4;    //Hazards
int pinbackLights    =    3;    //Backlights
int pinfrontLights   =    2;    //Frontlights
int pace             =  1600; // change pace of music("speedy")
int melody[] =
{
  //Introduction://array of notes
  NOTE_E4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_E4,
  NOTE_E4, NOTE_G4, NOTE_G4, NOTE_G4, NOTE_G4, NOTE_E4, NOTE_E4, NOTE_E4,
  NOTE_E4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_E4,
  NOTE_E4, NOTE_G4, NOTE_G4, NOTE_G4, NOTE_G4, NOTE_E4, NOTE_E4, NOTE_E4,
  NOTE_DS5, NOTE_D5, NOTE_B4, NOTE_A4, NOTE_B4,
  NOTE_E4, NOTE_G4, NOTE_DS5, NOTE_D5, NOTE_G4, NOTE_B4,
  NOTE_B4, NOTE_FS5, NOTE_F5, NOTE_B4, NOTE_D5, NOTE_AS5,
  NOTE_A5, NOTE_F5, NOTE_A5, NOTE_DS6, NOTE_D6, NO_SOUND
};
int noteDurations[] =
{
  8, 16, 16, 8, 4, 8, 8, 8,
  8, 16, 16, 8, 4, 8, 8, 8,
  8, 16, 16, 8, 4, 8, 8, 8,
  8, 16, 16, 8, 4, 8, 8, 8,
  8, 2, 8, 8, 1,
  8, 4, 8, 4, 8, 8,
  8, 8, 4, 8, 4, 8,
  4, 8, 4, 8, 3
};// note duration: 1 = whole note, 2 = half note, 4 = quarter note, 8 = eighth note, etc.

SRF02 srf02[1] = 

  SRF02(0x70, SRF02_CENTIMETERS),
  //SRF02(0x71, SRF02_CENTIMETERS), 
  //SRF02(0x72, SRF02_CENTIMETERS)
};

unsigned long nextPrint = 0;

/*Intelligent Lighting*/

#define ldrPin       0   //LDR and resistor 4k7. A0
int parklightPin =  11; // Pin used for parkLight Led.
int inVal        =   0; // variable used to store state of input
int switchOn     = 100; // value at which we switch parkLightPin on.

void setup()
{
  for (int Note = 0; Note < 54; Note++)        //counter of Notes (54 limit the array)
  {
    int duration = pace / noteDurations[Note]; //Adjust duration with the pace of music
    tone(9, melody[Note], duration);           //Play note
    delay(duration * 1.2);                     // to distinguish the notes, set a minimum time between them.
  }
  Serial.begin(9600);                          //Initialize serial communication with Bluetooth module at 9600 btu.
  Wire.begin();
  pinMode(pinForward, OUTPUT);
  pinMode(pinBack, OUTPUT);
  pinMode(pinSpeedForwardBack, OUTPUT);
  pinMode(pinbackLights, OUTPUT);
  pinMode(pinfrontLights, OUTPUT);
  pinMode(speakerPin, OUTPUT);
  pinMode(extra1, OUTPUT);
  pinMode(extra4, OUTPUT);
  pinMode(extra5, OUTPUT);
  pinMode(analogInput, INPUT);
  pinMode(voltLedpin, OUTPUT);
  pinMode(ldrPin, INPUT);
  pinMode(parklightPin, OUTPUT);
  leftRight.attach(10);              //Servo on pin 10 of Arduino
  timedAction.disable();             //We disable the blinking leds now and will activate with button from application on phone.
  digitalWrite(extra4, ledState);
  SRF02::setInterval(100);
}

void loop()
{
  SRF02::update();
  if (millis() > nextPrint)
  {
    int distance = srf02[0].read();
    if (distance > 0 && distance <= 75 && commands[0] == 0xf1)
    {
      EmergencyStop();
    }
    //Serial.print(distance);
    // Serial.println("cm");
    // Serial.print(",");
    //Serial.print(srf02[1].read());
    //Serial.print(",");
    //Serial.print(srf02[2].read());
    //Serial.println();
    nextPrint = millis () + 1000;
  }

  timedAction.check();                    //Our blink leds interruption
  voltageMonitor();                       //Led battery voltage indication.
  Intelligentlighting();                  //May be to the setup???? 

  int available = Serial.available();
  if (available == 3)
  {
    commands[0] = Serial.read();         //Direction.Commands from the phone
    commands[1] = Serial.read();         //Speed.    Commands from the phone
    commands[2] = Serial.read();         //Angle.    Commands from the phone

    /*
    Since the last byte yields the servo's angle (between 0-180 or 0x00-0xb4), it can never
         be 255 (0xff). This and the range of the first byte (0xf1 - 0xfc) are used in the following
         IF statement to make sure that the bytes never get mixed up.
     */
    if((commands[2] <= 0xb4) && ((commands[0] <= 0xfc) && (commands[0] >= 0xf1)))
    {
      if(commands[0] == 0xf1)       //if incoming data is a F, move forward
      {
        digitalWrite(pinForward, HIGH);
        digitalWrite(pinBack, LOW);
        analogWrite(pinSpeedForwardBack, commands[1]);
        leftRight.write(commands[2]);
      }
      else if(commands[0] == 0xf2)  //if incoming data is a B, move back
      {
        digitalWrite(pinForward, LOW);
        digitalWrite(pinBack, HIGH);
        analogWrite(pinSpeedForwardBack, commands[1]);
        leftRight.write(commands[2]);
      }
      else if(commands[0] == 0xf3)  //if incoming data is a S, stop
      {
        digitalWrite(pinForward, LOW);
        digitalWrite(pinBack, LOW);
        analogWrite(pinSpeedForwardBack, commands[1]);
        leftRight.write(commands[2]);
      }
      else if(commands[0] == 0xf6) //Back and front lights
      {

        if(commands[1] == 0x01)   //back lights on
        {
          digitalWrite(pinbackLights, HIGH);
        }
        else if(commands[1] == 0x00) //back lights off
        {
          digitalWrite(pinbackLights, LOW);
        }
        if(commands[2] == 0x01)   //front lights on
        {
          digitalWrite(pinfrontLights, HIGH);
        }
        else if(commands[2] == 0x00) //front lights off
        {
          digitalWrite(pinfrontLights, LOW);
        }
      }
      else if(commands[0] == 0xf8)              //Extra button 1
      {
        if((commands[1] == 0x01) && (commands[2] == 0x01))
        {
          digitalWrite(extra1, HIGH);
        }
        else if((commands[1] == 0x00) && (commands[2] == 0x00))
        {
          digitalWrite(extra1, LOW);
        }
      }
      else if(commands[0] == 0xfb)           //Extra button 4
      {

        if((commands[1] == 0x01) && (commands[2] == 0x01))
        {
          timedAction.enable();          // Begin blink led's
        }
        else if((commands[1] == 0x00) && (commands[2] == 0x00))
        {
          timedAction.disable();        // Stop blinking the leds
        }
      }
      else if(commands[0] == 0xfc)          //Extra button 5. Horn sequence
      {
        if((commands[1] == 0x01) && (commands[2] == 0x01))
        {
          digitalWrite(extra5, HIGH);
          delay(100);
          digitalWrite(extra5, LOW);
          delay(100);
          digitalWrite(extra5, HIGH);
          delay(100);
          digitalWrite(extra5, LOW);
          delay(100);
          digitalWrite(extra5, HIGH);
          delay(100);
        }
        else if((commands[1] == 0x00) && (commands[2] == 0x00))
        {
          digitalWrite(extra5, LOW);
        }
      }
    }
    else
    {
      Serial.end();      //Resetting the Serial port (clearing the buffer) in case the bytes are not being
      //read in correct order.
      Serial.begin(9600);
    }
  }
}

void blink()
{
  ledState ? ledState = false : ledState = true;
  digitalWrite(extra4, ledState);
}

void EmergencyStop()
{
  digitalWrite(pinbackLights, HIGH);              //Backlights on to indicate emergency stop.
  tone(extra5, 500);                              //Buzzer tone audio to indicate emergency.
  digitalWrite(pinForward, LOW);                  //this stops the motor turning. Both pins to LOW
  digitalWrite(pinBack, LOW);
  delay(500);                                     //Vary this just enough to see the car stop,
  digitalWrite(pinForward, LOW);                  //This reverse the motor after stop
  digitalWrite(pinBack, HIGH);
  analogWrite(pinSpeedForwardBack, emergencySpeed); //The motor reverse with this speed set on top.
  delay(1250);                                    //Delay after reverse and motor stop again.Hopefully without momentum again!!
  //Vary delay accordingly to get momentum to a minimum backwards run.
  digitalWrite(pinForward, LOW);                  //Motor stop without momentum.
  digitalWrite(pinBack, LOW);
  digitalWrite(pinbackLights, LOW);               //Backlights off again.
  noTone(extra5);                                 //Make sure the buzzer wont buzzzz.
  delay(1);                                       //The end of the emergency stop,and code ready to return
  Serial.end();//Resetting the Serial port (clearing the buffer) in case the bytes are not being read in correct order.
  Serial.begin(9600);

}

void voltageMonitor()

{
  // read the value at analog input
  value = analogRead(analogInput);
  vout = (value * 4.85) / 1024.0; // see text
  vin = vout / (R2/(R1+R2));
  //Serial.println(vout);

  if (vout <= 0.74)
  {  
    digitalWrite(voltLedpin, HIGH);
  }
  else
  {
    digitalWrite(voltLedpin, LOW);
  }
}

void Intelligentlighting()

{
  inVal = analogRead(ldrPin);                 // Read state of the input pin
  //Serial.println(inVal);
  //delay(550);
  if(inVal <= switchOn)
  {
    digitalWrite(parklightPin, HIGH);
  }

  else
  {
    digitalWrite(parklightPin, LOW);
  }
}

 

Back Back to top