/****************RGB LED**************/
int redPin = A1;
int greenPin = D1;
int bluePin = D0;
int colors[3] = {255,255,255};
/************light sensor**************/
int photoCellPin = A0;
int ledPin = D7;
int photoCellReading = 0;
/************force sensor**************/
int forcePin = A6;
int forceCellReading = 0;
int redLedLinePin = A5;
int debugValue = 0;
void setup()
{
/************setting LED************/
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(redLedLinePin, OUTPUT);
digitalWriteColor();
/********setting photosensor********/
pinMode(ledPin, OUTPUT);
/**********report to cloud**********/
//Spark.function("led",ledControl);
Spark.variable("light", &photoCellReading, INT);
Spark.variable("force", &forceCellReading, INT);
Spark.variable("red", &colors[0], INT);
Spark.variable("green", &colors[1], INT);
Spark.variable("blue", &colors[2], INT);
Spark.variable("debug", &debugValue, INT);
// RGB.color(redV, greenV, blueV);
}
void loop()
{
/****reading the value of light sensor****/
photoCellReading = analogRead(photoCellPin);
forceCellReading = analogRead(forcePin);
/****random the color while the light changing****/
if(photoCellReading > 200 && photoCellReading < 1000)
ledSingleColorRandom();
if(photoCellReading <= 200)
ledAllColorRandom();
else
{
digitalWriteColor();
debugValue = 0;
}
/**** the force ****/
if(forceCellReading > 1000)
analogWrite(redLedLinePin, random(0, 255));
if(forceCellReading <= 1000)
analogWrite(redLedLinePin, 0);
}
/********when the light gets dark (200 - 1000)********/
/********the RGB LED blink randomly********/
/********BUT COLOR BY COLOR********/
void ledSingleColorRandom()
{
int randomValue = random(0, 255);
int whichColor = random(1,100)%3;
//colors[0] = randomValue;
colors[whichColor] = randomValue;
debugValue = 1;
digitalWriteColor();
delay(1000);
/********set all color's value back********/
/****!!don't forget it!!!! Setting back here****/
for(int i = 0; i <3; i++)
{
colors[i] = 255;
}
}
/********when the light gets dark (0 - 200)********/
/********the RGB LED blink randomly********/
/********ALL COLOR********/
void ledAllColorRandom()
{
for(int i = 0; i <3; i++)
{
colors[i] = random( 0, 255);
}
debugValue = 2;
digitalWriteColor();
delay(800);
/********set all color's value back********/
/****!!don't forget it!!!! Setting back here****/
for(int i = 0; i <3; i++)
{
colors[i] = 255;
}
}
/********write the value to each color********/
void digitalWriteColor()
{
analogWrite(redPin,colors[0]);
analogWrite(greenPin,colors[1]);
analogWrite(bluePin,colors[2]);
}
Click to Expand