DesignSpark Electrical Logolinkedin
  • Brought to you by
  • RS Components
  • Allied Electronics

April 7, 2017 10:21

Follow

How to build a Arduino Robocar Part 1

I've been meaning to put some content on here for a while and recently I got around to getting an Uno and motor shield. Given I also have access to a 3d printer and have a shedload of microswitches, semis and other goodies knocking around - Robocar is an obvious choice!

I've had a few Arduino's in the past but have always used them as an ISP to write to AVR instead of just embracing the Arduino eco-system. I'm over that nonsense now and will be learning as I go.

The plan

A four-wheeled car type thingy which drives two rear wheels and steers with two front wheels.

For starters, I want to make it go forward until it hits an obstacle detected by a front-left & front-right µswitch-bumper system. At that point, it will reverse/turn around 90 degrees and go forward again.

The parts

(so far)

 

The code

I've hacked together some of the example sketches from the Arduino IDE to get a small motor to run when input A5 on the Uno goes high.

// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!
#include <AFMotor.h>

AF_DCMotor motor(4);
const int buttonPin = A5;     // the number of the pushbutton pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
 
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);}


void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
 // turn on motor
  motor.setSpeed(200);
 
  motor.run(FORWARD);}
else {
  motor.setSpeed(0);
 
  motor.run(RELEASE);
  }
}

It works almost as expected although the motor runs for about 7 seconds then stops. I have no idea why at this stage - Surely the loop isn't taking 7 seconds to run and there's no time value in there. I'd expect it to ONLY run when the pin is high.

I'm off to figure that out and think about the 3d printed body/wheels/steering mechanics.

Stay tuned for part 2.....

Comments/suggestions/things you would like to see me attempt and then explain etc.... Are encouraged so please just shout.

Stuart.

stu.law222 has not written a bio yet…

April 7, 2017 10:21

Follow

Comments

0 Votes
Tony_Swanwick

April 27, 2017 11:32

Good to see Arduino projects appearing here Stu. I do a lot myself and use DesignSpark for modelling. With push switches I would recommend that you use the INPUT_PULLUP option for the digital port, to employ the internal pull-up resistor, and test for an active LOW condition. I suspect your digital input was floating around and picked up random HIGH/LOW states from electrical noise. It would also be useful to include a simple circuit schematic as part of your descriptions.

0 Votes
stu.law222

April 27, 2017 07:46

@thpitsch Thanks! I didn't realise that. I think the problem was my circuit as I'm waiting for the input pin to go high and don't have the pin referenced. In part 2 I talk about pull-ups and it seems to have solved the issue :)

0 Votes
thpitsch

April 27, 2017 07:41

Maybe you have a reset every 7 seconds. This is happening if you are connected to your computer via USB but don't use the serial connection with an open serial monitor. If you initialize the serial connection - even if you don't use it - then it will work without auto-resets.

0 Votes
stu.law222

April 10, 2017 10:37

Thanks Lewis! See you for part two!

0 Votes
lewisswortley

April 10, 2017 10:32

Great article Stuart