[SOLVED] Wifly and Servo not working in one Program?

Hi all,
I'm new in Arduino. I have the following problem and hope to find a solution here.
I have a Arduino Mega2560 with a Wifly shield installed. Mega-Pins 50-53 are connected to the Wifly board 10-13.
I have a Arduino program to control a few digital outputs and a servo. These outputs will be controlled over a telnet session or a iPad Connection. As long as I use the Ethernet shield, the programm runs fine, I can control the digital outputs as well as the servo.
as soon as I use the Wifly library, I can control the digital outputs but the Servo is not moving anymore.
I have read somewhere that the Timers are in conflict but even with the ServoTimer2 Library I can not get it to work.
Is there a fact-sheet for the Libraries telling what Registers are used?
Can somebody help me here to get the problem solved?
Are there some limitations I didn't count with?

What I have: Library Wifly downloaded from here : GitHub - sparkfun/WiFly-Shield: WiFly Shield -- A shield for the Roving Networks RN-52 WiFly Module. and did some moving of the Libraries
Arduino Mega2560 IDE 1.0.6 with default Libraries

My code:

/*
 * Web Server
 *
 * (Based on Ethernet's WebServer Example)
 *
 * A simple web server that shows the value of the analog input pins.
 * for the MEGA board: Pins 50->12, 51->11, 52->13, 53->10 Be sure these pins (10-13) are not connected to the Mega
 * WiFly will have IP 169.254.1.1
 */

#include <SPI.h>
#include <WiFly.h>
#include <Servo.h> 

#include "Credentials.h"


// Pins used on Arduino
const int ledPinGruen = 24;
const int ledPinGelb = 25;
const int ledPinRot = 26;
const int servoPinA = 29;

int ledState = LOW;

// The getInputStream has a blank line
int inputStringIndex = 0;
int inputStringStart = 0;
char inputString[] = {};

Servo myservo;  // create servo object to control a servo 

WiFlyServer server(23);

void setup() {
  pinMode(ledPinGruen, OUTPUT);
  pinMode(ledPinGelb, OUTPUT);
  pinMode(ledPinRot, OUTPUT);
  digitalWrite(ledPinGruen,LOW);
  digitalWrite(ledPinGelb,LOW);
  digitalWrite(ledPinRot,LOW);

  WiFly.begin(true);
  Serial.begin(9600);
  Serial.println("WiFly Shield AdHoc Example");

  if(!WiFly.createAdHocNetwork(ssid)) {
    while(1) {
      Serial.println("Failed to create network, handle error properly?");
    }
  }

  Serial.println("Network Created");
  Serial.print("IP: ");
  Serial.println(WiFly.ip());
  
  Serial.println("Starting Server");
  server.begin();
  
  myservo.attach(servoPinA);  // attaches the servo on pin 28 to the servo object 
}



void SetOutput()
{
  boolean Output;
  String tempString;
  int tempInt;
  int pos = 0;    // variable to store the servo position 
  
  inputStringStart++;
  if (inputString[inputStringStart+1] == '0') {
    Output=LOW;
  } else {
    Output=HIGH;
  }
  switch (inputString[inputStringStart])  {
    case '0':
      digitalWrite(ledPinGruen, Output);
      break;
    case '1':
      digitalWrite(ledPinGelb, Output);
      break;
    case '2':    
      digitalWrite(ledPinRot, Output);
      break;
    case '3':
      tempString = inputString;
      tempString = tempString.substring(2);
      tempInt = tempString.toInt();
      Serial.println(tempInt);
      myservo.write(tempInt);
      break;
    default: ;
  }
  
  
  
}

void decodeInputString()
{
/*
    Format of an inputString:
    first char: G or S
    G = Get
    S = Set
    second char: Type
    third char: Version
    rest: parameterlist
*/
inputStringStart = 0;

switch (inputString[inputStringStart]) {
  case 'G':
    // do get
    break;
  case 'S':
    // do set
    Serial.println("Found S");
    SetOutput();
    break;
default: 
  // move start of inputSting 
  // ToDo in case the first char was not one of the above, move pointer and try again, check pointer against line length
  Serial.println("No S or G found");
  
  }


}

