Go Down

Topic: Arduino beginner (Read 6087 times) previous topic - next topic



May 04, 2012, 07:15 am Last Edit: May 04, 2012, 07:35 am by AWOL Reason: 1
hello everyone,
im writing my code now to control the dc motors based on the feedback received form sensors,
this is my initial code, and whenever i verify it in arduino ide , i get an error message saying
''a function-definition is not allowed here before '{' token

Code: [Select]
int ultrasoundsignalpins[]={0,1,2,3,4,5}; //6ultrasound signla pins

int ultrasoundtriggerpin=6; // rx pin, the trigger pin

char *sensorstring[]={"left: " , "  frontleft: " , "  front 1: " , "  front 2: " , "  frontright: " , "  Right: "};// (NOT NESSECARY)

float obstacle =constrain(obstacle, 0, 50);

float notobstacle =constrain(notobstacle, 51, 100);

#include <Servo.h>

Servo M1;
Servo M2;
Servo M3;
Servo M4;

int angle = 0;

void setup()


   for(int i=0; i<6; i++)

pinMode(ultrasoundsignalpins[i],INPUT);//set ultrasoundsignalpins as input}
 pinMode(ultrasoundtriggerpin,OUTPUT);//set rx pin as output
 delay(250);// time to boot up
digitalWrite(ultrasoundtriggerpin, HIGH);//send rx pin high for at least 20microsecs
digitalWrite(ultrasoundtriggerpin, LOW);
pinMode(ultrasoundtriggerpin, INPUT);

M1.attach(5); // attaches the servo M1 on pin 5 to the servo object.
M2.attach(9); // attaches the servo M2 on pin 9 to the servo object.
M3.attach(10); // attaches the servo M3 on pin 10 to the servo object.
M4.attach(11); // attaches the servo M4 on pin 11 to the servo object.


void loop()

for(int i=0; i<6; i++)

float sensor1 = analogread(ultrasoundsignalpins[0]) * (254.0/1024.0) * 2.54;// reads distances in cm
float sensor2 = analogread(ultrasoundsignalpins[1]) * (254.0/1024.0) * 2.54;//
float sensor3 = analogread(ultrasoundsignalpins[2]) * (254.0/1024.0) * 2.54;//
float sensor4 = analogread(ultrasoundsignalpins[3]) * (254.0/1024.0) * 2.54;//
float sensor5 = analogread(ultrasoundsignalpins[4]) * (254.0/1024.0) * 2.54;//
float sensor6 = analogread(ultrasoundsignalpins[5]) * (254.0/1024.0) * 2.54;//

        do    {

analogread(ultrasoundsignalpins[i]);//reads sensors first

while (sensor1 == obstacle; sensor2 == notobstacle, sensor3 == notobstacle, sensor4 == notobstacle, sensor5 == notobstacle, sensor6 == notobstacle)

//then run OP I first command only
for(angle = 0; angle < 180; angle += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
M1.write(angle); // tell servo to go to position in variable 'angle'
delay(20); // waits 20ms between servo commands
for(angle = 180; angle >= 1; angle -= 1) // goes from 180 degrees to 0 degrees
M1.write(angle); // tell servo to go to position in variable 'pos'
delay(20); // waits 20ms between servo commands



Can someone help me fix it pls
this is the code , and when the message appears, the pointer stops after Void loop()    { <=(it highlight this bracket)


May 04, 2012, 07:37 am Last Edit: May 04, 2012, 08:49 am by AWOL Reason: 1
analogread  != analogRead

Your "while" condition needs a rework.

If you'd tried the auto-format tool in the IDE before you posted your code (hint), it would have told you part of the problem with your code.

Edit: I took a longer look through.
It looks like you've got some pretty major misconceptions about how things work in C (e.g. "constrain" must be called every time you want to constrain a value; it doesn't automagically bind to a variable and operate on it whenever you reference that value).
When I said earlier to iterate through an array of pin numbers, I didn't mean to set up a for loop, then execute the exact same code for each of the array members explicity - that's why we have for loops.
Your "do..while" looks like you don't understand how to combine conditions, and doesn't make any sense.

Code: [Select]
      for(angle = 0; angle < 180; angle += 1) // goes from 0 degrees to 180 degree the code does not match the comment. This is no big deal, because the compiler ignores the comment, but it is best to try to get the comment to describe the action, or you may as well miss it out altogether.

I'd recommend you take a few days going through some tutorial and examples, and working through them.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


Just a quick comment, since you mentioned you were using Sabertooth motor drivers.
Be sure to use servo.attach(pin, 1000, 2000); instead of just servo.attach(pin);

If you use the 1000, 2000 variation, you can use angles from 0 to 180 like a normal servo.
If you don't it's more like 40 to 140 (the Arduino uses a nonstandard, wide range for servo -- using
the three argument version fixes that).


Code: [Select]
float obstacle =constrain(obstacle, 0, 50);

float notobstacle =constrain(notobstacle, 51, 100);

Please explain what you think this code is doing. It isn't, but I'd like to hear what you think it is doing.
The art of getting good answers lies in asking good questions.


i was trying to define a variable called obstacle which have a number range between 0 and 50,
to be more clear with you i wanted to say that if eg sensor1 read a value between 0 and 50 then that is an obstacle.

so i was thinking that creating defining an obstacle using this constrain and equating to the sensor was gona work but apparently it doesnt,

could anyone give me a more easier way to do this, please as if I've spent the last 5 hours trying to figure how to debug this problem and now im frustrated, i would really appreciate your help


Try spending time finding out how C works instead of why it doesn't fit your ideas of how it works.

Your PC has an FPU, Arduino does not. Don't use floating point unless you really need to.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up