int redPin = A5;
int greenPin = A4;
int bluePin = A3;
int redValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255
int greenValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255
int blueValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255
int ledPin = D2;
int ledPin2 = D3;
int ledControl(String command)
{
int state = LOW;
// find out the state of the led
if(command == "HIGH" || command == "HI"){
state = HIGH;
}else if(command == "LOW" || command == "LO"){
state = LOW;
}else{
return -1;
}
// write to the appropriate pin
digitalWrite(ledPin, state);
return 1;
}
int led2Control(String command)
{
int state = LOW;
// find out the state of the led
if(command == "HIGH" || command == "HI"){
state = HIGH;
}else if(command == "LOW" || command == "LO"){
state = LOW;
}else{
return -1;
}
// write to the appropriate pin
digitalWrite(ledPin2, state);
return 1;
}
int RGBControl(String command){
//0 is HI, 255 is LOW b/c CATHODE RGB LED
String colors[3];
colors[0]="";
colors[1]="";
colors[2]="";
int index = 0;
for( int i = 0; i < command.length(); i++ )
{
if( index < 3 ){
char c = command.charAt(i);
colors[index] += c;
if( c == ',') index++;
}
}
// get the red component...
redValue = colors[0].toInt();
// now green
greenValue = colors[1].toInt();
// now blue
blueValue = colors[2].toInt();
// write the mixed color
analogWrite( redPin, redValue);
analogWrite( greenPin, greenValue);
analogWrite( bluePin, blueValue);
return 1;
}
void setup() {
// Configure the pins to be outputs
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
//Uses CATHODE RGB LED, 255 is OFF, 0 is ON
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
//Start up as OFF
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
// Initialize both the LEDs to be OFF
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
//Register our Particle function here
Particle.function("led", ledControl);
Particle.function("led2", led2Control);
//RGB
Particle.function("RGB LED", RGBControl);
}
void loop() {
//loop
//particle analog read range is 0-4095
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .