Hola a todos,
Mi nombre es Larissa y estudio computer art en ny. Este ano presento mi tesis que es una instalación interactiva. Construí un jardín mecánico de flores- ahora necesito ayuda con mi código. Estoy usando 6 servos en pin digitales y 6 maxsonar en los pin análogos. Ya tengo el código para un servo y un pin y funciona bien. Solo necesito hacer lo mismo para los 6 servos y maxbotix.
El código original (Ayuda de Michael Margolis) es:
#include <Servo.h>
Servo myservo;
int pos = 0;
int pos2 = 0;
void setup() {
Serial.begin(9600);
myservo.attach(9);
}
void loop() {
if (analogRead(1)< 110){
Serial.print (analogRead(1));
Serial.println();
moveTo(170);
}
else{
Serial.print (analogRead(1));
Serial.println();
moveTo(10);
}
}
// move to the given position
// degreesPerStep of 4 moves 160 degrees in a little under a second
// degreesPerStep of 1 moves 160 degrees in around 3.2 seconds
void moveTo(int newPosition){
int degreesPerStep = 1; // decrease this to slow movement
int currentPos = myservo.read();
int movement = newPosition - currentPos; // the number of degrees to move
if(movement < 0){
while(currentPos > newPosition){
currentPos = currentPos - degreesPerStep;
myservo.write( currentPos);
delay(20);
}
}
else{ // movement is >0 )
while(currentPos < newPosition){
currentPos = currentPos + degreesPerStep;
myservo.write( currentPos);
delay(20);
}
}
}
Luego Awol me ayudo a crear un array para mis servos- codigo:
#include <Servo.h>
#define N_SERVOS 6
Servo myServo [N_SERVOS];
const byte servoPin [N_SERVOS] = {2, 3, 4, 5, 6, 7};
int pos = 0;
int pos2 = 0;
void setup() {
Serial.begin(9600);
for (int i = 0; i < N_SERVOS; ++i) {
myServo [i].attach (servoPin [i]);
}
}
void loop() {
[glow] if (analogRead(Pin)< 110){
Serial.print (analogRead(Pin));
Serial.println();
moveTo(170);
}
else{
Serial.print (analogRead(Pin));
Serial.println();
moveTo(10);
}[/glow]
}
void moveTo(int newPosition){
int degreesPerStep = 1; // decrease this to slow movement
int currentPos = myServo.read();
int movement = newPosition - currentPos; // the number of degrees to move
if(movement < 0){
while(currentPos > newPosition){
currentPos = currentPos - degreesPerStep;
myServo.write( currentPos);
delay(20);
}
}
else{ // movement is >0 )
while(currentPos < newPosition){
currentPos = currentPos + degreesPerStep;
myServo.write( currentPos);
delay(20);
}
}
}
Como soluciono los pins análogos?
Cualquier ayuda es muy bien recibida!