int redPin = A0;
int greenPin = D0;
int bluePin = D1;
// Define a pin that we'll place the pot on
int potPin = A1;
//send to internet
int buttonPin = D2;
// Create a variable to hold the pot reading
int potReading = 0;
// Create a variable to store color state.
int state = 0;
void setup(){
pinMode( redPin, OUTPUT);
pinMode( greenPin, OUTPUT);
pinMode( bluePin, OUTPUT);
pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
Spark.subscribe( "db2015/conchain123/color1" , receiveColor );
}
void loop() {
// when the button is not pushed we'll get a HIGH
//first set color off (255,255,255)
potReading = analogRead(potPin);
//map the color state
state = map(potReading, 0, 4095, 0, 255);
if( state == 0 )
{
// turn the LED off
digitalWrite( redPin, HIGH );
digitalWrite( greenPin, HIGH );
digitalWrite( bluePin, HIGH );
}
else if(state > 0 && state < 85) //red
{
digitalWrite( redPin, LOW );
digitalWrite( greenPin, HIGH );
digitalWrite( bluePin, HIGH );
}
else if(state > 85 && state < 170) //green
{
digitalWrite( redPin, HIGH );
digitalWrite( greenPin, LOW );
digitalWrite( bluePin, HIGH );
}
else if(state > 170 && state < 255) //blue
{
digitalWrite( redPin, HIGH );
digitalWrite( greenPin, HIGH );
digitalWrite( bluePin, LOW );
}
buttonpublish(state);
delay (100);
}
//press button to publish
void buttonpublish(int value)
{
int buttonState = digitalRead( buttonPin );
if( buttonState == LOW )
{
delay(2000);
if( buttonState == LOW)
{
if(value > 0 && value < 85)
{
publishred();
blinkredLED();
}
else if(value > 85 && value < 170)
{
publishgreen();
blinkgreenLED();
}
else if(value > 170 && value < 255)
{
publishblue();
blinkblueLED();
}
}
}
delay(100);
}
//publish red
void publishred()
{
Spark.publish( "db2015/conchain123/color2", "RED" );
}
//publish green
void publishgreen()
{
Spark.publish( "db2015/conchain123/color2", "GREEN" );
//Spark.publish( "db2015/conchain123/green" );
}
//publish blue
void publishblue()
{
Spark.publish( "db2015/conchain123/color2", "BLUE" );
//Spark.publish( "db2015/conchain123/blue" );
}
void blinkredLED()
{
digitalWrite( redPin, HIGH );
digitalWrite( greenPin, HIGH );
digitalWrite( bluePin, HIGH );
for( int i = 0 ; i < 3 ; i++ )
{
digitalWrite( redPin, LOW );
delay( 500 );
digitalWrite( redPin, HIGH );
delay( 500 );
}
}
void blinkgreenLED()
{
digitalWrite( redPin, HIGH );
digitalWrite( greenPin, HIGH );
digitalWrite( bluePin, HIGH );
for( int i = 0 ; i < 3 ; i++ )
{
digitalWrite( greenPin, LOW );
delay( 500 );
digitalWrite( greenPin, HIGH );
delay( 500 );
}
}
void blinkblueLED()
{
digitalWrite( redPin, HIGH );
digitalWrite( greenPin, HIGH );
digitalWrite( bluePin, HIGH );
for( int i = 0 ; i < 3 ; i++ )
{
digitalWrite( bluePin, LOW );
delay( 500 );
digitalWrite( bluePin, HIGH );
delay( 500 );
}
}
void receiveColor( const char *event, const char *data)
{
if( strcmp( data, "RED" ) == 0 ){
receiveRED( event, data);
}
else if( strcmp( data, "GREEN" ) == 0 ){
receiveGREEN( event, data);
}
else if( strcmp( data, "BLUE" ) == 0 ){
receiveBLUE( event, data);
}
else
{
blinkblueLED();
}
}
void receiveRED(const char *event, const char *data)
{
digitalWrite( redPin, HIGH );
digitalWrite( greenPin, HIGH );
digitalWrite( bluePin, HIGH );
blinkredLED();
digitalWrite( redPin, LOW );
delay(3000);
}
void receiveGREEN(const char *event, const char *data)
{
digitalWrite( redPin, HIGH );
digitalWrite( greenPin, HIGH );
digitalWrite( bluePin, HIGH );
blinkgreenLED();
digitalWrite( greenPin, LOW );
delay(3000);
}
void receiveBLUE(const char *event, const char *data)
{
digitalWrite( redPin, HIGH );
digitalWrite( greenPin, HIGH );
digitalWrite( bluePin, HIGH );
blinkblueLED();
digitalWrite( bluePin, LOW );
delay(3000);
}
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. .