Grumpy_Mike:
That is why we kept asking you to POST ALL YOUR CODE, you would have been forced to copy and paste it.
That is true, I'll keep in mind this!
Grumpy_Mike:
Well it is rare that a "what's wrong with this code" has an error that corrects the problem.
Rare but not impossible, you can check by your own if you want that (incredibly, probably when I wrote those lines I was a bit careless, who knows!) the semicomma in that specific point didn't let the "if statament" do its work properly.
CODE UNCORRECT
#define SER 13 // Serial data on pin 13
#define RCLK 12 // 74HC595 register clock on pin 12
#define SHCLK 11 // 74HC595 shift clock on pin 11
#define ROWS 8 // Number of rows set by the designers - we are going to make this value changable by the operator to
// let him control different kind of hardware boards with different setups
#define COLS 8 // Numbers of columns set by the designer (same as above)
#define PWMbits 4 /* Every color has 4 bits, so that the number of color results to be: 2^4 * 2^4 * 2^4 = 4096 - we are going
to make this value changable by the operator in order to let him control the colors setup */
#include <Led.h>
Led led[8]; // Object belonging to the Led class
uint32_t value = 0; // Variable where to store the whole 4 bytes that have to be sent at the shift registers
void enableOuts (uint32_t Out);
void setup()
{
Serial.begin(115200); // Set the serial comunication speed
pinMode(SER, OUTPUT); // Set inputs/outputs
pinMode(SHCLK, OUTPUT);
pinMode(RCLK, OUTPUT);
}
void loop()
{
if (Serial.available() > 23);
{
// Reads the upcoming 24 bytes
for (int i = 0; i < 8; i++)
{
led[i].red = Serial.read() - '0';
led[i].green = Serial.read() - '0';
led[i].blue = Serial.read() - '0';
Serial.print("red["); Serial.print(i); Serial.print("] = ");
Serial.println(led[i].red);
Serial.print("green["); Serial.print(i); Serial.print("] = ");
Serial.println(led[i].green);
Serial.print("blue["); Serial.print(i); Serial.print("] = ");
Serial.println(led[i].blue);
}
}
// Shift the colors in the correct order and store the data into value
for (int row = 0; row < pow(2, ROWS); row *= 2)
{
for (int j = 0; j < pow(2, PWMbits); j++)
{
for (int col = COLS; col > 0; col--)
{
value = ((led[col].blue && 1) << (32 - col)) | ((led[col].green && 1) << (24 - col)) | ((led[col].red && 1) << (16 - col)) | row;
(((led[col].blue && 1) > 0) ? led[col].blue-- : led[col].blue = 0);
(((led[col].green && 1) > 0) ? led[col].green-- : led[col].green = 0);
(((led[col].red && 1) > 0) ? led[col].red-- : led[col].red = 0);
}
enableOuts(value);
}
}
}
void enableOuts(uint32_t Out)
{
for (byte c = 0; c < 32; c++)
{
(((Out & (1 << c)) == 1) ? digitalWrite(SER, HIGH) : digitalWrite(SER, LOW));
digitalWrite(SHCLK, HIGH);
digitalWrite(SHCLK, LOW);
}
digitalWrite(RCLK, HIGH);
digitalWrite(RCLK, LOW);
}
CORRECT CODE
#define SER 13 // Serial data on pin 13
#define RCLK 12 // 74HC595 register clock on pin 12
#define SHCLK 11 // 74HC595 shift clock on pin 11
#define ROWS 8 // Number of rows set by the designers - we are going to make this value changable by the operator to
// let him control different kind of hardware boards with different setups
#define COLS 8 // Numbers of columns set by the designer (same as above)
#define PWMbits 4 /* Every color has 4 bits, so that the number of color results to be: 2^4 * 2^4 * 2^4 = 4096 - we are going
to make this value changable by the operator in order to let him control the colors setup */
#include <Led.h>
Led led[8]; // Object belonging to the Led class
uint32_t value = 0; // Variable where to store the whole 4 bytes that have to be sent at the shift registers
void enableOuts (uint32_t Out);
void setup()
{
Serial.begin(115200); // Set the serial comunication speed
pinMode(SER, OUTPUT); // Set inputs/outputs
pinMode(SHCLK, OUTPUT);
pinMode(RCLK, OUTPUT);
}
void loop()
{
if (Serial.available() > 23)
{
// Reads the upcoming 24 bytes
for (int i = 0; i < 8; i++)
{
led[i].red = Serial.read() - '0';
led[i].green = Serial.read() - '0';
led[i].blue = Serial.read() - '0';
Serial.print("red["); Serial.print(i); Serial.print("] = ");
Serial.println(led[i].red);
Serial.print("green["); Serial.print(i); Serial.print("] = ");
Serial.println(led[i].green);
Serial.print("blue["); Serial.print(i); Serial.print("] = ");
Serial.println(led[i].blue);
}
}
// Shift the colors in the correct order and store the data into value
for (int row = 0; row < pow(2, ROWS); row *= 2)
{
for (int j = 0; j < pow(2, PWMbits); j++)
{
for (int col = COLS; col > 0; col--)
{
value = ((led[col].blue && 1) << (32 - col)) | ((led[col].green && 1) << (24 - col)) | ((led[col].red && 1) << (16 - col)) | row;
(((led[col].blue && 1) > 0) ? led[col].blue-- : led[col].blue = 0);
(((led[col].green && 1) > 0) ? led[col].green-- : led[col].green = 0);
(((led[col].red && 1) > 0) ? led[col].red-- : led[col].red = 0);
}
enableOuts(value);
}
}
}
void enableOuts(uint32_t Out)
{
for (byte c = 0; c < 32; c++)
{
(((Out & (1 << c)) == 1) ? digitalWrite(SER, HIGH) : digitalWrite(SER, LOW));
digitalWrite(SHCLK, HIGH);
digitalWrite(SHCLK, LOW);
}
digitalWrite(RCLK, HIGH);
digitalWrite(RCLK, LOW);
}
As you can see the "if" only executes a NULL when 24+ bytes are incoming.
Thanks for your tips anyway (;
Matt
PS: now I need to improve the algorithm, in case I have problems I'll post here