First I used "Analyse_MulticolorLamp", that decodes what is received from the bluetooth serial device.
I use 2 and 3 as SoftSerial for my Bluetooth module.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
Serial.println("send any byte and I'll tell you everything I can about it");
Serial.println();
}
void loop() {
// get any incoming bytes:
if (mySerial.available() > 0) {
int thisChar = mySerial.read();
// say what was sent:
Serial.print("You sent me: \'");
Serial.write(thisChar);
Serial.print("\' ASCII Value: ");
Serial.print(thisChar);
Serial.print(" ");
// Analyze what was sent:
if(isAlpha(thisChar)) {
Serial.println("it's alphabetic");
}
if(isControl(thisChar)) {
Serial.println("it's a control character");
}
if(isDigit(thisChar)) {
Serial.println("it's a numeric digit");
}
}
}
And then I found some code from the book "Beginning Arduino" that fitted my purpose nicely.
// Project 10 - Serial controlled RGB Lamp
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
char buffer[18];
int red, green, blue;
int RedPin = 9;
int GreenPin = 10;
int BluePin = 11;
void setup()
{
Serial.begin(9600);
Serial.flush();
mySerial.begin(9600);
pinMode(RedPin, OUTPUT);
pinMode(GreenPin, OUTPUT);
pinMode(BluePin, OUTPUT);
}
void loop()
{
if (mySerial.available() > 0) {
int index=0;
delay(100); // let the buffer fill up
int numChar = mySerial.available();
if (numChar>15) {
numChar=15;
}
while (numChar--) {
buffer[index++] = mySerial.read();
}
splitString(buffer);
}
}
void splitString(char* data) {
Serial.print("Data entered: ");
Serial.println(data);
char* parameter;
parameter = strtok (data, " ,");
while (parameter != NULL) {
setLED(parameter);
parameter = strtok (NULL, " ,");
}
// Clear the text and serial buffers
for (int x=0; x<16; x++) {
buffer[x]='\0';
}
Serial.flush();
}
void setLED(char* data) {
if ((data[0] == 'r') || (data[0] == 'R')) {
int Ans = strtol(data+1, NULL, 10);
Ans = constrain(Ans,0,255);
analogWrite(RedPin, Ans);
Serial.print("Red is set to: ");
Serial.println(Ans);
}
if ((data[0] == 'g') || (data[0] == 'G')) {
int Ans = strtol(data+1, NULL, 10);
Ans = constrain(Ans,0,255);
analogWrite(GreenPin, Ans);
Serial.print("Green is set to: ");
Serial.println(Ans);
}
if ((data[0] == 'b') || (data[0] == 'B')) {
int Ans = strtol(data+1, NULL, 10);
Ans = constrain(Ans,0,255);
analogWrite(BluePin, Ans);
Serial.print("Blue is set to: ");
Serial.println(Ans);
}
}
And now I have a RGB controlled lamp without using the Amarino library that is using the hardcoded serial port.