Skip to main content
Arduino

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Ended up going with something like this like this:

Ended up going with something like this:

Ended up going with something like this:

added 19 characters in body
Source Link
gotnull
  • 145
  • 8
#include <RFduinoBLE.h>
// State properties.
int state = 1;
char command;
String hexstring;
// RGB pins.
int redPin = 2;
int grnPin = 3;
int bluPin = 4;
// Setup function to set RGB pins to OUTPUT pins.
void setup () {
 pinMode(redPin, OUTPUT);
 pinMode(grnPin, OUTPUT);
 pinMode(bluPin, OUTPUT);
 // This is the data we want to appear in the advertisement
 // (the deviceName length plus the advertisement length must be <= 18 bytes.
 RFduinoBLE.deviceName = "iOS";
 RFduinoBLE.advertisementInterval = MILLISECONDS(300);
 RFduinoBLE.txPowerLevel = -20;
 RFduinoBLE.advertisementData = "rgb";
 // Start the BLE stack.
 RFduinoBLE.begin();
}
void loop () {
 switch (command) {
 case 1:
 // Blink.
 break;
 case 2:
 // Fade.
 break;
 }
 //RFduino_ULPDelay(INFINITE);
}
// Converts HEX as a String to actual HEX values.
// This is needed to properly convert the ASCII value to the hex
// value of each character.
byte getVal (char c) {
 if (c >= '0' && c <= '9') return (byte)(c - '0');
 else return (byte)(c - 'a' + 10);
}
// Process each function/command.
void processCommand (int command, String hex) {
 switch (command) {
 case 'b':
 command = 1; // Set blink mode.
 break;
 case 'f':
 command = 2; // Set fade mode.
 break;
 case 'c':
 // We put together 2 characters as is
 // done with HEX notation and set the color.
 byte red = getVal(hex.charAt(1)) + (getVal(hex.charAt(0)) << 4);
 byte green = getVal(hex.charAt(3)) + (getVal(hex.charAt(2)) << 4);
 byte blue = getVal(hex.charAt(5)) + (getVal(hex.charAt(4)) << 4);
 // Set the color.
 setColor (red, green, blue);
 break;
 }
}
// Sets the color of each RGB pin.
void setColor (byte red, byte green, byte blue) {
 analogWrite(redPin, red);
 analogWrite(grnPin, green);
 analogWrite(bluPin, blue);
 delay(200);
}
// This function returns data from the radio.
void RFduinoBLE_onReceive (char *data, int len) {
 for (int i = 0; i < len; i++) {
 stateMachine(data[i]);
 }
}
// Main state machine function, which processes
// data depending on the bytes received.
void stateMachine (char data) {
 switch (state) {
 case 1:
 if (data == 1) {
 state = 2;
 }
 break;
 case 2:
 if (data == 'b' || data == 'f' || data == 'c') {
 command = data;
 hexstring = "";
 state = 3;
 } else if (data != 1) { // Stay in state 2 if we received another 0x01.
 state = 1;
 }
 break;
 case 3:
 if ((data >= 'a' && data <= 'z') || (data >= '0' && data <= '9')) {
 hexstring = hexstring + data;
 if (hexstring.length() == 6) {
 state = 4;
 }
 } else if (data == 1) {
 state = 2;
 } else {
 state = 1;
 }
 break;
 case 4:
 if (data == 3) {
 processCommand(command, hexstring);
 state = 1;
 } else if (data == 1) {
 state = 2;
 } else {
 state = 1;
 }
 break;
 }
}
#include <RFduinoBLE.h>
// State properties.
int state = 1;
char command;
String hexstring;
// RGB pins.
int redPin = 2;
int grnPin = 3;
int bluPin = 4;
// Setup function to set RGB pins to OUTPUT pins.
void setup () {
 pinMode(redPin, OUTPUT);
 pinMode(grnPin, OUTPUT);
 pinMode(bluPin, OUTPUT);
 // This is the data we want to appear in the advertisement
 // (the deviceName length plus the advertisement length must be <= 18 bytes.
 RFduinoBLE.deviceName = "iOS";
 RFduinoBLE.advertisementInterval = MILLISECONDS(300);
 RFduinoBLE.txPowerLevel = -20;
 RFduinoBLE.advertisementData = "rgb";
 // Start the BLE stack.
 RFduinoBLE.begin();
}
void loop () {
 switch (command) {
 case 1:
 // Blink.
 break;
 case 2:
 // Fade.
 break;
 }
 //RFduino_ULPDelay(INFINITE);
}
// Converts HEX as a String to actual HEX values.
// This is needed to properly convert the ASCII value to the hex
// value of each character.
byte getVal (char c) {
 if (c >= '0' && c <= '9') return (byte)(c - '0');
 else return (byte)(c - 'a' + 10);
}
// Process each function/command.
void processCommand (int command, String hex) {
 switch (command) {
 case 'b':
 command = 1; // Set blink mode.
 break;
 case 'f':
 command = 2; // Set fade mode.
 break;
 case 'c':
 // We put together 2 characters as is
 // done with HEX notation and set the color.
 byte red = getVal(hex.charAt(1)) + (getVal(hex.charAt(0)) << 4);
 byte green = getVal(hex.charAt(3)) + (getVal(hex.charAt(2)) << 4);
 byte blue = getVal(hex.charAt(5)) + (getVal(hex.charAt(4)) << 4);
 // Set the color.
 setColor (red, green, blue);
 break;
 }
}
// Sets the color of each RGB pin.
void setColor (byte red, byte green, byte blue) {
 analogWrite(redPin, red);
 analogWrite(grnPin, green);
 analogWrite(bluPin, blue);
}
// This function returns data from the radio.
void RFduinoBLE_onReceive (char *data, int len) {
 for (int i = 0; i < len; i++) {
 stateMachine(data[i]);
 }
}
// Main state machine function, which processes
// data depending on the bytes received.
void stateMachine (char data) {
 switch (state) {
 case 1:
 if (data == 1) {
 state = 2;
 }
 break;
 case 2:
 if (data == 'b' || data == 'f' || data == 'c') {
 command = data;
 hexstring = "";
 state = 3;
 } else if (data != 1) { // Stay in state 2 if we received another 0x01.
 state = 1;
 }
 break;
 case 3:
 if ((data >= 'a' && data <= 'z') || (data >= '0' && data <= '9')) {
 hexstring = hexstring + data;
 if (hexstring.length() == 6) {
 state = 4;
 }
 } else if (data == 1) {
 state = 2;
 } else {
 state = 1;
 }
 break;
 case 4:
 if (data == 3) {
 processCommand(command, hexstring);
 state = 1;
 } else if (data == 1) {
 state = 2;
 } else {
 state = 1;
 }
 break;
 }
}
#include <RFduinoBLE.h>
// State properties.
int state = 1;
char command;
String hexstring;
// RGB pins.
int redPin = 2;
int grnPin = 3;
int bluPin = 4;
// Setup function to set RGB pins to OUTPUT pins.
void setup () {
 pinMode(redPin, OUTPUT);
 pinMode(grnPin, OUTPUT);
 pinMode(bluPin, OUTPUT);
 // This is the data we want to appear in the advertisement
 // (the deviceName length plus the advertisement length must be <= 18 bytes.
 RFduinoBLE.deviceName = "iOS";
 RFduinoBLE.advertisementInterval = MILLISECONDS(300);
 RFduinoBLE.txPowerLevel = -20;
 RFduinoBLE.advertisementData = "rgb";
 // Start the BLE stack.
 RFduinoBLE.begin();
}
void loop () {
 switch (command) {
 case 1:
 // Blink.
 break;
 case 2:
 // Fade.
 break;
 }
 //RFduino_ULPDelay(INFINITE);
}
// Converts HEX as a String to actual HEX values.
// This is needed to properly convert the ASCII value to the hex
// value of each character.
byte getVal (char c) {
 if (c >= '0' && c <= '9') return (byte)(c - '0');
 else return (byte)(c - 'a' + 10);
}
// Process each function/command.
void processCommand (int command, String hex) {
 switch (command) {
 case 'b':
 command = 1; // Set blink mode.
 break;
 case 'f':
 command = 2; // Set fade mode.
 break;
 case 'c':
 // We put together 2 characters as is
 // done with HEX notation and set the color.
 byte red = getVal(hex.charAt(1)) + (getVal(hex.charAt(0)) << 4);
 byte green = getVal(hex.charAt(3)) + (getVal(hex.charAt(2)) << 4);
 byte blue = getVal(hex.charAt(5)) + (getVal(hex.charAt(4)) << 4);
 // Set the color.
 setColor (red, green, blue);
 break;
 }
}
// Sets the color of each RGB pin.
void setColor (byte red, byte green, byte blue) {
 analogWrite(redPin, red);
 analogWrite(grnPin, green);
 analogWrite(bluPin, blue);
 delay(200);
}
// This function returns data from the radio.
void RFduinoBLE_onReceive (char *data, int len) {
 for (int i = 0; i < len; i++) {
 stateMachine(data[i]);
 }
}
// Main state machine function, which processes
// data depending on the bytes received.
void stateMachine (char data) {
 switch (state) {
 case 1:
 if (data == 1) {
 state = 2;
 }
 break;
 case 2:
 if (data == 'b' || data == 'f' || data == 'c') {
 command = data;
 hexstring = "";
 state = 3;
 } else if (data != 1) { // Stay in state 2 if we received another 0x01.
 state = 1;
 }
 break;
 case 3:
 if ((data >= 'a' && data <= 'z') || (data >= '0' && data <= '9')) {
 hexstring = hexstring + data;
 if (hexstring.length() == 6) {
 state = 4;
 }
 } else if (data == 1) {
 state = 2;
 } else {
 state = 1;
 }
 break;
 case 4:
 if (data == 3) {
 processCommand(command, hexstring);
 state = 1;
 } else if (data == 1) {
 state = 2;
 } else {
 state = 1;
 }
 break;
 }
}
Source Link
gotnull
  • 145
  • 8

