Help with programming

Hi there,

I would like to know if arduino programming supports other user-defined structures apart from setup and loop because i only see these two under structure in arduino webpage?

Yes. Setup and loop are just two C++ functions called by the the start-up framework. Anything else you add needs to be called by your own code or attached to something that will call it.

Korman

Neither "setup" nor "loop" are structures, they are functions.

IMHO, the header "Structure" here: http://arduino.cc/en/Reference/HomePage is incorrect or at best insufficient - it should at least say "Program structure"

So for eg. in this following program, i only want the distance sensor to be in void loop function and don’t want the motor to restart after every loop, do i need to define new function for motor which is outside void loop ? Can i give it any function name as i want like in C++?

#include <Servo.h>

Servo myservo;

void setSpeed( int speed)
{
int angle= map(speed,0,100, 0,90);
myservo.write (angle);
}

void setup()
{
Serial.begin(115200);
myservo.attach(9);

}

void loop ()
{
int IRpin = 1;
int val = 0;
int speed;

Serial.println(“Arming for 5 seconds and Accelerating”);
for (speed=65; speed<100; speed+=1)
{
setSpeed (speed);
Serial.println("Speed is ");
Serial.println(speed);
delay (1000);
}

setSpeed(150);
Serial.println(“Maintaining the speed”);
delay(1000);

val= analogRead(IRpin);
long distance = 9462/(val-16.92);// converting sensor value to distance
Serial.println ("The distance is ");
Serial.println (distance);
delay (1000);

if ( distance > 30 && distance < 50)
{
setSpeed(150);
Serial.println(" Speed is maximum. ");
delay(10000);
}
else
{
setSpeed(0);
Serial.println(“Exceed the limit-The motor has come to a stop”);
delay(10000);
}

}

Thanks a lot if you can help.

PKW

do i need to define new function for motor which is outside void loop ?

Maybe, maybe not. It isn't really clear what you want to do.

Can i give it any function name as i want like in C++?

You are programming the Arduino in C++, so, yes, you can name the function anything you want. Well, almost. There are some reserved keywords, like int and float, and existing functions, like loop and setup and init. You can not re-use any of those names.

Once you create a function, you have to call it from somewhere. If you call it from setup(), it runs once. If you call it from loop(), unconditionally, it is called every time loop is, which could be millions of times a second.

i only want the distance sensor to be in void loop function and don't want the motor to restart after every loop,

So, don't call that code every time.

The code you posted could use some comments, that illustrate that you know what it is doing.

Hi PaulS,

Thanks a lot for the reply. I came to know i can put my code in the setup function other than declaring the pin no. after i have read your comment. ( How noob i am , haha. >.< ) I just put the part of the code for motor under setup function and i kinda get what i want the motor and sensor to do.

I am sorry if my writing confuses you.

Thanks again for your help. :slight_smile:

PKW