Hi all,
Working on a project for an arduino328 based device (Uno, pro mini etc) which can control 2 channels of RGB lighting with fading between colours and transistions. It takes it's commands via I2C or TTL serial and responds appropriately.
For example, if I sent the TTL command "L1S,255,255,255.\n" , this would tell the module that I want:
Light channel 1, Set RGB values R, G, B, stop.
What I have a problem with is extracting that 8 bit value from the serial string to a usable integer. It's probably simple but I always fall at this stage. I2C works fine because I just treat the RGB values as raw 8-bit data during transfers.
The current command handling is probably sloppy and also doesn't use the ttlColour array at the minute since I'm focusing on getting the parser working then update the second stage. Also I don't want to use my old method of Alt-0"value" for the RGB values since, altough it worked, it wasn't convenient especially when using a terminal which I couldn't use that keyboard combo with
Any help would be appreicated
The current code I have is as follows (edited out none related functions):
#include <Wire.h>
//#include <SPI.h>
#include "defs.h"
#include <stdlib.h>
unsigned long currentTime = 0;
char iicReceived[16] = {};
byte iicToSend[8] = {};
byte iicLength = 0;
byte iicSendLength = 0;
// 8-Bit array to hold RGB/w? brightness for comms
byte ttlColour[4] = {};
struct RGB {
byte red;
byte green;
byte blue;
byte setRed;
byte setGreen;
byte setBlue;
bool fade = false;
};
// Dual channel RGB
RGB rgb1 = {};
RGB rgb2 = {};
bool cyclicPattern = 0;
void setup() {
Wire.begin(moduleAddr);
Wire.onRequest(iicReq);
Wire.onReceive(iicRec);
Serial.begin(57600);
pinMode(red1, OUTPUT);
pinMode(green1, OUTPUT);
pinMode(blue1, OUTPUT);
pinMode(red2, OUTPUT);
pinMode(green2, OUTPUT);
pinMode(blue2, OUTPUT);
// Intro
Serial.println();
Serial.print(F(moduleID));
Serial.print(F(" - "));
Serial.println(programVer);
Serial.println(F(make));
Serial.println(F("---------"));
Serial.print(F("Type: "));
Serial.println(moduleType);
Serial.print(F("Type Ver: "));
Serial.println(moduleVer);
Serial.print(F("Chipset: "));
Serial.println(AVR_BOARD);
Serial.print( F("Compiled: "));
Serial.print( F(__DATE__));
Serial.print( F(", "));
Serial.println( F(__TIME__));
Serial.println(F("-----------------------"));
}
void loop() {
// Receive from host
if (Serial.available()) {
serialRec();
}
// Transmit to host
if ( iicSendLength > 0) {
for (int i = 0; i < iicSendLength; i++) {
Serial.print(iicToSend[i], HEX);
Serial.print(", ");
}
Serial.println();
for (int i = 0; i < iicSendLength; i++) {
char temp = iicToSend[i];
Serial.print(temp);
Serial.print(", ");
}
Serial.println();
memset(iicToSend, 0, sizeof(iicToSend));
iicSendLength = 0;
//serialCom = 0;
}
if (cyclicPattern == 1) cycleRGB(1);
currentTime = millis();
fadeRGB(currentTime);
ioUpdate(currentTime);
}
// Parse input from TTL
void serialRec() {
bool endMsg = 0;
while (Serial.available() > 0 && endMsg == 0) {
char rc = Serial.read();
if (rc == '\n') {
endMsg = 1;
} else {
iicReceived[iicLength] = rc;
iicLength++;
}
}
if (endMsg == 1) {
byte colour = 0; // 0 = red, 1 = green, 2 = blue, 3 = white?
Serial.print(F("Serial: "));
for (int i = 0; i < iicLength; i++) {
// Parse for RGB values if present
Serial.print(iicReceived[i]);
if (iicReceived[i] == ',') {
Serial.println();
char val[4] = {};
for (byte c = 0; c < 3; c++) {
i++;
Serial.print(iicReceived[i]);
if (iicReceived[i] == ',' || iicReceived[i] == '.') {
val[c] = '\0';
ttlColour[0] = atoi(val);
Serial.println(val);
Serial.println(ttlColour[0]);
break;
}
else val[c] += iicReceived[i];
}
//ttlColour[colour] =
}
}
Serial.println();
if (!commandCode()) {
Serial.println(F("Command not found"));
}
//serialCom = 1;
memset(iicReceived, 0, sizeof(iicReceived));
iicLength = 0;
endMsg = 0;
}
}
bool commandCode() {
bool exitCode = 0;
switch (iicReceived[0]) {
case 'B': // Assume broadcast ID.
switch (iicReceived[1]) {
case 'I':
//Serial.println(F("Broadcast ID"));
iicToSend[0] = moduleAddr;
iicSendLength = 1;
exitCode = 1;
break;
case 'N':
//Serial.println(F("Broadcast Name"));
iicToSend[0] = moduleAddr;
iicSendLength = 1;
exitCode = 1;
break;
}
break;
case 'C':
Serial.print("Cycle pattern ");
if (cyclicPattern == 0) {
cyclicPattern = 1;
fadeRate = 50;
Serial.println("on.");
}
else {
Serial.println("off.");
cyclicPattern = 0;
fadeRate = 30;
setRGB(0, 0, 0, 1);
}
exitCode = 1;
break;
case 'L':
//Serial.print(F("Light Channel "));
byte channel = iicReceived[1] - '0';
//Serial.print(channel);
char mode = iicReceived[2];
switch (mode) {
case 'S':
//Serial.println(F(" set"));
setRGB(iicReceived[3], iicReceived[4], iicReceived[5], channel);
exitCode = 1;
break;
case '0':
//Serial.println(F(" blank"));
setRGB(0, 0, 0, channel);
exitCode = 1;
break;
case 'T':
//Serial.print(F(" target"));
if (channel == 1) {
iicToSend[0] = rgb1.setRed;
iicToSend[1] = rgb1.setGreen;
iicToSend[2] = rgb1.setBlue;
iicSendLength = 3;
exitCode = 1;
} else if (channel == 2) {
iicToSend[0] = rgb2.setRed;
iicToSend[1] = rgb2.setGreen;
iicToSend[2] = rgb2.setBlue;
iicSendLength = 3;
exitCode = 1;
}
break;
case 'A':
//Serial.print(F(" actual"));
if (channel == 1) {
iicToSend[0] = rgb1.red;
iicToSend[1] = rgb1.green;
iicToSend[2] = rgb1.blue;
iicSendLength = 3;
exitCode = 1;
} else if (channel == 2) {
iicToSend[0] = rgb2.red;
iicToSend[1] = rgb2.green;
iicToSend[2] = rgb2.blue;
iicSendLength = 3;
exitCode = 1;
}
break;
case 'D':
Serial.println("Daylight White");
setRGB(255, 255, 255, channel);
exitCode = 1;
break;
case 'W':
Serial.println("Warm White");
setRGB(255, 103, 64, channel);
exitCode = 1;
break;
case 'R':
setRGB(255, 0, 0, channel);
exitCode = 1;
break;
case 'B':
setRGB(0, 255, 0, channel);
exitCode = 1;
break;
case 'G':
setRGB(0, 0, 255, channel);
exitCode = 1;
break;
case 'C':
//Serial.println(F(" program colour"));
char colour = iicReceived[3];
if (colour == 'R') {
if (channel == 1) rgb1.setRed = iicReceived[4];
else if (channel == 2) rgb2.setRed = iicReceived[4];
rgb1.fade = true;
rgb2.fade = true;
exitCode = 1;
} else if (colour == 'G') {
if (channel == 1) rgb1.setGreen = iicReceived[4];
else if (channel == 2) rgb2.setGreen = iicReceived[4];
rgb1.fade = true;
rgb2.fade = true;
exitCode = 1;
} else if (colour == 'B') {
if (channel == 1) rgb1.setBlue = iicReceived[4];
else if (channel == 2) rgb2.setBlue = iicReceived[4];
rgb1.fade = true;
rgb2.fade = true;
exitCode = 1;
}
break;
}
//Serial.println();
if (exitCode != 0 && cyclicPattern == 1) {
Serial.println("Cycle pattern off.");
cyclicPattern = 0;
fadeRate = 30;
}
break;
}
return exitCode;
}