Pages: 1 2 [3]   Go Down
Author Topic: Arduino beginner  (Read 2102 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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()
{  

Serial.begin(9600);

    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
delayMicroseconds(20);
digitalWrite(ultrasoundtriggerpin, LOW);
pinMode(ultrasoundtriggerpin, INPUT);
delay(1000);

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'
M3.write(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'
M3.write(angle);
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)
« Last Edit: May 04, 2012, 12:35:27 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24433
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Here,
Code:
      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.
« Last Edit: May 04, 2012, 01:49:09 am by AWOL » Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46218
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4029
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Examples can be found in your IDE.

Pages: 1 2 [3]   Go Up
Jump to: