Hi all, I'm thinking about making a really cheap amblight clone with a 328p hence using softPWM
I can't get the code to compile, I get these errors:
AmbloneSOFT.cpp: In function 'void SetPWMs()':
AmbloneSOFT.pde:-1: error: 'r4_current' was not declared in this scope
AmbloneSOFT.pde:-1: error: 'g4_current' was not declared in this scope
AmbloneSOFT.pde:-1: error: 'b4_current' was not declared in this scope
I'm not sure why because the variables are declared globally I thought?
Here is the code:
#include <SoftPWM.h>
#define C_SF1 0xF1 // Startflag for 1-channel mode (1 RGB channel)
#define C_SF2 0xF2 // Startflag for 2-channel mode (2 RGB channels)
#define C_SF3 0xF3 // Startflag for 3-channel mode (3 RGB channels)
#define C_SF4 0xF4 // Startflag for 4-channel mode (4 RGB channels)
#define C_END 0x33 // End flag
#define C_ESC 0x99 // Escape character
// States for receiving the information, see the flow chart for more info
#define S_WAIT_FOR_SF 0
#define S_RECV_RGB 1
#define S_RECV_RGB_ESC 2
//---------------------------------------------------------------------------
//--------------------------- FUNCTION DECLARATIONS -------------------------
//---------------------------------------------------------------------------
// Receives bytes and returns true if a valid packet was received
boolean PacketReceived();
// Uses the rgb values to set the PWMs
void SetPWMs();
//---------------------------------------------------------------------------
//--------------------------- VARIABLE DECLARATIONS -------------------------
//---------------------------------------------------------------------------
int pulse = 0;
// State we are in: one of the S_* defines
int State = 0;
// The payload of a received message
int Payload[32];
// The amount of RGB values we have received
int ByteCount = 0;
// The character we received
int Recv;
// The amount of RGB channels we are using
int ChannelMode;
// The value for easing (lower is more)
// For more easing adjust the delay in loop()
int easing = 1;
// PWM pins for channel 1
int r1_pin = 2;
int g1_pin = 3;
int b1_pin = 4;
// PWM pins for channel 2
int r2_pin = 5;
int g2_pin = 6;
int b2_pin = 7;
// PWM pins for channel 3
int r3_pin = 8;
int g3_pin = 9;
int b3_pin = 10;
// PWM pins for channel 4
int r4_pin = 11;
int g4_pin = 12;
int b4_pin = 13;
// Current PWM output values (as opposed to target output values)
int r1_current = 0;
int g1_current = 0;
int b1_current = 0;
int r2_current = 0;
int g2_current = 0;
int b2_current = 0;
int r3_current = 0;
int g3_current = 0;
int b3_current = 0;
//---------------------------------------------------------------------------
//----------------------------- IMPLEMENTATIONS -----------------------------
//---------------------------------------------------------------------------
void setup() {
// initialize the serial communication
Serial.begin(256000); // opens serial port, sets data rate to 256000 bps
TCCR0B = TCCR0B & 0b11111000 | 0x2;
TCCR1B = TCCR0B & 0b11111000 | 0x2;
TCCR2B = TCCR0B & 0b11111000 | 0x2;
State = S_WAIT_FOR_SF;
SoftPWMBegin();
}
//---------------------------------------------------------------------------
void loop()
{
if (Serial.available() > 0) {
PacketReceived();
}
SetPWMs();
delay(10);
}
//---------------------------------------------------------------------------
boolean PacketReceived() {
Recv = Serial.read();
switch (State) {
case S_WAIT_FOR_SF:
// =============================== Wait for start flag state
switch (Recv) {
case C_SF1:
// Start flag for 1-channel mode
ChannelMode = 1;
State = S_RECV_RGB;
ByteCount = 0;
return false;
case C_SF2:
// Start flag for 2-channel mode
ChannelMode = 2;
State = S_RECV_RGB;
ByteCount = 0;
return false;
case 243://C_SF3:
// Start flag for 3-channel mode
ChannelMode = 3;
State = S_RECV_RGB;
ByteCount = 0;
return false;
case C_SF4:
// Start flag for 4-channel mode
ChannelMode = 4;
State = S_RECV_RGB;
ByteCount = 0;
return false;
default:
// No action for all other characters
return false;
}
break;
case S_RECV_RGB:
// =============================== RGB Data reception state
switch (Recv) {
case C_SF1:
// Start flag for 1-channel mode
ChannelMode = 1;
State = S_RECV_RGB;
ByteCount = 0;
return false;
case C_SF2:
// Start flag for 2-channel mode
ChannelMode = 2;
State = S_RECV_RGB;
ByteCount = 0;
return false;
case C_SF3:
// Start flag for 3-channel mode
ChannelMode = 3;
State = S_RECV_RGB;
ByteCount = 0;
return false;
case C_SF4:
// Start flag for 4-channel mode
ChannelMode = 4;
State = S_RECV_RGB;
ByteCount = 0;
return false;
case C_END:
// End Flag
// For each channel, we should have received 3 values. If so, we have received a valid packet
if (ByteCount == ChannelMode * 3) {
State = S_WAIT_FOR_SF;
ByteCount = 0;
return true; // <------------------------ TRUE IS RETURNED
}
else {
// Something's gone wrong: restart
State = S_WAIT_FOR_SF;
ByteCount = 0;
return false;
}
case C_ESC:
// Escape character
State = S_RECV_RGB_ESC;
return false;
default:
// The character received wasn't a flag, so store it as an RGB value
Payload[ByteCount] = Recv;
ByteCount++;
return false;
}
case S_RECV_RGB_ESC:
// =============================== RGB Escaped data reception state
// Store the value in the payload, no matter what it is
Payload[ByteCount] = Recv;
ByteCount++;
State = S_RECV_RGB;
return false;
}
return false;
}
//---------------------------------------------------------------------------
void SetPWMs() {
// Channel 1
r1_current = rgbStep(r1_current, Payload[0]);
g1_current = rgbStep(g1_current, Payload[1]);
b1_current = rgbStep(b1_current, Payload[2]);
SoftPWMSet(r1_pin, r1_current);
SoftPWMSet(g1_pin, g1_current);
SoftPWMSet(b1_pin, b1_current);
// Channel 2
if (ChannelMode > 1) {
r2_current = rgbStep(r2_current, Payload[3]);
g2_current = rgbStep(g2_current, Payload[4]);
b2_current = rgbStep(b2_current, Payload[5]);
SoftPWMSet(r2_pin, r2_current);
SoftPWMSet(g2_pin, g2_current);
SoftPWMSet(b2_pin, b2_current);
}
else {
// turn the rest to 0 (black)
SoftPWMSet(r2_pin, 0);
SoftPWMSet(g2_pin, 0);
SoftPWMSet(b2_pin, 0);
SoftPWMSet(r3_pin, 0);
SoftPWMSet(g3_pin, 0);
SoftPWMSet(b3_pin, 0);
SoftPWMSet(r4_pin, 0);
SoftPWMSet(g4_pin, 0);
SoftPWMSet(b4_pin, 0);
}
// Channel 3
if (ChannelMode > 2) {
r3_current = rgbStep(r3_current, Payload[6]);
g3_current = rgbStep(g3_current, Payload[7]);
b3_current = rgbStep(b3_current, Payload[8]);
SoftPWMSet(r3_pin, r3_current);
SoftPWMSet(g3_pin, g3_current);
SoftPWMSet(b3_pin, b3_current);
}
else {
// turn the rest to 0 (black)
SoftPWMSet(r3_pin, 0);
SoftPWMSet(g3_pin, 0);
SoftPWMSet(b3_pin, 0);
SoftPWMSet(r4_pin, 0);
SoftPWMSet(g4_pin, 0);
SoftPWMSet(b4_pin, 0);
}
// Channel 4
if (ChannelMode > 3) {
r4_current = rgbStep(r4_current, Payload[9]);
g4_current = rgbStep(g4_current, Payload[10]);
b4_current = rgbStep(b4_current, Payload[11]);
SoftPWMSet(r4_pin, r4_current);
SoftPWMSet(g4_pin, g4_current);
SoftPWMSet(b4_pin, b4_current);
}
else {
// turn the rest to 0 (black)
SoftPWMSet(r4_pin, 0);
SoftPWMSet(g4_pin, 0);
SoftPWMSet(b4_pin, 0);
}
}
//---------------------------------------------------------------------------
// rgbStep - the function for easing the colour changes
int rgbStep(int from, int to){
if(to > from){ // destination colour is greater than current colour
from += easing;
if(from > 255){
from = 255;
}
return from;
}
else if(to < from){ // current colour is greater than destination colour
from -= easing;
if(from < 0){
from = 0;
}
return from;
}
else{ // values are the same...return as submitted
return from;
}
}