help

hi i know basic electronics but wish to start using arduino to to start work on building a robot. how i plan to do this is in stages and learn as i go like the the first stage is to get it to go foward and backwards then to add sensor inputs to control the action of the motors like distance sensors and movement please i would like any input from anybody like examples of code i could use and adapt for my use thanks

Look at these pictures of robots and tell us which is closest to the one you want to build:
https://www.google.com/search?q=robot+arduino&source=lnms&tbm=isch&sa=X

(Please try to use capitalization and punctuation. It should make your writing easier to understand.)

Basically it's going to be a object avoiding robot. So I was thinking of using a 3v DC motor for the forward and reverse action and using a servo motor to adjust the direction when the sensor detects something. But I would also like to add different functions to it as I go along . Thanks for sparing your valuable time and as I'm just starting it's very much appreciated

How about your ability to write working C code? Arduino IDE uses C/C++.

If you're not up to knowing variable types well or using arrays and the basic logic structures (if, for-loop, while, switch-case) then you can waste a lot more hours typing huge kludges to do simple things than it takes to learn the simple better ways first.

Yes writing the code will b hard at first I appreciate that but I grew up using old basic on the old home computers when there wasn't Google or YouTube like nowadays so loops and using variables aren't that alien to me what I don't know I'll find on the internet or help from people like yourselves,plus I've started learning python on the raspberry pi and also c and c+ aren't new I just would like some pointers to help me research what I need to do to get this on my way.

const int trig = 5;
const int echo = 6;
const int leftForward = 8;
const int leftBackward = 7;
const int rightForward = 4;
const int rightBackward = 3;

int duration = 0;
int distance = 0;

void setup()
{
pinMode(trig , OUTPUT);
pinMode(echo , INPUT);
pinMode(leftForward , OUTPUT);
pinMode(leftBackward , OUTPUT);
pinMode(rightForward , OUTPUT);
pinMode(rightBackward , OUTPUT);
Serial.begin(9600);

}

void loop()
{
digitalWrite(trig , HIGH);
delayMicroseconds(1000);
digitalWrite(trig , LOW);

duration = pulseIn(echo , HIGH);
distance = (duration/2) / 28.5 ;
Serial.println(distance);

if ( distance < 20 )
{
digitalWrite(leftForward , LOW);
digitalWrite(leftBackward , HIGH);
digitalWrite(rightForward , HIGH);
digitalWrite(rightBackward , LOW);
delay(100);
}
else
{
digitalWrite(leftForward , HIGH);
digitalWrite(leftBackward , LOW);
digitalWrite(rightForward , HIGH);
digitalWrite(rightBackward , LOW);
}
}

I could use this as this does what i want i plan to study the code and learn what each command does then learn to adapt the code as i go along. Dont mind making mistakes as long as i know where i went wrong

That code is a good starting point for a two-wheel robot with two DC motors and a motor controller. Something like this:
http://www.ebay.com/itm/Car-Chassis-Kit-Smart-Robot-Speed-Control-Encoder-Battery-Box-2WD-For-Arduino-/252827632581

thank you for your reply. Im going to learn what each part does then add to it once ive learned its function. Understanding how the program works is not a problem.Learning c and cc+ is the hardest part i can see due to i have hardly ever used it before as i have mentioned i know basic and assembly but im willing to put the effort in to learn c and cc+. That is what i have looking at for my base chassis it looks just what i need plus i have also been looking at some ultrasound sensors as well

chrislittler1234:
Yes writing the code will b hard at first I appreciate that but I grew up using old basic on the old home computers when there wasn't Google or YouTube like nowadays so loops and using variables aren't that alien to me what I don't know I'll find on the internet or help from people like yourselves,plus I've started learning python on the raspberry pi and also c and c+ aren't new I just would like some pointers to help me research what I need to do to get this on my way.

I started writing line-number interpreter (as opposed to compiled) Basic for pay in 1980. IIRC in 1983 we got CB80, a compiled Basic that ran like lightning compared. Those computers we had were equal to Z80 systems of the time, the Sinclair ZX80 was a stripped-down version of those.

In your IDE is a whole series of Example sketches. For you each won't take an hour unless you get interested in an idea to explore which is a Good Thing as the practice will help you knock rust off and sharpen your own edges. Go through some or all of sections 1, 2, 3 and 5 with section 5 covering logic structures and arrays specifically.
Avoid section 4. It teaches using C++ String objects that while being fine on PC's with loads of RAM, can be done at all on limited-memory microcontrollers but have bad behaviors (ie: do #$&@ behind your back) that C char-array strings do not.

