I am trying to automate some venitian blinds using an arduino and servoes. three blinds in a bay window so all operate at the same time. all the servoes do is tilt the slats from open to closed and back depending on the time of day.
i have found code on line that does what i want with just one servo but cant figur out how to add the other two.
the part of the code that is stumping me is:
Servo myservo; // create servo object to control a servo
#define servoPin 3 //~
#define pushButtonPin 2
i will change the servo name to servo1,servo2,servo3 in the code if any one can help.
Sorry if this is a dump question i am very new to arduino and coding.
Please show all the code.
Help us help you.
here is all the code im using at the moment:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
#define servoPin 10 //~
#define pushButtonPin 2
int angle =90; // initial angle for servo
int angleStep =5;
const int minAngle = 90;
const int maxAngle = 180;
int buttonPushed =0;
void setup() {
Serial.begin(9600); // setup serial
myservo.attach(servoPin); // attaches the servo on pin 10 to the servo object
pinMode(pushButtonPin,INPUT_PULLUP);
Serial.println("Servo Button ");
}
void loop() {
if(digitalRead(pushButtonPin) == LOW){
buttonPushed = 1;
}
if( buttonPushed ){
// change the angle for next time through the loop:
angle = angle + angleStep;
// reverse the direction of the moving at the ends of the angle:
if (angle <= minAngle || angle >= maxAngle) {
angleStep = -angleStep;
buttonPushed = 0;
}
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
delay(100); // waits for the servo to get there
}
}
Did you try to add a second servo to the code? Please show us your efforts.
B707 are you saying to just add
"#define servoPin 11 //~"
for the second servo?
Not only servopin, but also a second servo object and all other code, that needs to control second servo.
Please note, that you can't use the same variables for both servos, so you need something like servoPin1 for first and servoPin2 for the second.... the same for objects....
Thanks b707 as I said very new to all this so all your help is appreciated, just to confirm i need the following :
servo servo1; // create servo object to control a servo
#define servoPin 10 //~
servo servo2; // create servo object to control a servo
#define servoPin 11 //~
etc
is that correct?
clu4u
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myOtherservo; // create servo object to control a servo
#define servoPin 10
#define servoOtherPin 100
#define pushButtonPin 2
int angle =90; // initial angle for servo
int angleStep =5;
const int minAngle = 90;
const int maxAngle = 180;
int buttonPushed =0;
void setup() {
Serial.begin(9600); // setup serial
myservo.attach(servoPin); // attaches the servo on pin 10 to the servo object
myservo.attach(servoOtherPin); // attaches the servo on pin 100 to the servo object
pinMode(pushButtonPin,INPUT_PULLUP);
Serial.println("Servo Button ");
}
void loop() {
if(digitalRead(pushButtonPin) == LOW){
buttonPushed = 1;
}
if( buttonPushed ){
// change the angle for next time through the loop:
angle = angle + angleStep;
// reverse the direction of the moving at the ends of the angle:
if (angle <= minAngle || angle >= maxAngle) {
angleStep = -angleStep;
buttonPushed = 0;
}
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
delay(100); // waits for the servo to get there
}
}
got it?
Thanks clu4u ill try that tonight when i get finished at work. Thanks everyone for all the help so far. im sure ill be asking more questions as i learn.
OK so i have tried that with both suggestions from B707 and Idahowalker and now get an error message when i run the verify. error is:
Compilation error: request for member 'attach' in '10', which is of non-class type 'int'
the code is now:
#include <Servo.h>
Servo servo0; // create servo object to control a servo
Servo servo1; // create servo object to control a servo
Servo servo2; // create servo object to control a servo
#define servo0 10
#define servo1 11
#define servo2 12
#define pushButtonPin 2
int angle =90; // initial angle for servo
int angleStep =5;
const int minAngle = 90;
const int maxAngle = 180;
int buttonPushed =0;
void setup() {
Serial.begin(9600); // setup serial
servo0.attach(servo0Pin1); // attaches the servo on pin 10 to the servo object
servo1.attach(servo1Pin2); // attaches the servo on pin 11 to the servo object
servo2.attach(servo2Pin3); // attaches the servo on pin 12 to the servo object
pinMode(pushButtonPin,INPUT_PULLUP);
Serial.println("Servo Button ");
}
void loop() {
if(digitalRead(pushButtonPin) == LOW){
buttonPushed = 1;
}
if( buttonPushed ){
// change the angle for next time through the loop:
angle = angle + angleStep;
// reverse the direction of the moving at the ends of the angle:
if (angle <= minAngle || angle >= maxAngle) {
angleStep = -angleStep;
buttonPushed = 0;
}
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
delay(100); // waits for the servo to get there
}
}
You have
#define servo0 10
Then later in the code you have
servo0.attach(servo0Pin1); // attaches the servo on pin 10 to the servo object
which because of the #define the compiler sees as
10.attach(servo0Pin1); // attaches the servo on pin 10 to the servo object
In addition, where are the servo pins declared in the sketch ?
Thanks i thought it might be somthing like that but still learning and very much a newby with this. now got the following error:
Compilation error: expected ')' before numeric constant but cant spot a missing '(' in the code.
Post your new code
new code is:
#include <Servo.h>
Servo servo0; // create servo object to control a servo
Servo servo1; // create servo object to control a servo
Servo servo2; // create servo object to control a servo
#define servo0Pin 10
#define servo1Pin 11
#define servo2Pin 12
#define pushButtonPin 2
int angle =90; // initial angle for servo
int angleStep =5;
const int minAngle = 90;
const int maxAngle = 180;
int buttonPushed =0;
void setup() {
Serial.begin(9600); // setup serial
servo0.attach(servo0Pin 10); // attaches the servo on pin 10 to the servo object
servo1.attach(servo1Pin 11); // attaches the servo on pin 11 to the servo object
servo2.attach(servo2Pin 12); // attaches the servo on pin 12 to the servo object
pinMode(pushButtonPin,INPUT_PULLUP);
Serial.println("Servo Button ");
}
void loop() {
if(digitalRead(pushButtonPin) == LOW){
buttonPushed = 1;
}
if( buttonPushed ){
// change the angle for next time through the loop:
angle = angle + angleStep;
// reverse the direction of the moving at the ends of the angle:
if (angle <= minAngle || angle >= maxAngle) {
angleStep = -angleStep;
buttonPushed = 0;
}
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
delay(100); // waits for the servo to get there
}
}
servo0.attach(servo0Pin 10); // attaches the servo on pin 10 to the servo object
Use either
servo0.attach(servo0Pin); // attaches the servo on pin 10 to the servo object
where servo0Pin has a value of 10
or
servo0.attach(10); // attaches the servo on pin 10 to the servo object
Not a mixture of both
Have you looked at the Servo examples in teh IDE ?
What's the problem with the new code? Besides what's noted in the previous posting.
Thanks, it now validates, so ill physicaly try it. the support on here is great thank you. Im sure ill be back with more questions as i lear to code.
You have changed the code again so please post the new version
ok it validates but when i try to run the servoes nothing happens unless i comment out any two of the servo instructions:
#include <Servo.h>
Servo servo0; // create servo object to control a servo
Servo servo1; // create servo object to control a servo
//Servo servo2; // create servo object to control a servo
#define servo0Pin 10
#define servo1Pin 11
//#define servo2Pin 12
#define pushButtonPin 2
int angle =0; // initial angle for servo
int angleStep =5;
const int minAngle = 0;
const int maxAngle = 90;
int buttonPushed =0;
void setup() {
Serial.begin(9600); // setup serial
servo0.attach(servo0Pin); // attaches the servo on pin 10 to the servo object
servo1.attach(servo1Pin); // attaches the servo on pin 11 to the servo object
//servo2.attach(servo2Pin); // attaches the servo on pin 12 to the servo object
pinMode(pushButtonPin,INPUT_PULLUP);
Serial.println("Servo Button ");
}
void loop() {
if(digitalRead(pushButtonPin) == LOW){
buttonPushed = 1;
}
if( buttonPushed ){
// change the angle for next time through the loop:
angle = angle + angleStep;
// reverse the direction of the moving at the ends of the angle:
if (angle <= minAngle || angle >= maxAngle) {
angleStep = -angleStep;
buttonPushed = 0;
}
servo0.write(angle); // move the servo to desired angle
servo1.write(angle); // move the servo to desired angle
//servo2.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
delay(100); // waits for the servo to get there
}
}