So I referred to the example section in the Arduino software and used the Knob example for my servo motor. The program needs a pot to be used. So when I used a pot, I noted down values from the serial monitor at which it would run at high speeds(anything above 560). Now when i try using these values in the program instead of the pot itself, it doesn't seem to work. I have an Arduino Uno. I modified the code as follows :
#include <Servo.h>
Servo myservo;
int potpin;
int val;
void setup()
{
myservo.attach(9);
}
void loop()
{
for(potpin=560;potpin<1024;potpin+=1)
{
val = potpin;
val = map(val, 0, 1023, 0, 179);
myservo.write(val);
delay(15);
}
}
After I download this code onto my Arduino board, it doesnt do anything. Nothing happens. Is there something wrong with the code? All I want is the pot values to stay in that range so that the motor can run at full speed.
Without more information, I assume the servo is modified for continuous rotation. If so, the speed/direction control of the servo is probably ~1400us to ~1600us, with ~1500us being stopped (this assumes the servo is tweaked for being stopped at 1500us). Below is some servo test code I find useful for testing.
// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.writeMicroseconds(1500); //set initial servo position if desired
myservo.attach(7, 500, 2500); //the pin for the servo control, and range if desired
Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
myservo.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
myservo.write(n);
}
readString=""; //empty for next input
}
}
UKHeliBob:
The first thing that I would do would be to print the value of val after the map() to make sure that the right values are being output.
Yes I checked the serial monitor. The right values are being output. Values between 0 and 180
How is the servo powered ? Direct from the Arduino or from an external supply with a common GND connection ?
Actually, you refer to a motor rather than a servo. Which do you have ?
I actually have a motor. I am using the servo code to run the motor. With the potentiometer connected, the motor runs fine. But when I tried putting those values as a program, it doesn't seem to work.
The loop seems to run fine. When I checked the values in the serial monitor, it runs in the range I want it to run(560 to 1023). But the motor still doesn't work. What am I doing wrong?
I actually have a motor. I am using the servo code to run the motor.
You're using at best 10% duty cycle 50Hz PWM to drive a motor, and you're surprised it isn't working?
Post code, post a schematic, post pictures.
I am using pin 5 as the PWM pin. So the base frequency of the PWM pin is 62.5 Khz. And for the duty cycle to be executed I used the analogWrite function and it still doesn't seem to work. I will be posting pictures shortly. This is the code:
int potpin;
int val;
int motor=5;
void setup()
{
Serial.begin(9600);
}
void loop()
{
for(potpin=560;potpin<1024;potpin+=1)
{
val = potpin;
val=map(val,0,1023,0,255);
Serial.println(val);
analogWrite(motor,val);
delay(100);
}
}
And I solved the problem. I stopped using the for loop. Just initialized one value to the variable(value above 560). And mapped it to a range between 0 and 255. Then used analogWrite and it runs fine at full speed. I think I made a mistake by using the for loop. I think the reason was that the loop was switching from one value to the other quickly. And the Arduino board wouldn't have time to write it.