On the Arduino site is the Arduino Reference page that you want to bookmark. The green bar at the top of your forum page has a LEARNING popdown menu with the Reference as the bottom link. You also want to find and bookmark the Foundations page with links to understanding many specifics about Arduino and the chips used.

When I get into a project, I open a browser and then tabs in that, one for every page/site I may need to look in just to make sure I have the right syntax and use. At over 60 I need my reminders more than ever!

I'll have a better look at your code though I see you not using array where it could save you lines of code and I notice that you use 2 byte int variables where 1 byte byte variables would do. You need to have a line

#include "Arduino.h"

to make the compiler see "byte" as a variable type, otherwise "uint8_T" is a type name that does the same thing.

Arduino Uno has 2048 bytes RAM for the heap and stack. If you practice using tight code it will put off the day when your project doesn't run on the chip. There are a few RAM-saving techniques to learn (besides don't use String or any other C++ Container Class) like printing constant text (labels, prompts, help messages) from flash (default is they all load to RAM at the start) and we have the F() macro to make that easy since IDE 1.0 or about. Using good methods the Uno can run sketches that do a magnitude more than not using good methods. You might be able to learn most of them well in less than 6 months.

And I see you've done some assembly code. Knowing how the chips work at the bottom level is a huge PLUS.

Consider that the AVR core cpu has 32 general purpose registers and all the port and hardware registers right there to use, accessible to Arduino code. The GP registers are all orthagonal, can be used as accumulators, address registers, index registers, etc. They need to be, the AVR Instruction Set is a bit sparse.

You might want to get a copy of the Complete datasheet for whatever chip is on your Arduino board. Here is the 328P:
http://www.microchip.com/wwwproducts/en/ATmega328P
You may have to click the Documentation link.

const int trig = 5;
const int echo = 6;
const int leftForward = 8;     
const int leftBackward = 7;
const int rightForward = 4;
const int rightBackward = 3;

int duration = 0;
int distance = 0;

void setup() 
{ 
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
  pinMode(leftForward , OUTPUT);
  pinMode(leftBackward , OUTPUT);
  pinMode(rightForward , OUTPUT);
  pinMode(rightBackward , OUTPUT);
  Serial.begin(9600);

}

void loop() 
{
  digitalWrite(trig , HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig , LOW);

  duration = pulseIn(echo , HIGH);
  distance = (duration/2) / 28.5 ;
  Serial.println(distance);

  if ( distance < 20 )
  { 
    digitalWrite(leftForward , LOW);
    digitalWrite(leftBackward , HIGH);
    digitalWrite(rightForward , HIGH);
    digitalWrite(rightBackward , LOW);
    delay(100); 
  }
  else 
  { 
    digitalWrite(leftForward , HIGH);
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward , HIGH);
    digitalWrite(rightBackward , LOW);
  }
}

Hit the quote button to see how I got the code into a forum code box. You should do that here.

In section 2 of the Examples sketches in your IDE is the BlinkWithoutDelay lesson at its most basic.
There is a very well made commonsense level tutorial on that here:

and more, ways to read incoming serial data without blocking (what delay does) here:

You need never use delay() again once you know this! Your code can do amazingly more when it doesn't block.
The techniques will let your code expand more freely by far and can serve greatly to avoid becoming spaghetti code.

OOPS! I forgot why I put your code here!
It's simple. Those 4 motor pins you use, if you put them all on the same same port then you can change all 4 at once as bits on that port using direct port manipulation, though you might have to write the port DDR (data direction) and PORTx registers using 2 commands in less than 1 usec.

chrislittler1234:
thank you for your reply. Im going to learn what each part does then add to it once ive learned its function. Understanding how the program works is not a problem.Learning c and cc+ is the hardest part i can see due to i have hardly ever used it before as i have mentioned i know basic and assembly but im willing to put the effort in to learn c and cc+. That is what i have looking at for my base chassis it looks just what i need plus i have also been looking at some ultrasound sensors as well

Real Basic is torture compared to C!
Every Basic compiler I've used or seen since 83 have progressively was more and more C-like, especially M$ VB.

You can insert assembly into C and Arduino but TBH the compiler does a pretty good optimize and porting to an ARM duino would need that much more rewriting.

Hit the quote button to see how I got the code into a forum code box

We'd rather you used the code tags button "</>"

It's nice to know them as well if you use Quick Reply.

Note that everyone on this forum, who helps out with some code, writes also proper English. Correct punctuation, capitals when required, no shortened words like u, r...

Errrrrrr..................... not completely but expect that long-time coders to pay more attention to detail.

I know that my use of the language is nothing like perfect.