Please help an artist with programming

Hi,

This may be fairly simple for some of you, but not for me. I have two kinetic virus sculptures that are very similar, each uses 15 servos, but one has continuous rotation servos, and the one I am trying to program now does not. They both have Cds sensors that are attached to a servo motor, and should move a part of the sculpture when a light shines on the particular sensor. Here is a video of the sculpture I am trying to program now: Bacteriophage - YouTube I have spent days wiring it up, but there are so many wires it’s hard to see what’s going on. I think I wired it all right. My question is about the program. If I want to change this program which works for continuous rotation servos, to one that works with standard servos, how could I do that? I just want to make the servo wiggle back and forth when the light shines on it. How do I tell it to wiggle back and forth instead of go full speed clockwise in this program?

#include <Servo.h>      // include the servo library

 Servo servoMotor;       // creates an instance of the servo object to control a servo

 int analogPin = 0;      // the analog pin that the sensor is on
 int analogValue = 0;    // the value returned from the analog sensor

 int servoPin = 9;       // Control pin for servo motor. As of Arduino 0017, can be any pin

 void setup() { 
   servoMotor.attach(servoPin);  // attaches the servo on pin 2 to the servo object
 } 

 void loop() 
 { 
     analogValue = analogRead(analogPin);                 // read the analog input (value between 0 and 1023)
   if(analogValue > 700) {
        servoMotor.write(0);                   // Spin servo at max speed clockwise
   } else {
        servoMotor.write(93);                 //  Stop the servo
   }
   delay(15);                                           // waits for the servo to get there 
                        
 }

Below is what I have done on my own, to try and combine my original program with a sweep program: It verified ok, but I can’t see anything working, not sure what the problem is.

#include <Servo.h>      // include the servo library

 Servo servoMotor;       // creates an instance of the servo object to control a servo

 int analogPin = A0;      // the analog pin that the sensor is on
 int analogValue = 0;    // the value returned from the analog sensor
int pos = 0;    // variable to store the servo position 
 int servoPin = 13;       // Control pin for servo motor. As of Arduino 0017, can be any pin

 void setup() { 
   servoMotor.attach(servoPin);  // attaches the servo on pin 2 to the servo object
 } 

 void loop()  { 
     analogValue = analogRead(analogPin);                 // read the analog input (value between 0 and 1023)
   if(analogValue > 700) 
      
  

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

Thanks so much for your advice.

You can use a while loop to check the distance of your hand and if it is within your preferred range, tell the servo to turn left and right. Being that it is in a while loop, it will continue to turn as long as your hand is near the sensor.

Also you can use an Array for your sensors and servos. Hint hint.

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

Try increasing the step from 1 to 10 and see what happens — pos += 10 (and pos -= 10 elsewhere)

Rather than code that moves all the way when your hand is detected I would try code that just moves one step at a time. Something like this

byte servoPos = 0; // <-------put this before setup() to create a global variable
byte servoStep = 10; // <------

if(analogValue > 700) {
servoMotor.write(servoPos);
servoPos += servoStep;
if servoPos < 0 || servoPos > 180
servoStep = - servoStep; // reverse direction
}
delay(50);
}

If you want to manage several servos it may be a good idea to replace the use of delay() with the technique in the Blink Without Delay example.

…R