go_away

Author Topic: Help Programming Arduino Uno  (Read 1259 times)

0 Members and 1 Guest are viewing this topic.

Offline robocreatorTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Help Programming Arduino Uno
« 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
}

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,343
  • Helpful? 81
Re: Help Programming Arduino Uno
« Reply #1 on: December 30, 2012, 07:01:36 PM »
"It doesn't work" how?
You are attempting to run this code, and what happens, exactly?

Offline arigid

  • Jr. Member
  • **
  • Posts: 22
  • Helpful? 0
Re: Help Programming Arduino Uno
« Reply #2 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.

Offline robocreatorTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: Help Programming Arduino Uno
« Reply #3 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...

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,343
  • Helpful? 81
Re: Help Programming Arduino Uno
« Reply #4 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);
}

Offline robocreatorTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: Help Programming Arduino Uno
« Reply #5 on: January 02, 2013, 03:04:13 PM »
I tried, thank you, do you know how to fix the sensor. Thanks for your help.

Offline briselec

  • Jr. Member
  • **
  • Posts: 10
  • Helpful? 1
Re: Help Programming Arduino Uno
« Reply #6 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
In assembly language no one can hear you scream

Offline robocreatorTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: Help Programming Arduino Uno
« Reply #7 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.

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,343
  • Helpful? 81
Re: Help Programming Arduino Uno
« Reply #8 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.

Offline MrWizard

  • Full Member
  • ***
  • Posts: 116
  • Helpful? 0
  • My cylon friend told me a killing joke......
Re: Help Programming Arduino Uno
« Reply #9 on: January 05, 2013, 10:16:53 AM »
http://www.computerscienceforeveryone.com/

Nice site and clear tutorials !

 


Get Your Ad Here

data_list