Servo - analogwrite issue

Hi there,
I'm controlling an BlendMicro with an android smartphone via BLE.
I have two servos (actually one servo and one motor) and serveral leds. The servos are connected to pin 9 and 10. Some of the LEDs shall be dim-able, so i need the other pwm-outputs for them.
The signal I send: 1st 2 numbers are the "channel", the folling 4 are the value - in the case of controlling a servo, they are microseconds. in the case of leds, each number stands for an led (1=on, 2=off). that's just if someone is wondering about my cruel code:D

The problem: if i turn on a led connected to an pwm output, i can't control the motor anymore. The servo is still working fine. it doesn't matter if i use "analogwrite" or "digitalwrite". If i switch the led off, everything is fine.
There is no problem with the signal. I checked it on the serial monitor.
it doesn't matter if there is a led connected to the output or if there is just nothing.
turning on a digital output doesn't affect the motor

Another interesting thing: If i turn on a pwm-output like before, and now turn of and turn off another pwm-ouput, I can control the motor again.

The motor isn't connected to the arduino directly, of course there is a motor-driver-shield between.

I hope someone has a simple solution:D

here is the code.

#include <SPI.h>
#include <EEPROM.h>
#include <boards.h>
#include <RBL_nRF8001.h>
#include <Servo.h>

#define SERVO_PIN_1   9
#define SERVO_PIN_2   10
#define LED_1_PWM   11 //headlight1
#define LED_2       12 //indicator left
#define LED_3        1 //indicator right
#define LED_4        8 //pos light 1
#define LED_5_PWM    5 //headlight2
#define LED_6       A0 //pos light 2
#define LED_7        0
#define LED_8_PWM    3 //headlight3
#define LED_9        2
#define LED_10      A2 //disabled
#define LED_11      A3 //disabled
#define LED_12      A5 //disabled
#define LED_13_PWM  13 //disabled
#define sensorPin    A1
//pin 9, 10: no analog write(pwm) if Servo used!!!!

byte bdata0 = 0;
byte bdata1 = 0;
byte bdata2 = 0;
byte bdata3 = 0;
byte bdata_all = 0;
long ldata_all;
String sdata_all = "0";
String schannel = "0";
int ichannel = 0;
int ivalue = 0;
int Servo1min = 1400;
int Servo1mid = 1500;
int Servo1max = 1600;
int Servo2min = 1400;
int Servo2mid = 1500;
int Servo2max = 1600;
int sensorValue = 0;

Servo Servo1;
Servo Servo2;

void setup() {

  ble_begin();
  Serial.begin(57600);
  ble_set_name("BLEtest3");

  // pinMode(TEST_PIN1, OUTPUT);
  Servo1.attach(SERVO_PIN_1);
  Servo2.attach(SERVO_PIN_2);
  pinMode(LED_1_PWM, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  pinMode(LED_4, OUTPUT);
  pinMode(LED_5_PWM, OUTPUT);
  pinMode(LED_6, OUTPUT);
  pinMode(LED_7, OUTPUT);
  pinMode(LED_8_PWM, OUTPUT);
  pinMode(LED_9, OUTPUT);

  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_3, HIGH);
  delay(5000);//time to open serial after connecting to computer
  //read eeprom

  EEPROM.get(1, Servo1min );
  EEPROM.get(3, Servo1mid );
  EEPROM.get(5, Servo1max );
  EEPROM.get(7, Servo2min );
  EEPROM.get(9, Servo2mid );
  EEPROM.get(11, Servo2max );
  Servo1.writeMicroseconds(Servo1mid);
  Servo2.writeMicroseconds(Servo2mid);

  Serial.println(Servo1min);
  Serial.println(Servo1mid);
  Serial.println(Servo1max);
  Serial.println(Servo2min);
  Serial.println(Servo2mid);
  Serial.println(Servo2max);

  digitalWrite(LED_2, LOW);
  digitalWrite(LED_3, LOW);
  //digitalWrite(TEST_PIN1, HIGH);

}
void loop() {

  while (ble_available()) {
    bdata0 = ble_read();//reading all data. it must be String. Data has 2-4 digits: 1st one always indicates "channel" (1-9), the following the (pwm) value. Android sends 4byte blocks: To send numbers bigger than 255, after 1st byte is "filled" (255) the 2nd get 1 and the 1st one starts at 0 again. then the second gets 2, and so on, till it reaches 255.
    bdata1 = ble_read();
    bdata2 = ble_read();
    bdata3 = ble_read();

    ldata_all = bdata0 + (bdata1 * 256L) + (bdata2 * 65536L); // + (bdata3 * 16777216); // "connect" data0 and data1 is quite easy, but didn't found out how to handle data2 and data3. Anyway, we don't need higher numbers (3 for pwm and 1-2 left for channel (max 60 channels possible - that's enough:D )

    //Serial.println("ldata_all");
    //Serial.println(ldata_all);

    sdata_all = ldata_all;
    //schannel = sdata_all.charAt(0);//saving channel in int
    schannel = sdata_all.substring(0, 2);
    ichannel = schannel.toInt();
    Serial.println("ichannel");
    Serial.println(ichannel);
    sdata_all.remove(0, 2);//remove 1st two digits, so just the value remains
    ivalue = sdata_all.toInt();//convert back to int :D
    Serial.println("ivalue");
    Serial.println(ivalue);//print for control

    switch (ichannel) {
      case 11://if channel 1 do:
        if ((ivalue > Servo1min) && (ivalue < Servo1max)) {
          Servo1.writeMicroseconds(ivalue);
        }
        break;
      case 12://if channel 2 do:
        if ((ivalue > Servo2min) && (ivalue < Servo2max)) {
          Servo2.writeMicroseconds(ivalue);
        }
        break;
      case 13://if channel 3 do:
        if (sdata_all.substring(0, 1) == "2") {
          analogWrite(LED_1_PWM, 250);
        }
        else {
          analogWrite(LED_1_PWM, 0);
        }
        if (sdata_all.substring(1, 2) == "2") {
          digitalWrite(LED_2, HIGH);
        }
        else {
          digitalWrite(LED_2, LOW);
        }
        if (sdata_all.substring(2, 3) == "2") {
          digitalWrite(LED_3, HIGH);
        }
        else {
          digitalWrite(LED_3, LOW);
        }
        if (sdata_all.substring(3, 4) == "2") {
          digitalWrite(LED_4, HIGH);
        }
        else {
          digitalWrite(LED_4, LOW);
        }
      case 14://if channel 4 do:
        if (sdata_all.substring(0, 1) == "2") {
          digitalWrite(LED_5_PWM, HIGH);
        }
        else {
          digitalWrite(LED_5_PWM, LOW);
        }
        if (sdata_all.substring(1, 2) == "2") {
          digitalWrite(LED_6, HIGH);
        }
        else {
          digitalWrite(LED_6, LOW);
        }
        if (sdata_all.substring(2, 3) == "2") {
          digitalWrite(LED_7, HIGH);
        }
        else {
          digitalWrite(LED_7, LOW);
        }
        if (sdata_all.substring(3, 4) == "2") {
          digitalWrite(LED_8_PWM, HIGH);
        }
        else {
          digitalWrite(LED_8_PWM, LOW);
        }
      case 51://if receiving startupsettings
        Servo1min = ivalue;
        EEPROM.put(1, Servo1min);
        break;
      case 52:
        Servo1mid = ivalue;
        EEPROM.put(3, Servo1mid);
        break;
      case 53:
        Servo1max = ivalue;
        EEPROM.put(5, Servo1max);
        break;
      case 54:
        Servo2min = ivalue;
        EEPROM.put(7, Servo2min);
        break;
      case 55:
        Servo2mid = ivalue;
        EEPROM.put(9, Servo2mid);
        break;
      case 56:
        Servo2max = ivalue;
        EEPROM.put(11, Servo2max);
        break;

        break;
    }
  }
  // Allow BLE Shield to send/receive data
  ble_do_events();
}

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into sections is fine but large spaces for no reason just make for more scrolling when we're trying to read your code.

