So i dont know anything about coding but my friend needs help on something. What we have are two buttons that allow our continuous servo to turn left and right.When we press the button that allows for the servo to turn left, for instance, the servo turns left. But when we press the button that makes the servo turn right after, the servo continues to go left for a little bit and then to the right. So basically there is a little delay. PLEASE HELP US ON HOW TO FIX THIS PROBLEM
Also, how do you change the speed of a servo in the simplest way? We want our servo to be fast
Below is our code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int npos = 1;
int pos = 0; // variable to store the servo position
const int maxDeg = 180;
const int minDeg = 0;
const int leftPin = 3;
const int rightPin = 2;
const int led1Pin = 6; // indicator
const int led2Pin = 5; // indicator
const int outputPin = 9; // pwm function will be disabled on pin 9 and 10 if using servo
int leftPressed = 0;
int rightPressed = 0;
void setup()
{
myservo.attach(outputPin); // attaches the servo on pin 9 to the servo object
pinMode(leftPin, INPUT);
pinMode(rightPin, INPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, LOW); // turn the LED on (HIGH is the voltage level)
leftPressed = digitalRead(leftPin);
rightPressed = digitalRead(rightPin);
if(leftPressed == LOW){
if(pos < maxDeg) pos += 3;
myservo.write(pos); // tell servo to go to position in variable ‘pos’
digitalWrite(led1Pin,HIGH);
delay(30);
myservo.write(90)
; }
else
digitalWrite(led1Pin,LOW);
if(rightPressed){
if(pos > minDeg) pos -= 3;
myservo.write(pos); // tell servo to go to position in variable ‘pos’
digitalWrite(led2Pin,HIGH);
delay(30);
myservo.write(90)
;
}
else
digitalWrite(led2Pin,LOW);
// waits 15ms for the servo to reach the position
Totally just guessing, but if you look at the documentation for servo.write:
Writes a value to the servo, controlling the shaft accordingly. On a standard servo, this will set the angle of the shaft (in degrees), moving the shaft to that orientation. On a continuous rotation servo, this will set the speed of the servo (with 0 being full-speed in one direction, 180 being full speed in the other, and a value near 90 being no movement).
I think the code you have is for a continuous rotation servo. That's why it has the myservo.write(90), which is intended to stop it. Your servo thinks "90" means go back to 90 degrees.
Also, there are some very rude people about to flame you for posting your code wrong.
Also, there are some very rude people about to flame you for posting your code wrong.
It is not "flaming" for pointing out basic rules of the forum unless it is done in an offensive manner.
Therefore I will point out what you refer to in an advisory manner.
Please put your code in its own window as seen in other posts. This can be done by placing [code] and [/code] around the code or use the </> icon. This makes it easier for others to read.
Alright first let me say welcome to the forums, just a little warning people are going to give you a hard time for not using the code tags. It makes the easier to read as well as keeping the forums more organized.
If I understood the problem correctly you wanted to remove the delay after pressing the button. I set the delay to 0, try it out and let us know whats going on.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int npos = 1;
int pos = 0; // variable to store the servo position
const int maxDeg = 180;
const int minDeg = 0;
const int leftPin = 3;
const int rightPin = 2;
const int led1Pin = 6; // indicator
const int led2Pin = 5; // indicator
const int outputPin = 9; // pwm function will be disabled on pin 9 and 10 if using servo
int leftPressed = 0;
int rightPressed = 0;
void setup(){
myservo.attach(outputPin); // attaches the servo on pin 9 to the servo object
pinMode(leftPin, INPUT);
pinMode(rightPin, INPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, LOW); // turn the LED on (HIGH is the voltage level)
leftPressed = digitalRead(leftPin);
rightPressed = digitalRead(rightPin);
if(leftPressed == LOW){
if(pos < maxDeg) pos += 3;
myservo.write(pos); // tell servo to go to position in variable 'pos'
digitalWrite(led1Pin,HIGH);
delay(0);
myservo.write(90);
}else
digitalWrite(led1Pin,LOW);
if(rightPressed){
if(pos > minDeg) pos -= 3;
myservo.write(pos); // tell servo to go to position in variable 'pos'
digitalWrite(led2Pin,HIGH);
delay(0);
myservo.write(90);
}
else
digitalWrite(led2Pin,LOW);
// waits 15ms for the servo to reach the position
}
I know nothing about this but my friend needs help on something. so how do you make a continuous servo go faster? I understand that there wouldn't be high torque cos ppl kept emphasizing that but is there a simple way to input something into the code that makes our servo faster and possibly still have a high torque(if not then thats fine)? And i apologize if im asking a dumb question but i just really want to help him somehow. Thank you
This is the code below:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int npos = 1;
int pos = 0; // variable to store the servo position
const int maxDeg = 180;
const int minDeg = 0;
const int leftPin = 3;
const int rightPin = 2;
const int led1Pin = 6; // indicator
const int led2Pin = 5; // indicator
const int outputPin = 9; // pwm function will be disabled on pin 9 and 10 if using servo
int leftPressed = 0;
int rightPressed = 0;
void setup()
{
myservo.attach(outputPin); // attaches the servo on pin 9 to the servo object
pinMode(leftPin, INPUT);
pinMode(rightPin, INPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, LOW); // turn the LED on (HIGH is the voltage level)
leftPressed = digitalRead(leftPin);
rightPressed = digitalRead(rightPin);
if(leftPressed == LOW){
if(pos < maxDeg) pos += 3;
myservo.write(pos); // tell servo to go to position in variable 'pos'
digitalWrite(led1Pin,HIGH);
delay(30);
myservo.write(90)
; }
else
digitalWrite(led1Pin,LOW);
if(rightPressed){
if(pos > minDeg) pos -= 3;
myservo.write(pos); // tell servo to go to position in variable 'pos'
digitalWrite(led2Pin,HIGH);
delay(30);
myservo.write(90)
;
}
else
digitalWrite(led2Pin,LOW);
// waits 15ms for the servo to reach the position
myservo.write(90)This fixes the servo speed whatever the value of pos. Why increment/decrement pos then write a completely different value to the servo ?
For a continuous-rotation servo, this equates to 'stop'.
// tell servo to go to position in variable 'pos'
// waits 15ms for the servo to reach the position
Continuous-rotation servos don't go to particular positions. 0 = full speed in one direction, 180 = full speed in the other direction, 90 = stop.
Accurate comments go a long way.
This seems like a common theme to a lot of your posts. I know I'm a n00b on this forum, but I've been in the software industry a couple of decades, so bear with me for a second. Anyway, that's long enough to know that developers come in a whole spectrum of talents, and treating newcomers (read: new developers) like this is just going to turn them away from something that could otherwise turn into a rewarding passion for them.
Here's a presentation I saw last year at Pycon that I thought captured this very well. If you've got the time, the good part starts around the 5 minute mark: https://www.youtube.com/watch?v=hIJdFxYlEKE If not, here are the Cliffs Notes: http://lwn.net/Articles/641779/ (Skip forward to the "Mediocrity" section.)
Anyway, just thought I'd leave that here. tl;dr - The talent myth, or the idea that all programmers either "rock" or "suck", is driving people away from learning this skill, and it has a very negative impact on the industry (or scene, or whatever).
The talent myth, or the idea that all programmers either "rock" or "suck", is driving people away from learning this skill, and it has a very negative impact on the industry (or scene, or whatever).
Thanks
Which is why anyone already in the industry has a vested interest in keeping the profession "lean".
Just sayin'
On the subject of code tags.... I know it's not the topic as such, but the door was opened by two other posts before.
Code tags don't only put the code in a nice box with a fixed pitch font, abut there are at least two cases where not using the tags mangles the code. Text not inside code tags is subject to getting interpreted like html. So it's not just some members being pedantic; in many cases the un-tagged code is impossible to read.
So first, compare this...
myVal=digitalRead(8);
to this...
myVal=digitalRead(8);
That one's just annoying. But in the next example, some of the code actually disappears.
Compare the arrowed line here... (I know, the code itself is nonsense)
// Dim an LED using a PWM pin
int PWMpin = 10; // LED in series with 470 ohm resistor on pin 10
void setup()
{
// no setup needed
}
void loop()
{
for (int i=0; i <= 255; i++){
char*=foo; //<<<<<<<<<<<<<<<<*
delay(10);*
}* //read pot for base servo and move servo
baseVal = analogRead(basePin); // reads the value of the potentiometer (value between 0 and 1023)*
baseVal = constrain(baseVal, 70, 1000);*
if (baseVal > 530 && baseVal <540)*
{*
base.write(75);* } ... to this one: ```
*// Dim an LED using a PWM pin
int PWMpin = 10; // LED in series with 470 ohm resistor on pin 10
void setup()
{
// no setup needed
}
void loop()
{
for (int i=0; i <= 255; i++){
char[i]=foo; //<<<<<<<<<<<<<<<<
delay(10);
}
//read pot for base servo and move servo
baseVal = analogRead(basePin); // reads the value of the potentiometer (value between 0 and 1023)
baseVal = constrain(baseVal, 70, 1000);
if (baseVal > 530 && baseVal <540)
{
base.write(75);
}* ``` Not only has the text changed to italic (annoying), but the array index has disappeared since that's what turned the italics on when it got parsed. So one can't copy/paste the code and compile it without fixing that. That said, yes there are good and bad ways of asking for code to be in tags, but the asking itself is not just for the heck of it.
What we have are two buttons that allow our continuous servo to turn left and right.
Very basic code to do that.
//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually *DOES NOT WORK*.
#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
servo1.attach(7);
digitalWrite(4, HIGH); //enable pullups to make pin high
digitalWrite(5, HIGH); //enable pullups to make pin high
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(170);
}
press2 = digitalRead(button2);
if (press2 == LOW)
{
servo1.write(10);
}
}
CrossRoads:
delay(0); has been discussed as having weird effects on code behavior as well.
That's interesting; perhaps worth discussing outside of this thread. (Not that one would often use a delay(0) except perhaps as in this case were it was part of the troubleshoot. If it does cause problems it's of course easy to // it out rather than make 0.)