Pages: [1]   Go Down
Author Topic: Servo Library, can I address servos dynamically?  (Read 453 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Lincoln, NE
Offline Offline
Full Member
***
Karma: 0
Posts: 106
Having lots of fun with Arduino. Thanks everyone!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
switch(ch) {

case '0'...'9':

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

Your switch selector is a "char" - it can't also be a "Servo".
Logged

Per Arduino ad Astra

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
 * 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;      
    }
  }
}
« Last Edit: March 15, 2010, 03:53:29 am by mem » Logged

Pages: [1]   Go Up
Jump to: