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    

Sparkfun joystick, L298 motordriver board and servo to control a rc car.

This code needs to be altered to accept bluetooth serial.

This code is not mine, it is from www.badji.com

/*
 
http://www.bajdi.com
 
 RC car controlled by analog joystick
 Micro SG90 servo to turn front wheels
 DC motor controlled by L298n 
 */
 
#include <Servo.h>
 
Servo myservo;                // create servo object to control a servo
int Y;                        // Y axis = forward/backward
int X;                        // X axis = left/right
int fspeed;           // forward speed 
int bspeed;           // backward speed
int iOldPos, iNewPos = 0;    // servo position
const int in1 = 2;    // direction pin 1
const int in2 = 4;    // direction pin 2
const int ena = 3;    // PWM pin to change speed
 
long previousMillis = 0;      // timer for servo
const int interval = 30;      // interval to update servo position
 
void setup()
{
  pinMode(in1, OUTPUT);      // connection to L298n
  pinMode(in2, OUTPUT);      // connection to L298n
  pinMode(ena, OUTPUT);      // connection to L298n
  myservo.attach(9);    // attaches the servo on pin 9 to the servo object
  myservo.write(87);    // center servo 
  delay(50);           // give servo time to change position
}
 
 
void loop()
{
 
  int X = analogRead(A1);  // joystick X axis
  int Y = analogRead(A0);  // joystick Y axis
 
  if (Y < 500)              // joystick forward
  {
    fspeed = (map(Y, 501, 0, 70, 250));
    forward(fspeed);
  }
 
  if (Y > 540)              // joystick backward
  {
    bspeed = (map(Y, 541, 1023, 70, 250));
    backward(bspeed);
  }
  if (Y >= 500 && Y <= 540)    // joystick is centered
  {
    stop();
  }
 
  if (X > 490 && X < 530 )    // joystick is centered
  {
    iNewPos = 87;     // 87 = servo in center position
  }
 
  if (X < 490)              
  {
    iNewPos = (map(X, 491, 0, 88, 130));
  }
 
  if (X > 530)
  {
    iNewPos = (map(X, 531, 1023, 86, 35));
  }
 
  unsigned long currentMillis = millis();
 
  if(iOldPos != iNewPos && currentMillis - previousMillis > interval) {      // Issue command only if desired position changes and interval is over
    previousMillis = currentMillis;
    iOldPos = iNewPos;
    myservo.write(iNewPos);     // tell servo to go to position in variable 'pos' 
  }
}
 
void stop()
{
  analogWrite(ena, 0);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}
 
void forward(int fspeed)
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(ena, fspeed);
}
 
void backward(int bspeed)
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(ena, bspeed);
}

Back Back to top