Ended up going with something like this:

#include <RFduinoBLE.h>
// State properties.
int state = 1;
char command;
String hexstring;
// RGB pins.
int redPin = 2;
int grnPin = 3;
int bluPin = 4;
// Setup function to set RGB pins to OUTPUT pins.
void setup () {
 pinMode(redPin, OUTPUT);
 pinMode(grnPin, OUTPUT);
 pinMode(bluPin, OUTPUT);
 // This is the data we want to appear in the advertisement
 // (the deviceName length plus the advertisement length must be <= 18 bytes.
 RFduinoBLE.deviceName = "iOS";
 RFduinoBLE.advertisementInterval = MILLISECONDS(300);
 RFduinoBLE.txPowerLevel = -20;
 RFduinoBLE.advertisementData = "rgb";
 // Start the BLE stack.
 RFduinoBLE.begin();
}
void loop () {
 switch (command) {
 case 1:
 // Blink.
 break;
 case 2:
 // Fade.
 break;
 }
 //RFduino_ULPDelay(INFINITE);
}
// Converts HEX as a String to actual HEX values.
// This is needed to properly convert the ASCII value to the hex
// value of each character.
byte getVal (char c) {
 if (c >= '0' && c <= '9') return (byte)(c - '0');
 else return (byte)(c - 'a' + 10);
}
// Process each function/command.
void processCommand (int command, String hex) {
 switch (command) {
 case 'b':
 command = 1; // Set blink mode.
 break;
 case 'f':
 command = 2; // Set fade mode.
 break;
 case 'c':
 // We put together 2 characters as is
 // done with HEX notation and set the color.
 byte red = getVal(hex.charAt(1)) + (getVal(hex.charAt(0)) << 4);
 byte green = getVal(hex.charAt(3)) + (getVal(hex.charAt(2)) << 4);
 byte blue = getVal(hex.charAt(5)) + (getVal(hex.charAt(4)) << 4);
 // Set the color.
 setColor (red, green, blue);
 break;
 }
}
// Sets the color of each RGB pin.
void setColor (byte red, byte green, byte blue) {
 analogWrite(redPin, red);
 analogWrite(grnPin, green);
 analogWrite(bluPin, blue);
}
// This function returns data from the radio.
void RFduinoBLE_onReceive (char *data, int len) {
 for (int i = 0; i < len; i++) {
 stateMachine(data[i]);
 }
}
// Main state machine function, which processes
// data depending on the bytes received.
void stateMachine (char data) {
 switch (state) {
 case 1:
 if (data == 1) {
 state = 2;
 }
 break;
 case 2:
 if (data == 'b' || data == 'f' || data == 'c') {
 command = data;
 hexstring = "";
 state = 3;
 } else if (data != 1) { // Stay in state 2 if we received another 0x01.
 state = 1;
 }
 break;
 case 3:
 if ((data >= 'a' && data <= 'z') || (data >= '0' && data <= '9')) {
 hexstring = hexstring + data;
 if (hexstring.length() == 6) {
 state = 4;
 }
 } else if (data == 1) {
 state = 2;
 } else {
 state = 1;
 }
 break;
 case 4:
 if (data == 3) {
 processCommand(command, hexstring);
 state = 1;
 } else if (data == 1) {
 state = 2;
 } else {
 state = 1;
 }
 break;
 }
}

AltStyle によって変換されたページ (->オリジナル) /