Arduino sketch problem

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino sketch problem

Post by adafruit_support_bill »

but it does not work in uno
What exactly are the symptoms? How does it behave differently on the Uno vs the Duemilanove?

And what version of the IDE are you using?

Murray
 
Posts: 29
Joined: Wed Jul 01, 2009 2:50 am

Re: Arduino sketch problem

Post by Murray »

I have both the decimilanova and the adafruit uno. The codes that work on decimilanova work differently on uno. Ror one thing the servos that work smoothly on decimilanovw, after following your instructions, are back to being jerky and very slow. I tested the servos by pluging them into a bot using decimilanova, turned it on and they work perfectly. I'm ready to believe that the uno board is full of problems and will replace it with a decmila nova if they are still available.

I really appreciate your past help getting my bots to work perfectly. I hope my present problems with uno can also be worked out.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino sketch problem

Post by adafruit_support_bill »

So they work on the Uno, just not as smooth as on the Duemilanove? I'm not sure what would cause that.

What version of the IDE are you using? There was a PWM-related problem in version 20 that was corrected in version 21. I don't know if that would affect the servo library or not, but I'd suggest using the latest version if you are not already.

Murray
 
Posts: 29
Joined: Wed Jul 01, 2009 2:50 am

Re: Arduino sketch problem

Post by Murray »

I am using 0021, but I have solved the problem, it's in the code.
They way I determined it was to upload a sketch (below) that only had 2 servos and it worked perfectly. That got me thinking.

Code: Select all


#include <ServoTimer1.h>
ServoTimer1 servo1;
ServoTimer1 servo2;
// 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 
void setup() 
{ 
servo1.attach(9); 
servo2.attach(10);
} 

void loop() 
{ 
for(pos = 0; pos < 180; pos += 1) 
// goes from 0 degrees to 180 degrees 
{ // in steps of 1 degree 
servo1.write(pos);
servo2.write (pos); 
// tell servo to go to position in variable 'pos' 
delay(39); 
// waits 15ms for the servo to reach the position 
} 
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees 
{ 
servo1.write(pos); 
servo2.write (pos);
// tell servo to go to position in variable 'pos' 
delay(30); 
// waits 15ms for the servo to reach the position 
} 
}

I then took a bot that used the Ping sensor on a Decimilanova board, unplugged the Ping and lo and behold the servos no longer worked properly.
Somehow, the way the code is written, the servos depend on the sensor working properly.
This is the first time I am using a IR sensor and though the code compiles as OK the code does have a problem. Apparently the IR sensoew is not working properly.
I will try to rewrite the code to eliminate what I think is the problem. I'm not sure I'm good enough to do that.

Murray
 
Posts: 29
Joined: Wed Jul 01, 2009 2:50 am

Re: Arduino sketch problem

Post by Murray »

Servo on pin 3 (attached to separate 6v power supply) makeds one sweep to left and then stops. Why?

Code: Select all


#include <AFMotor.h>
#include <ServoTimer1.h>
AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);
ServoTimer1 servo1;
ServoTimer1 servo2;
ServoTimer1 servo3;
const int IRpin = 1; 
// analog pin for reading the IR sensor
const int voicePin=2;
const int eyePin=12;//always on
boolean servDir;
int servPos;
void setup()
{
Serial.begin(9600);
motor1.setSpeed(250);
motor2.setSpeed(250);
pinMode(voicePin, OUTPUT);
servo1.attach(9);// wall-e arm
servo2.attach(10);//wall-e arm
servo3.attach(3);//wall-e head 
servPos=45;
servDir=true;
}
void loop()
{
digitalWrite(voicePin, HIGH);//wall-e talks
// sets the voice on
delay(1000);
// waits for 1  seconds
digitalWrite(voicePin, LOW);
// sets the voice off
delay(5000);
// waits for 5  seconds
float volts = analogRead(IRpin)*0.0048828125; 
// value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float distance = 65*pow(volts, -1.10); 
// worked out from graph 65 = theretical distance / 
Serial.println(distance); // print the distance
long duration, inches, cm;
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

servo1.attach(9);// wall-e arm
if (servDir == true) servPos = servPos + 7;
if (servDir == false) servPos = servPos - 7;
if (servPos < 45) servDir = true;
if (servPos > 125) servDir = false;
delay(100);
Serial.print(servPos);
Serial.print(servDir);

servo2.attach(10);// wall-e arm
if (servDir == true) servPos = servPos + 7;
if (servDir == false) servPos = servPos - 7;
if (servPos < 45) servDir = true;
if (servPos > 125) servDir = false;
delay(100);
Serial.print(servPos);
Serial.print(servDir);

servo3.attach(3);//wall-e head 
if (servDir == true) servPos = servPos + 7;
if (servDir == false) servPos = servPos - 7;
if (servPos < 45) servDir = true;
if (servPos > 135) servDir = false;
delay(100);
Serial.print(servPos);
Serial.print(servDir);
{
Serial.print("Back ");
motor1.run(BACKWARD);
motor2.run(BACKWARD);
delay(500);
Serial.print("Turn ");
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(500);
}
{
Serial.print("Forward ");
motor1.run(FORWARD);
motor2.run(FORWARD);
}
Serial.println();
servo1.write(servPos);
delay(100);
servo2.write(servPos);
delay(100);
servo3.write(servPos);
delay(100);

}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;}


