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();
}