You are using SPI.
Which pins does SPI use for MOSI, MISO, SCK, SS (slave) and SS (master) on your Arduino ?

@pert: your're right, i changed it. sorry

@helibob:
SPI is used to communicate with the bluetooth chip, right?
So, I don't use the pins MOSI, MISO, CLK, SS at all.
There are some other pins, like "3: Digital I/O or PWM or I2C SCL". i mean, if there is a "or" i can use it, can't I?

if you scroll down to the end you'll find the complete list of all pins if you need more information:
http://redbearlab.com/blendmicro/

So, I don't use the pins MOSI, MISO, CLK, SS at all.

You may not be using the pins on the board actually marked as such but check out SPI - Arduino Reference and you will see that each of them is actually mapped to numbered pins and they cannot generally be used as general purpose IO pins at the same time as being used for SPI.

I have two servos (actually one servo and one motor) and serveral leds. The servos are connected to pin 9 and 10.

handy reference - second paragraph

@HeliBob:
OK, i disabled all pins that have a second function I can assosciate with SPI. That's pin 0,1,2,3
Still the same.

@groove:
I first thought about that too. But:
1)it doesn't matter if there is a led connected to the pin or not (= no extra currency)
2)the motor still runs. Im just not able to change speed anymore..the arduino definitly gets the signal (i checked it in the serial monitor). it just dosn't change the pwm signal anymore.

the motor still runs. Im just not able to change speed anymore..

That's exactly what I would expect - the pin will behave as a non-PWM pin.
If I remember correctly, analogWrite to such a pin, will write 0 for values below 128 and 1 for all other values.

hm. don't understand :confused: .
wouldn't this mean that my motor would drive at max speed? But it doesn't. The motor has the same speed as before i turned the "pwm-led" on. I just can't change it anymore.
And (i said before, but maybe it just got lost): if i turn on two pwm-outputs, and one of them off again, the motor behaves as usual.

I use writeMicroseconds for the servos. Could this cause a problem?

OK, i disabled all pins that have a second function I can assosciate with SPI. That's pin 0,1,2,3

Which Arduino board are you using ?

Servo::write simply does a map and then calls Servo::writeMicroseconds, IIRC, so I don't see that could be a problem

http://redbearlab.com/blendmicro/

To see the pinout scroll down to the end

OK. So that should eliminate the multiple use of the SPI pins as a possible cause of the problem.

I just tried to handle the led as an additional servo (writeMicroseconds). Did not change anything.

solved. funny story.
I started a new project and just started with some simple code (let servo drive from one side to the other) and added all other code step by step (add BLE, add one servo, second servo, one led, second led...).
Now I have the same code as before, and it's working as it's supposed tol. I don't have no clue why. maybe some code wasn't deleted? Anyway, problem solved. (I hope it will be stable :confused: )

Now I have the same code as before,

I am willing to bet that you don't, or if you do then you have changed the wiring.