Software > Software

Help Programming Arduino Uno

(1/2) > >>

robocreator:
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
}

jwatte:
"It doesn't work" how?
You are attempting to run this code, and what happens, exactly?

arigid:
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.

robocreator:
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...

jwatte:
Well, the first problem is that you don't use braces around your for() loop statements. When you write code like this:


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

--- End code ---

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: ---for (int i = 0; i < 180; ++i) {
    something();
    delay(15);
}

--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version