User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino sketch problem

Post by adafruit_support_bill »

What do Servos 1 and 2 do?
You have one "servoDir" and one "servoPos" controlling three servos. Not sure what motionyou are looking for, but after you execute the logic for servo1, the logic for servos 2 and 3 can't be right.

Murray
 
Posts: 29
Joined: Wed Jul 01, 2009 2:50 am

Re: Arduino sketch problem

Post by Murray »

I forgot to mention I am connecting to pins on the motor shield. Servo one and two are moving wall-e;s arms and they are working. servo three is for wall-e's head.

Murray
 
Posts: 29
Joined: Wed Jul 01, 2009 2:50 am

Re: Arduino sketch problem

Post by Murray »

where can I find the download for MsTimer2_h. All that I can find is for servo MsTimer2.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino sketch problem

Post by adafruit_support_bill »

http://www.arduino.cc/playground/Main/MsTimer2
The MsTimer2 zip file download contains both the cpp and h files.

Murray
 
Posts: 29
Joined: Wed Jul 01, 2009 2:50 am

Re: Arduino sketch problem

Post by Murray »

I am using Arduino uno with Arduino motor shield (using pins on shield. ) My project requires 3 servos and 2 DC motors. The compiler is Arduino 0021. The Arduino Uno is powered with a 9v rechargable and the shield is powered with a 6v rechargable.

Servo 1 and 2 attached to pins 9 and 10 (moves arms up and down) and are working correctly. Motors are attched to motor 1 and 2 and they are working correctly.
I have attached the 3rd servo to pin 6. No responce.


How do I get that 3rd servo operational on the shield? Is pin 6 on the shield a working pin?

Murray

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino sketch problem

Post by adafruit_support_bill »

How do you have the servo connected? Have you wired up power & ground to it as well as the signal pin?\

Pin 6 is also used by DC motor 4. You will have a conflict if you use that motor port.

From the FAQ:
The following pins are in use only if the DC/Stepper noted is in use:
Digital pin 11: DC Motor #1 / Stepper #1 (activation/speed control)
Digital pin 3: DC Motor #2 / Stepper #1 (activation/speed control)
Digital pin 5: DC Motor #3 / Stepper #2 (activation/speed control)
Digital pin 6: DC Motor #4 / Stepper #2 (activation/speed control)

Murray
 
Posts: 29
Joined: Wed Jul 01, 2009 2:50 am

Re: Arduino sketch problem

Post by Murray »

the 3rd servo has signal pin wired to pin 6 on shield and =/- wired to 6v shield power supply. If I understand correctly pin 6 on shield is reserved for motor. Can I use pin one on shield, as that seems to be open?

Thanks

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino sketch problem

Post by adafruit_support_bill »

Pin-6 is only used if you are using Motor-4. Pin-1 is used by serial communication. You can use it for a servo if you do not need the serial communication.

Murray
 
Posts: 29
Joined: Wed Jul 01, 2009 2:50 am

Re: Arduino sketch problem

Post by Murray »

So! .. what other pin can I use for the third servo? I am using pin 2 for a bumper and it works. I tried pin 1 and it doesen't work. Pins 9 and 10 plus motors 1 and 2 are also working.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino sketch problem

Post by adafruit_support_bill »

Check the FAQ: http://www.ladyada.net/make/mshield/faq.html
What pins are not used on the motor shield?
All 6 analog input pins are available. They can also be used as digital pins (pins #14 thru 19)

Digital pin 2, and 13 are not used.

The following pins are in use only if the DC/Stepper noted is in use:
Digital pin 11: DC Motor #1 / Stepper #1 (activation/speed control)
Digital pin 3: DC Motor #2 / Stepper #1 (activation/speed control)
Digital pin 5: DC Motor #3 / Stepper #2 (activation/speed control)
Digital pin 6: DC Motor #4 / Stepper #2 (activation/speed control)


The following pins are in use if any DC/steppers are used
Digital pin 4, 7, 8 and 12 are used to drive the DC/Stepper motors via the 74HC595 serial-to-parallel latch


The following pins are used only if that particular servo is in use:
Digitals pin 9: Servo #1 control
Digital pin 10: Servo #2 control


Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”