Society of Robots - Robot Forum

Software => Software => Topic started by: robocreator on December 28, 2012, 09:57:13 PM

Title: Help Programming Arduino Uno
Post by: robocreator on December 28, 2012, 09:57:13 PM
Hi, I am new here....
I have been working on my robot for at least 1 year now and my programming doesn't seem to work using Arduino. The robot is shown below, it has 2 back servos(left and right), one is in the front(turning the IR sensor), lastly the IR sensor. I've tried a lot of ways from the web, however it doesn't seem to work so I tried changing it and it still doesn't work. I tried coding one at a time, however the servos on the back doesn't seem to respond to the next line by using the sensor. I have save this one copy :
can someone help me with coding my robot using an IR sensor from an Arduino Uno?


#include <Servo.h>
 
Servo myservo1;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
Servo myservo2;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
Servo myservo3;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
 
int pos = 0;    // variable to store the servo position
int SensorPin = 9; // variable connected on pin 9 to the sensor object
 
void setup()
{
  myservo1.attach(5);  // attaches the servo on pin 5 to the servo object
  myservo2.attach(6);  // attaches the servo on pin 6 to the servo object
  myservo3.attach(10);  // attaches the servo on pin 10 to the servo object
  SensorPin = (10, 20, 30, 40, 50, 60, 70, 80);
}
 
 
void loop()

{ // move forward
  for(pos = 0; pos < 10; pos += 1)  // goes from 0 degrees to 10 degrees
                                     // in steps of 1 degree
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  for(pos = 0; pos < 10; pos += 1)  // goes from 0 degrees to 10 degrees
                                     // in steps of 1 degree
 
    myservo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
 
 

 // turn left
  for(pos = 0; pos < 10; pos += 1)  // goes from 0 degrees to 10 degrees
                                     // in steps of 1 degree
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
 
  for(pos = 0; pos > 180; pos -= 1)  // goes from 0 degrees to 10 degrees
                                     // in steps of 1 degree
    myservo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
   
   // front servo
  for(pos = 0; pos < 1; pos += 1)  // goes from 0 degrees to 10 degrees
                                     // in steps of 1 degree
    myservo3.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5);                       // waits 15ms for the servo to reach the position
  for(pos = 0; pos>=1; pos-=1)     // goes from 10 degrees to 0 degrees
                                 
    myservo3.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5);                       // waits 15ms for the servo to reach the position
}
Title: Re: Help Programming Arduino Uno
Post by: jwatte on December 30, 2012, 07:01:36 PM
"It doesn't work" how?
You are attempting to run this code, and what happens, exactly?
Title: Re: Help Programming Arduino Uno
Post by: arigid on December 31, 2012, 02:36:58 PM
Try putting the a bracket inside the for loop , that , if you are using the delay and the servo write command inside it.
A bracket is not required for 1 statement, but is required for more.
 Also, pls let us know the way it is behaving now, and the way you want it to. Check out the Servo code in the examples of the Arduino IDE, there are very simple codes to control servo.
Title: Re: Help Programming Arduino Uno
Post by: robocreator on January 02, 2013, 11:33:03 AM
well, I'm not sure how to describe it but i'll try my best. The front servo just spins clock-wise, and the 5 and the 6th servo spins the opposite way. None of the servos are using the sensor like I hoped. Here is what I want it to do. The servo in the front should go 50 degrees back and forth this allows the IR to scan. Using the Sharp IR, the two back servo should move forward unless the IR detects a closer measurement. Then both servo should move backward for 50 degrees and then left servo spin backward for 90 degrees, then it continues to scan. The curly brackets I have tried doing this
{
   for(pos = 0; pos < 10; pos += 1)  // goes from 0 degrees to 10 degrees
                                     // in steps of 1 degree
  {  myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 0; pos > 180; pos -= 1)  // goes from 0 degrees to 10 degrees
  {                                   // in steps of 1 degree
    myservo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
 }
}

however, it says "expected unqualified-id before '{' token".
I hope this helps describe and explain. Thank You...
Title: Re: Help Programming Arduino Uno
Post by: jwatte on January 02, 2013, 01:42:17 PM
Well, the first problem is that you don't use braces around your for() loop statements. When you write code like this:

Code: [Select]
for (int i = 0; i < 180; ++i)
    something();
    delay(15);

Then only the "something" belongs to the for() loop; the "delay" will only be run once when the for() loop is done.

Try something like this for each for() loop:

Code: [Select]
for (int i = 0; i < 180; ++i) {
    something();
    delay(15);
}
Title: Re: Help Programming Arduino Uno
Post by: robocreator on January 02, 2013, 03:04:13 PM
I tried, thank you, do you know how to fix the sensor. Thanks for your help.
Title: Re: Help Programming Arduino Uno
Post by: briselec on January 02, 2013, 09:25:39 PM
// front servo
  for(pos = 0; pos < 1; pos += 1)  // goes from 0 degrees to 10 degrees
                                     // in steps of 1 degree
    myservo3.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5);                       // waits 15ms for the servo to reach the position
  for(pos = 0; pos>=1; pos-=1)     // goes from 10 degrees to 0 degrees
                                 
    myservo3.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5);                       // waits 15ms for the servo to reach the position
}

Have a closer look at the expressions in your for loops. Neither of them do as stated in the comments
Title: Re: Help Programming Arduino Uno
Post by: robocreator on January 02, 2013, 09:43:41 PM
OK, how do you suggest it should look like. I tried what the other said... I want to make it move, turn, stop, etc from the signal from the IR sensor.
Title: Re: Help Programming Arduino Uno
Post by: jwatte on January 04, 2013, 06:36:02 PM
Also, this code doesn't do what you think it does:

Code: [Select]
SensorPin = (10, 20, 30, 40, 50, 60, 70, 80);

It actually just assigns the value 80 to the variable SensorPin. The rest of the values are discarded by the comma operator.

I think you'd be well served by trying to learn C / C++ programming on a desktop computer, without a robot involved. Then you can step through the program in a debugger, and see what each line does, and compare it to what you think it should do -- a much faster way to learn!

If that's not an option for you, then I suggest cutting your program down to the smallest thing it can do. Then add one small feature at a time, and make it work before you move on to the next.
Title: Re: Help Programming Arduino Uno
Post by: MrWizard on January 05, 2013, 10:16:53 AM
http://www.computerscienceforeveryone.com/ (http://www.computerscienceforeveryone.com/)

Nice site and clear tutorials !