Go Down

Topic: Servo Library, can I address servos dynamically? (Read 482 times) previous topic - next topic

pinter75

Howdy.

using a case switch can I dynamically address a servo?

switch(ch) {

case '0'...'9':
      v = v * 10 + sm - '0';
 break;

case 'a'...'z':
     ch.write(v);
break;

I get error: request for member 'write' in 'ch', which is of non-class type 'char'

any ideas?

Thanks.

hari

Without seeing the rest of the code, I can't figure out what you're trying to do.

The compiler did not yell at you on the switch and case, so I presume ch is really a char and not a Servo object.

That is why the compiler pukes on ch.write.  Unlike a Servo object, a char is not even an object and therefore it does not know how to write itself.

You probably meant something like this:
Servo myServoArray[4];

myServoArray[ servoIndex ].write(v);

If you post more complete code, we can try to help you further.

Groove

Code: [Select]

switch(ch) {

case '0'...'9':

case 'a'...'z':
    ch.write(v);


Your switch selector is a "char" - it can't also be a "Servo".
Per Arduino ad Astra

mem

#3
Mar 15, 2010, 09:51 am Last Edit: Mar 15, 2010, 09:53 am by mem Reason: 1
Here is an example sketch that may help, you can easily convert the if statements to a case statment if you want.
I use this sketch to test lots of servos with different boards

Code: [Select]
/*
* Servo serial tester
*
* Command a servo by entering a numeric value for the angle followed by a letter to identify the servo
* where 'a' is the first servo, 'b' the second and so on
* '*' commands all servos
* '+' sweeps all servos in one direction and '-' sweeps then in the other direction
*/

#include <Servo.h>

const int NBR_SERVOS = MAX_SERVOS;       // the number of servos, up to 48 for Mega, 12 for other boards
const int FIRST_SERVO_PIN = 2;

Servo Servos[NBR_SERVOS] ; // max servos is 48 for mega, 12 for other boards

void setup()
{
 Serial.begin(9600);
 for(int i=0; i < NBR_SERVOS; i++)  
   Servos[i].attach(FIRST_SERVO_PIN +i);  
   
 delay(2000);  
 Serial.print(" Initialized ");  
 Serial.print(NBR_SERVOS,DEC);      
 Serial.println(" Servos");
}


void loop()
{
 static int pos = 0;

 if ( Serial.available())
 {
   char ch = Serial.read();

   if(ch >= '0' && ch <= '9')              // is ch a number?  
     pos = pos * 10 + ch - '0';           // yes, accumulate the value
   else if(ch >= 'a' && ch <= 'a'+  NBR_SERVOS) // is ch a letter for one of our servos?
   {
     Servos[ch - 'a'].write(pos);         // yes, save the position in the position array  
     pos = 0;
     int channel =  ch - 'a';
     int  angle = Servos[channel].read();
     Serial.print("Servo on pin ");
     Serial.print(FIRST_SERVO_PIN + channel, DEC);
     Serial.print(": angle = ");  
     Serial.println(angle,DEC);  
   }
   else if (ch == '*')
   {
     // position all the servos
     for(int i=0; i < NBR_SERVOS; i++)
       Servos[i].write(pos);        
     pos = 0;      
   }
   else if (ch == '+')
   {
     // sweep all servos from 0 to 180
     for(int angle = 0;angle < 180; )
     {
       for(int i=0; i < NBR_SERVOS; i++)
       {
         Servos[i].write(angle);            
       }
       Serial.print("Angle = ");
       Serial.println(angle,DEC);
       ++angle;

       delay(20);
     }
     pos = 0;      
   }
   else if (ch == '-')
   {
     // sweep all servos from 180 to 0
     for(int angle = 180;angle >= 0; )
     {
       for(int i=0; i < NBR_SERVOS; i++)
       {
         Servos[i].write(angle);                
       }
       angle = Servos[0].read();  
       Serial.print("Angle = ");
       Serial.println(angle,DEC);
       --angle;
       delay(20);
     }
     pos = 0;      
   }
 }
}


Go Up