void getInputStream(char c)
{
// reads the input till newLine
  inputString[inputStringIndex] = c;
  inputStringIndex++;
  
  if (c == '\n') {
  // you're starting a new line  
    decodeInputString();
    inputStringIndex = 0;
  }
}

void loop() {
  delay(200);
  WiFlyClient client = server.available();
  Serial.println("Server Available");
  if (client) {
    Serial.println("new client");

    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);

        getInputStream(c);
        
      }
    }
    client.stop();
    Serial.println("client disconnected");
  }
}
  myservo.attach(servoPinA);  // attaches the servo on pin 28 to the servo object

Really? That's why you assigned the value of 29 to servoPinA? Useless comments MUST be kept accurate. Or disposed of.

void getInputStream(char c)
{
// reads the input till newLine
  inputString[inputStringIndex] = c;
  inputStringIndex++;

This function does not GET anything. The use of get in the name is wrong.

You see SOME serial output that you haven't shared. You have the servo powered somehow, that you haven't shared. With the WiFly shield attached, but NO code to use it, does your servo work?

As you have not told the WiFly lib to use pins 50-53 why do you expect it to use them. A link to the WiFly board would also be useful!

Mark

As you have not told the WiFly lib to use pins 50-53 why do you expect it to use them.

Well, I would certainly expect a library like that to KNOW that it has to use the SPI pins.

If I use the ethershield (and the corresponding Library with it) this program works fine.

With the Wifly, the "input" works fine as well, on the Serial Monitor I get the values I want to set the Servo at.

...
    case '3':
      tempString = inputString;
      tempString = tempString.substring(2);
      tempInt = tempString.toInt();
      Serial.println(tempInt);
      myservo.write(tempInt);
      break;

the tempInt Value is printed as expected.
The same Function used with the Ethershield works without modification.

and yes, the Servo is connected to pin 29 (I tried a range of pins)

Even more, If I add the following lines in the setup(), the Servo moves.

  myservo.attach(servoPinA);  // attaches the servo on pin 29 to the servo object 
  myservo.write(10);
  delay(500);
  myservo.write(170);

If I add the same lines in the Case selection, nothing goes (the text is printed on the serial).

    case '3':
      tempString = inputString;
      tempString = tempString.substring(2);
      tempInt = tempString.toInt();
      Serial.println(tempInt);
      myservo.write(tempInt);
  Serial.println("WiFly Shield Move Servo 3");
  myservo.write(10);
  delay(500);
  myservo.write(170);
  delay(500);
  
      break;

How are you powering the servo?

The Servo is powered by the 5V of the Arduino.
This is a very small Servo without any load.
I doubt it is a power or noise problem, in the beginning of the program I can move the servo, with the ethershield, the servo moves as well.

I try to understand the both libraries now to see if there is a conflict in interrupt or counters.

Ok, Found the problem.

As I know, the libraries are compatible, if you write your code correct :slight_smile:

Initially I hat the following 2 code segments:

char inputString[] = {};
...
void getInputStream(char c)
{
// reads the input till newLine
  inputString[inputStringIndex] = c;
  inputStringIndex++;
...

So, as far as I understand this, the inputString hat a length of 0
Putting character in it and increase the index , must have overwritten something else, so the code didn't work any more.
after setting a initial size to the char-array, this avoids the overwriting and the program will work.

char inputString[10];

Now I have to ask myself if this is the right way to do this or if I will use String for this. As a newby, I have to do some more reading here.

Thanks, Especially PaulS for help.

is the right way to do this

Yes. Set the initial char array size to 1 character larger than you will ever put in the array and you should be OK.

As to Strings ? Just say No.