<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bajdi.com</title>
	<atom:link href="http://www.bajdi.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bajdi.com</link>
	<description>Breadboards Arduino Joystick DHT22 IR ...</description>
	<lastBuildDate>Sat, 19 May 2012 23:09:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Playing with Dagu Rover 5 with gripper</title>
		<link>http://www.bajdi.com/playing-with-dagu-rover-5-with-gripper/</link>
		<comments>http://www.bajdi.com/playing-with-dagu-rover-5-with-gripper/#comments</comments>
		<pubDate>Sat, 19 May 2012 23:09:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Arduino sketches]]></category>

		<guid isPermaLink="false">http://www.bajdi.com/?p=394</guid>
		<description><![CDATA[Finally got around to make a sketch to remote control my Rover 5 and gripper. I started with a sketch to control just the gripper with a 2 axis joystick and 2 buttons to open and close the gripper. While testing that sketch I made a little mistake that stripped the gears of the claw [...]]]></description>
			<content:encoded><![CDATA[<p><iframe src="http://player.vimeo.com/video/42465800" width="600" height="337" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></p>
<p>Finally got around to make a sketch to remote control my Rover 5 and gripper. I started with a sketch to control just the gripper with a 2 axis joystick and 2 buttons to open and close the gripper. While testing that sketch I made a little mistake that stripped the gears of the claw servo. Luckily I received a new servo very fast and while I was at it I ordered a spare one <img src='http://www.bajdi.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Most servos can move 180 degrees and if you have mounted a servo so it can only move less then 180 degrees because of mechanical reasons it&#8217;s best to limit the movement of the servo in software. I hadn&#8217;t thought of that and my code sent the servo to a position it physically couldn&#8217;t go to and that stripped the metal gears of the servo. Luckily these little Chinese servos are pretty cheap so it wasn&#8217;t a big loss. It&#8217;s a lesson learnt. I did have some more problems testing the gripper. When I use the sketch with only the code to control the gripper the servos move very fast and I had to slow them down in the sketch else I couldn&#8217;t control them accurately. When I added that sketch to my existing remote control sketch of my rover the servos where very slow so I had to change it so they moved faster. To power the 3 servos I use a very cheap 4A UBEC (5V regulator). I soldered a bunch of pins to a small PCB to connect the UBEC to the servos and the micro controller. I&#8217;ve even added a big capacitor to the board to help keep the noise of the UBEC under control. I was a bit worried that it will interfere with my 2,4GHz wireless nRF24L01 module. Since I&#8217;ve added the gripper my Lipo batteries seem to go flat a lot faster, those 3 servos must draw a considerate amount of power.<br />
I used my self made remote control (Arduino Uno + joystick shield + nRF24L01 + I2C 4&#215;20 LCD) to control the Rover and gripper. The LCD shows the current of the 4 motors and the battery voltages of the battery in the remote and the rover. </p>
<p>Here are the sketched I&#8217;ve used in the video:<br />
The sketch that went in my self made remote control:</p>
<pre class="brush: arduino; title: ; notranslate">
// http://www.bajdi.com
// Sketch to remote control Dagu Rover 5 with 3 DIF gripper
// Nrf24L01 connected to Arduino Uno
// Nrf24L01 connection details http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
// 4 x 20 I2C LCD
// Transmit analog values from joystick and the state of 3 buttons to the Rover 5 using RF24 library.
// Receive 4 floats (current values from the motors) and the battery voltage from the Rover 5 and display it on the LCD

#include &lt;SPI.h&gt;
#include &lt;nRF24L01.h&gt;
#include &quot;RF24.h&quot;
#include &lt;Wire.h&gt;
#include &lt;LiquidCrystal_I2C.h&gt;
#include &lt;Bounce.h&gt;

LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27

RF24 radio(8,9);

const uint64_t pipes[2] = {
  0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

int joystick[5];
float rover[5];
float batr;

const int z = 2;
const int clawopen = 3;
const int clawclose = 6;
int roverState;         // the rover state of the output pin

Bounce bouncerz = Bounce( z, 10 );

long previousMillis = 0;
const int interval = 25;

void setup(){
  Serial.begin(9600);
  pinMode(z, INPUT);
  digitalWrite(z, HIGH);
  pinMode(clawopen , INPUT);
  pinMode(clawclose , INPUT);
  digitalWrite(clawopen, HIGH);
  digitalWrite(clawclose, HIGH);
  radio.begin();
  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(1,pipes[1]);
  radio.startListening();

  lcd.init(); // initialize the lcd
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print(&quot;M1=&quot;);
  lcd.setCursor(8,0);
  lcd.print(&quot;M2=&quot;);
  lcd.setCursor(0,1);
  lcd.print(&quot;M3=&quot;);
  lcd.setCursor(8,1);
  lcd.print(&quot;M4=&quot;);
  lcd.setCursor(0,2);
  lcd.print(&quot;Bat remote = &quot;);
  lcd.setCursor(0,3);
  lcd.print(&quot;Bat rover = &quot;);
}

void loop(){
  joystick[0] = analogRead(A0);
  joystick[1] = analogRead(A1);
  joystick[2] = roverState;

  batr = analogRead(A3) / 102.3;

  if ( bouncerz.update() ) {
    if ( bouncerz.read() == LOW) {
      if ( roverState == HIGH ) {
        roverState = LOW;
      }
      else {
        roverState = HIGH;
      }
    }
  }

  joystick[3] = digitalRead(clawopen);
  joystick[4] = digitalRead(clawclose);

  if ( radio.available() )
  {
    unsigned long roverMillis = millis();

    if(roverMillis - previousMillis &gt; interval) {
      previousMillis = roverMillis;   

      // Dump the payloads until we've gotten everything
      bool done = false;
      while (!done)
      {
        // Fetch the payload, and see if this was the last one.
        done = radio.read( &amp;rover, sizeof(rover) );

      }
    }

    radio.stopListening();

    bool ok = radio.write( &amp;joystick, sizeof(joystick) );

    radio.startListening();  

  }
  lcd.setCursor(3,0);
  lcd.print(rover[0]);
  lcd.setCursor(11,0);
  lcd.print(rover[1]);
  lcd.setCursor(3,1);
  lcd.print(rover[2]);
  lcd.setCursor(11,1);
  lcd.print(rover[3]);
  lcd.setCursor(13,2);
  lcd.print(batr);
  lcd.setCursor(12,3);
  lcd.print(rover[4]);
}
</pre>
<p>The sketch that went into the Mega1280 based micro controller on the Rover 5:</p>
<pre class="brush: arduino; title: ; notranslate">
// http://www.bajdi.com
// Dagu Rover 5 chassis with 4 motors and 3 DOF gripper
// Remote control through Nrf24L01 2,4GHz wireless module
// µcontroller = Dagu Red back spider, Arduino Mega 1280 compatible
// Motor controller = Dagu 4 channel motor controller

#include &lt;SPI.h&gt;
#include &lt;nRF24L01.h&gt;
#include &quot;RF24.h&quot;
#include &quot;Ultrasonic.h&quot;
#include &lt;Servo.h&gt;

Servo bottomservo;  // create servo object to control a servo
Servo shouldservo;  // create servo object to control a servo
Servo clawservo;  // create servo object to control a servo

/* Nrf24L01 pinout
 1 GND
 2 VCC (3,3V)
 3 CE    pin 48 on Mega 1280
 4 CSN   pin 49 on Mega 1280
 5 SCK   pin 52 on Mega 1280
 6 MOSI  pin 51 on Mega 1280
 7 MISO  pin 50 on Mega 1280
 8 IRQ (not used)
 */

RF24 radio(48,49);
const uint64_t pipes[2] = {
  0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

const int DirM1 = 30; // direction motor 1
const int DirM2 = 31; // direction motor 2
const int DirM3 = 32; // direction motor 3
const int DirM4 = 33; // direction motor 4
const int PWMM1 = 4;  // pwm motor 1
const int PWMM2 = 5;  // pwm motor 2
const int PWMM3 = 6;  // pwm motor 3
const int PWMM4 = 7;  // pwm motor 4

uint8_t fs; // forward speed
uint8_t bs; // backward speed
uint8_t ls; // turn left speed
uint8_t rs; // turn right speed

int bat; // analog reading of battery voltage through voltage divider
const int lowbat = 34; // low battery led (red)
const int buzzer = 35; // buzzer
const int orange = 38; // orange leds
const int headlight = 39; // white leds

const int  TRIG = 36;
const int  ECHO = 37;

uint8_t XNewPos =  110;
uint8_t YNewPos =  146;
uint8_t ZNewPos =  100;

int Xcounter = 0;
int Ycounter = 0;
int Zcounter = 0;

int XOldPos;
int YOldPos;
int ZOldPos;

long previousMillis1 = 0;
long previousMillis2 = 0;
long previousMillis3 = 0;

const int intervalg = 50;

Ultrasonic SRF6A(TRIG, ECHO);

int joystick[5];    // Array with 2 analog values (X,Y-axis) and one digital (Z-axis) from transmitter/joystick
float rover[5];   // Array with the 4 motor rover values, to sent to remote control

int apin[] = {
  A1, A2, A3, A4 };  // analog pin array

long previousMillis = 0;

const int interval = 100;

void setup() {
  pinMode(DirM1, OUTPUT);  // direction motor 1, left front
  pinMode(DirM2, OUTPUT);  // direction motor 2, left rear
  pinMode(DirM3, OUTPUT);  // direction motor 3, right front
  pinMode(DirM4, OUTPUT);  // direction motor 4, right rear
  pinMode(PWMM1, OUTPUT);  // PWM motor 1
  pinMode(PWMM2, OUTPUT);  // PWM motor 2
  pinMode(PWMM3, OUTPUT);  // PWM motor 3
  pinMode(PWMM4, OUTPUT);  // PWM motor 4
  pinMode(lowbat, OUTPUT); // low battery led (red)
  pinMode(buzzer, OUTPUT); // buzzer
  pinMode(orange, OUTPUT); // orange leds
  pinMode(headlight, OUTPUT); // white leds

  radio.begin();
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1,pipes[0]);
  radio.startListening();

  bottomservo.attach(9);     // attaches the servo on pin 9 to the servo object
  shouldservo.attach(10);   // attaches the servo on pin 10 to the servo object
  clawservo.attach(11);   // attaches the servo on pin 11 to the servo object
}

void loop() {

  rover[4] = analogRead(A0) /102.3;

  if ( radio.available() )
  {
    // Dump the payloads until we've gotten everything
    bool done = false;
    while (!done)
    {
      // Fetch the payload, and see if this was the last one.
      done = radio.read( &amp;joystick, sizeof(joystick) );
    }
    if (rover[4] &gt; 7.4)
    {
      if(joystick[2] == LOW)       // 760 / 1023 * 5 * 2 = 7,4V from battery
      {

        digitalWrite(headlight, HIGH); // turn on headlights

        if(SRF6A.Ranging(CM) &lt; 5){
          digitalWrite(buzzer, HIGH);      // we're going to crash
        }
        else {
          digitalWrite(buzzer, LOW);
        } 

        if (joystick[0] &gt; 490 &amp;&amp; joystick[0] &lt; 510 &amp;&amp; joystick[1] &gt; 490 &amp;&amp; joystick[1] &lt; 510 )    // joystick is centered
        {
          stopped();
        }

        if (joystick[1] &gt;= 505 &amp;&amp; joystick[0] &gt; 490 &amp;&amp; joystick[0] &lt; 510)               // joystick forward = all motors forward
        {
          bs = (map(joystick[1], 510, 1023, 30, 250));
          backward(bs);
        }

        if (joystick[1] &lt;= 490 &amp;&amp; joystick[0] &gt; 490 &amp;&amp; joystick[0] &lt; 510)               // joystick backward = all motors backward
        {
          fs = (map(joystick[1], 490, 0, 30, 250));
          forward(fs);
        }

        if (joystick[0] &lt;= 490 &amp;&amp; joystick[1] &gt; 490 &amp;&amp; joystick[1] &lt; 510)              // joystick left = left motors backward &amp;&amp; right motors forward
        {
          rs = (map(joystick[0], 490, 0, 30, 250));
          turnright(rs);
        }

        if (joystick[0] &gt;= 510 &amp;&amp; joystick[1] &gt; 490 &amp;&amp; joystick[1] &lt; 510)              // joystick right = left motors forward &amp;&amp; right motors backward
        {
          ls = (map(joystick[0], 510, 1023, 30, 250));
          turnleft(ls);
        }
      }
      else {
        stopped();
        digitalWrite(headlight, LOW); // turn off headlights
        digitalWrite(orange, HIGH);

        if ( joystick[3] == LOW &amp;&amp; ZNewPos &lt; 169)
        {
          Zcounter+=2;
          ZNewPos = ZNewPos + Zcounter;
        }

        if ( joystick[4] == LOW &amp;&amp; ZNewPos &gt; 92)
        {
          Zcounter+=2;
          ZNewPos = ZNewPos - Zcounter;
        }

        if (joystick[0] &gt; 600 &amp;&amp; XNewPos &lt; 180)
        {
          Xcounter+=3;
          XNewPos = XNewPos + (Xcounter);
        }

        if (joystick[0] &lt; 400 &amp;&amp; XNewPos &gt; 30)
        {
          Xcounter+=3;
          XNewPos = XNewPos - (Xcounter);
        }

        if (joystick[1] &lt; 400 &amp;&amp; YNewPos &lt; 174)
        {
          Ycounter+=3;
          YNewPos = YNewPos + (Ycounter);
        }

        if (joystick[1] &gt; 600 &amp;&amp; YNewPos &gt; 50)
        {
          Ycounter+=3;
          YNewPos = YNewPos - (Ycounter);
        }

        unsigned long currentMillis1 = millis();

        if(XOldPos != XNewPos &amp;&amp; currentMillis1 - previousMillis1 &gt; intervalg) {
          previousMillis1 = currentMillis1;
          Xcounter = 0;
          XOldPos = XNewPos;
          if(XNewPos &lt; 30)
          {
            XNewPos = 30;
          }
          if(XNewPos &gt; 180)
          {
            XNewPos = 180;
          }
          bottomservo.write(XNewPos);     // tell servo to go to position in variable 'XNewPos'
        } 

        unsigned long currentMillis2 = millis();

        if(YOldPos != YNewPos &amp;&amp; currentMillis2 - previousMillis2 &gt; intervalg) {
          previousMillis2 = currentMillis2;
          Ycounter = 0;
          YOldPos = YNewPos;
          if(YNewPos &lt; 50)
          {
            YNewPos = 50;
          }
          if(YNewPos &gt; 174)
          {
            YNewPos = 174;
          }
          shouldservo.write(YNewPos);     // tell servo to go to position in variable 'YNewPos'
        }

        unsigned long currentMillis3 = millis();

        if(ZOldPos != ZNewPos &amp;&amp; currentMillis3 - previousMillis3 &gt; intervalg) {	    // Issue command only if desired position changes
          previousMillis3 = currentMillis3;
          Zcounter = 0;
          ZOldPos = ZNewPos;

          // Set servo in degrees from approximately 0 to 180
          if(ZNewPos &lt; 92)
          {
            ZNewPos = 92;
          }
          if(ZNewPos &gt; 169)
          {
            ZNewPos = 169;
          }
          clawservo.write(ZNewPos);     // tell servo to go to position in variable 'ZNewPos'
        }
      }
    }
  }

  unsigned long roverMillis = millis();
  if(roverMillis - previousMillis &gt; interval) {
    previousMillis = roverMillis;   

    radio.stopListening();
    bool ok = radio.write( &amp;rover, sizeof(rover) );
    radio.startListening();
  }
  // 4 analog readings from motor controller = motor rover
  for (int i=0; i&lt;4; i++)
  {
    rover[i] = analogRead( apin[i] )/ 1023.0 * 5.0;
  }
}

void stopped()
{
  analogWrite(PWMM1, 0);
  analogWrite(PWMM2, 0);
  analogWrite(PWMM3, 0);
  analogWrite(PWMM4, 0);
  digitalWrite(orange, LOW);
}

void forward(int fs)
{
  digitalWrite(DirM1, HIGH);
  digitalWrite(DirM2, HIGH);
  digitalWrite(DirM3, HIGH);
  digitalWrite(DirM4, HIGH);
  analogWrite(PWMM1, fs);
  analogWrite(PWMM2, fs);
  analogWrite(PWMM3, fs);
  analogWrite(PWMM4, fs);
  digitalWrite(orange, LOW);
}

void backward(int bs)
{
  digitalWrite(DirM1, LOW);
  digitalWrite(DirM2, LOW);
  digitalWrite(DirM3, LOW);
  digitalWrite(DirM4, LOW);
  analogWrite(PWMM1, bs);
  analogWrite(PWMM2, bs);
  analogWrite(PWMM3, bs);
  analogWrite(PWMM4, bs);
  digitalWrite(orange, LOW);
}

void turnleft(int ls)
{
  digitalWrite(DirM1, LOW);
  digitalWrite(DirM2, LOW);
  digitalWrite(DirM3, HIGH);
  digitalWrite(DirM4, HIGH);
  analogWrite(PWMM1, ls);
  analogWrite(PWMM2, ls);
  analogWrite(PWMM3, ls);
  analogWrite(PWMM4, ls);
  digitalWrite(orange, HIGH);
}

void turnright(int rs)
{
  digitalWrite(DirM1, HIGH);
  digitalWrite(DirM2, HIGH);
  digitalWrite(DirM3, LOW);
  digitalWrite(DirM4, LOW);
  analogWrite(PWMM1, rs);
  analogWrite(PWMM2, rs);
  analogWrite(PWMM3, rs);
  analogWrite(PWMM4, rs);
  digitalWrite(orange, HIGH);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bajdi.com/playing-with-dagu-rover-5-with-gripper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dimming power leds with Arduino</title>
		<link>http://www.bajdi.com/dimming-power-leds-with-arduino/</link>
		<comments>http://www.bajdi.com/dimming-power-leds-with-arduino/#comments</comments>
		<pubDate>Thu, 17 May 2012 11:17:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Arduino]]></category>

		<guid isPermaLink="false">http://www.bajdi.com/?p=384</guid>
		<description><![CDATA[I bought a couple of 3W power leds on Ebay recently. These 3 watt power leds have a forward voltage of 3,4V and need 700mA current to shine at their maximum rating. A led is a semiconductor and needs current limiting to make it work. With small leds you just put a small resistor in [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_386" class="wp-caption alignnone" style="width: 610px"><a href="http://www.bajdi.com/wp-content/uploads/2012/05/led-driver.jpg"><img src="http://www.bajdi.com/wp-content/uploads/2012/05/led-driver.jpg" alt="Power led driver" title="Power led driver" width="600" height="488" class="size-full wp-image-386" /></a><p class="wp-caption-text">Power led driver</p></div><br />
I bought a couple of 3W power leds on Ebay recently. These 3 watt power leds have a forward voltage of 3,4V and need 700mA current to shine at their maximum rating. A led is a semiconductor and needs current limiting to make it work. With small leds you just put a small resistor in series with the led to accomplish this. With power leds this is a bit more difficult since you would need a resistor with a high power rating because of the high currents these leds need. You would waste a lot of energy heating up the resistor. I&#8217;m far from an expert in these things but I had a look what I could make with parts I had on hand. With a bit of googling I quickly found a solution to make the leds work with a minimum of parts. I still had to order some big resistors, I got me some 1,8Ohm 2W resistors. To further limit the current I used an LM317 regulator. To dim the led I used a n-channel mosfet, the IRF510. I connected the gate of the mosfet to an Arduino PWM pin so that I could control the brightness of the leds. The IRF510 is not an ideal mosfet to drive with an Arduino, it is not a logic level mosfet. It needs 10V at its gate to fully open. But in this case driving it with an Arduino PWM pin did the trick. The LM317 regulator consumes quite a bit of heat driving the leds so its best that you put a heatsink on it. The leds came fitted to a small aluminium star but they need a bigger heatsink to use them as they get quite hot. I connected a 10K potentiometer to the Arduino and mapped the value of the pot to the PWM pin. So my Arduino is now a led dimmer <img src='http://www.bajdi.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div id="attachment_388" class="wp-caption alignnone" style="width: 630px"><a href="http://www.bajdi.com/wp-content/uploads/2012/05/arduino-power-led.jpg"><img src="http://www.bajdi.com/wp-content/uploads/2012/05/arduino-power-led-1024x680.jpg" alt="Arduino dimming power led" title="Arduino dimming power led" width="620" height="411" class="size-large wp-image-388" /></a><p class="wp-caption-text">Arduino dimming power led</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.bajdi.com/dimming-power-leds-with-arduino/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arduino rc car part 2: controlling the servo</title>
		<link>http://www.bajdi.com/arduino-rc-car-part-2-controlling-the-servo/</link>
		<comments>http://www.bajdi.com/arduino-rc-car-part-2-controlling-the-servo/#comments</comments>
		<pubDate>Fri, 11 May 2012 21:21:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Arduino sketches]]></category>

		<guid isPermaLink="false">http://www.bajdi.com/?p=373</guid>
		<description><![CDATA[Fiddled some more with my old Nikko rc car, last time I managed to control the main motor with a L298n motor controller board. Since my last post about it I found out that the servo that originally controlled the front wheels is broken. So I needed to find a solution for that. I had [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_374" class="wp-caption alignnone" style="width: 630px"><a href="http://www.bajdi.com/wp-content/uploads/2012/05/arduino-rc-car.jpg"><img src="http://www.bajdi.com/wp-content/uploads/2012/05/arduino-rc-car-1024x680.jpg" alt="Controlling an old rc car with Arduino" title="Arduino rc car" width="620" height="411" class="size-large wp-image-374" /></a><p class="wp-caption-text">Controlling an old rc car with Arduino</p></div>
<p>Fiddled some more with my old Nikko rc car, last time I managed to control the main motor with a L298n motor controller board. Since my last post about it I found out that the servo that originally controlled the front wheels is broken. So I needed to find a solution for that. I had a small Tower Pro SG90 servo and decided to fit that. With the use of my Dremel, tie wrap, a small bolt and pieces of carton I succeeded in mounting the servo so it can turn the front wheels <img src='http://www.bajdi.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  To be honest it took me a couple of hours and a lot of swearing to get it right.<br />
So after mounting the servo it was time to connect it all to an Arduino. I connected the L298n board, SG90 servo and joystick to an Arduino Duemilanove to test if everything worked like it should. I used my lab power supply to power everything, I gave 7-8V to the L298n and used a L7805CV to give the servo 5V, I think 7-8V is a bit to much for these little servos. I actually first tried to power the servo from the Arduino, but it would just reset every time I tried to move the servo.<br />
I made the following sketch that lets me control the main motor and front wheels with an analog joystick. Next step is to control it wireless with my self made remote control using a pair of nRF24L01 modules.</p>
<pre class="brush: arduino; title: ; notranslate">
/*

http://www.bajdi.com

 RC car controlled by analog joystick
 Micro SG90 servo to turn front wheels
 DC motor controlled by L298n
 */

#include &lt;Servo.h&gt;

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 &lt; 500)              // joystick forward
  {
    fspeed = (map(Y, 501, 0, 70, 250));
    forward(fspeed);
  }

  if (Y &gt; 540)              // joystick backward
  {
    bspeed = (map(Y, 541, 1023, 70, 250));
    backward(bspeed);
  }
  if (Y &gt;= 500 &amp;&amp; Y &lt;= 540)    // joystick is centered
  {
    stop();
  }

  if (X &gt; 490 &amp;&amp; X &lt; 530 )    // joystick is centered
  {
    iNewPos = 87;     // 87 = servo in center position
  }

  if (X &lt; 490)
  {
    iNewPos = (map(X, 491, 0, 88, 130));
  }

  if (X &gt; 530)
  {
    iNewPos = (map(X, 531, 1023, 86, 35));
  }

  unsigned long currentMillis = millis();

  if(iOldPos != iNewPos &amp;&amp; currentMillis - previousMillis &gt; 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);
}
</pre>

<a href='http://www.bajdi.com/arduino-rc-car-part-2-controlling-the-servo/arduino-rc-car/' title='Arduino rc car'><img width="150" height="150" src="http://www.bajdi.com/wp-content/uploads/2012/05/arduino-rc-car-150x150.jpg" class="attachment-thumbnail" alt="Controlling an old rc car with Arduino" title="Arduino rc car" /></a>
<a href='http://www.bajdi.com/arduino-rc-car-part-2-controlling-the-servo/arduino-rc-car2/' title='Arduino RC car'><img width="150" height="150" src="http://www.bajdi.com/wp-content/uploads/2012/05/arduino-rc-car2-150x150.jpg" class="attachment-thumbnail" alt="Arduino RC car: SG90 servo and L298n" title="Arduino RC car" /></a>
<a href='http://www.bajdi.com/arduino-rc-car-part-2-controlling-the-servo/arduino-rc-car3/' title='Arduino RC car'><img width="150" height="150" src="http://www.bajdi.com/wp-content/uploads/2012/05/arduino-rc-car3-150x150.jpg" class="attachment-thumbnail" alt="Arduino RC car: SG90 servo and L298n" title="Arduino RC car" /></a>

]]></content:encoded>
			<wfw:commentRss>http://www.bajdi.com/arduino-rc-car-part-2-controlling-the-servo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Controlling a servo without the delay function</title>
		<link>http://www.bajdi.com/controlling-a-servo-without-the-delay-function/</link>
		<comments>http://www.bajdi.com/controlling-a-servo-without-the-delay-function/#comments</comments>
		<pubDate>Sun, 06 May 2012 14:26:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Arduino sketches]]></category>

		<guid isPermaLink="false">http://www.bajdi.com/?p=367</guid>
		<description><![CDATA[I started writing a sketch to control my 3 DOF gripper, it has 3 servos controlled by an Arduino. All the servo example sketches that I have seen use the delay function. You need to have a delay after sending the servo the command to move to a certain position. The delay function is the [...]]]></description>
			<content:encoded><![CDATA[<p><iframe src="http://player.vimeo.com/video/41647626" width="600" height="337" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></p>
<p>I started writing a sketch to control my 3 DOF gripper, it has 3 servos controlled by an Arduino. All the servo example sketches that I have seen use the delay function. You need to have a delay after sending the servo the command to move to a certain position. The delay function is the easiest way to do this, but this stops the code running on the Arduino. You can not do anything else during that delay. When you have a lot of delays in your program you will end up with a very unresponsive program. This is an issue I have seen many times on the Arduino forum. As I will use a remote control to move the servos I want instant reaction of my servos. So I had to come up with a sketch to control a servo without the delay function. To do that you can use the millis function instead, this is a timer that runs continuously on the Arduino. The Arduino IDE comes with the &#8220;blink without delay&#8221; example, this example uses the millis function to blink a LED. So I integrated this example with a servo example I found on the Arduino forum. To test it I connected the 3 servos of my gripper to my spare Arduino Mega 2560 together with 3 potentiometers. I powered the 3 servos from my lab power supply. Don&#8217;t try to power servos directly from an Arduino, the regulator on the Arduino is not powerful enough. You can control many servos with an Arduino but not power them. Always make sure you have a common ground if you&#8217;re using a different power supply to power the Arduino and servos. For example when you use a battery to power the servos and use an USB cable to power the Arduino you must connect the ground of the Arduino with the ground of the battery. Else your servos won&#8217;t be working or will do funny things.<br />
Here is the sketch I have come up with to control one servo with a potentiometer without using the delay function:</p>
<pre class="brush: arduino; title: ; notranslate">
/*

http://www.bajdi.com

 Moving a servo without the delay function.
 The servo is controlled by a potentiometer connected to A0.
*/

#include &lt;Servo.h&gt;

Servo myservo;               // create servo object to control a servo
int potValue;                //variables to hold A0 input
int iOldPos, iNewPos = 0;    // servo position

long previousMillis = 0;
const int interval = 20;

int pot = 0; 	//potentiometer connected to A0

void setup()
{
  //Initialize serial port
  Serial.begin(9600);
  Serial.println(&quot;Servo Demo&quot;);

  // Initialize servo
  myservo.attach(9);   // attaches the servo on pin 9
}

void loop()
{
  potValue = analogRead(pot);       // read the value of the potentiometer
  iNewPos = potValue/5;		    // convert to quasi-degrees (alter per your potentiometer)

  unsigned long currentMillis = millis();

  if(iOldPos != iNewPos &amp;&amp; currentMillis - previousMillis &gt; interval) {	    // Issue command only if desired position changes
    previousMillis = currentMillis;
    iOldPos = iNewPos;
    Serial.print(&quot;Potentiometer = &quot;);         // Value of potentiometer on serial port
    Serial.print(potValue);
    Serial.print(&quot;, ~degrees = &quot;);
    Serial.println(iNewPos);

    // Set servo in degrees from approximately 0 to 180
    myservo.write(iNewPos);     // tell servo to go to position in variable 'iNewPos'
  }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bajdi.com/controlling-a-servo-without-the-delay-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rover 5 with gripper</title>
		<link>http://www.bajdi.com/rover-5-with-gripper/</link>
		<comments>http://www.bajdi.com/rover-5-with-gripper/#comments</comments>
		<pubDate>Sun, 06 May 2012 10:10:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Rover 5]]></category>

		<guid isPermaLink="false">http://www.bajdi.com/?p=364</guid>
		<description><![CDATA[I had some bad luck recently, the voltage regulator on my Dagu Red back spider robot controller died The mcu on the board still works when I power it from USB but when I use external power the regulator only gives 3V it should be 5V. So I needed a new one, the European suppliers [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_361" class="wp-caption alignnone" style="width: 630px"><a href="http://www.bajdi.com/wp-content/uploads/2012/05/dagu-rover-5-gripper.jpg"><img src="http://www.bajdi.com/wp-content/uploads/2012/05/dagu-rover-5-gripper-1024x680.jpg" alt="Dagu rover 5 with gripper" title="Dagu rover 5 with gripper" width="620" height="411" class="size-large wp-image-361" /></a><p class="wp-caption-text">Dagu rover 5 with gripper</p></div>
<p>I had some bad luck recently, the voltage regulator on my Dagu Red back spider robot controller died <img src='http://www.bajdi.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  The mcu on the board still works when I power it from USB but when I use external power the regulator only gives 3V it should be 5V. So I needed a new one, the European suppliers seemed to be all out of stock. I contacted Dagu in China and they sold me a new one. Dagu also sells a nice 2 DOF gripper and I couldn&#8217;t resist and ordered one. The parts arrived here pretty fast, courtesy of DHL. What was I going to do with the 2 DOF gripper? I needed to make a base for it or maybe mount it on my Rover? Yesterday I took my Dremel and made a hole in the plexi glass I have on my Rover chassis. So now my Rover 5 is equipped with a 3 DOF gripper. The gripper comes with 2 servos (2 DOF), I added another servo to the base so I can rotate the gripper, that makes it 3 DOF. The one I currently use has plastic gears and horn, it&#8217;s not strong enough to hold the gripper. But it&#8217;s good enough to start testing, I&#8217;ll order a stronger servo with metal gears and metal horn. I&#8217;m working on a sketch to control the Rover 5 and gripper with my new remote control. That will keep me busy for a while <img src='http://www.bajdi.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div id="attachment_362" class="wp-caption alignnone" style="width: 630px"><a href="http://www.bajdi.com/wp-content/uploads/2012/05/dagu-rover5-remote-gripper.jpg"><img src="http://www.bajdi.com/wp-content/uploads/2012/05/dagu-rover5-remote-gripper-1024x680.jpg" alt="Dagu rover 5 with gripper and remote control" title="Dagu rover 5 with gripper and remote control" width="620" height="411" class="size-large wp-image-362" /></a><p class="wp-caption-text">Dagu rover 5 with gripper and remote control</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.bajdi.com/rover-5-with-gripper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

