|
Main Links:
Final Projects
|
Main /
GroupStoneglowGroup Members
Using the combination of an infrared emitter and a 38kHz IR detector module. This will allow for the blocking of ambient noise and will even allow us to "tag" the emitted light with data.
Serial.begin(2400); pinMode(redPin,OUTPUT); pinMode(greenPin,OUTPUT); pinMode(bluePin,OUTPUT); } void loop() { time = millis();
if(!startup)
startupStone();
checkBreathing();
if(breathing)
{
analogWrite(redPin,breatheValue);
analogWrite(greenPin,breatheValue);
analogWrite(bluePin,breatheValue);
}
else
{
analogWrite(redPin,redValue);
analogWrite(greenPin,greenValue);
analogWrite(bluePin,blueValue);
}
checkDetectors();
emitData();
} void startupStone() { byte inByte = 0;
byte lastByte = 0;
unsigned long lastOn = 0;
unsigned long lastOff = 0;
while(!startup)
{
time = millis();
if(time - lastOff > 100)
{
analogWrite(defaultColor,0);
lastOff = millis();
}
else if(time - lastOn > 2000)
{
analogWrite(defaultColor,60);
lastOn = millis();
}
emitData();
if(Serial.available() > 0)
{
inByte = Serial.read();
if((inByte == 'R' || inByte == 'G' || inByte == 'B') && inByte != stoneCode)
{
lastByte = inByte;
while(Serial.available() <= 0)
{
time = millis();
if(time - lastOff > 100)
{
analogWrite(defaultColor,0);
lastOff = millis();
}
else if(time - lastOn > 2000)
{
analogWrite(defaultColor,60);
lastOn = millis();
}
}
inByte = Serial.read();
if(lastByte == inByte)
{
analogWrite(defaultColor,255);
startup = true;
}
}
}
}
} void checkBreathing() { delay(2); //this is a weird fix... without it the tri-color will blink
if(redValue >= 230 && greenValue >= 230 && blueValue >= 230)
{
breathing = true;
if(breatheValue + breatheDirection > 255)
breatheDirection *= -1;
else if(breatheValue + breatheDirection < 0)
{
breatheValue = 0;
breathePause = time = millis();
while(time - breathePause <= 500)
time = millis();
breatheDirection *= -1;
}
time = millis();
if(time - breatheChange > 12)
{
breatheValue += breatheDirection;
breatheChange = millis();
}
}
else
breathing = false;
} void checkDetectors() { byte inByte = 0;
if(time - last_detection_time > 10)
{
if(Serial.available() > 0)
{
inByte = Serial.read();
if(inByte == 'R' && stoneCode != 'R')
{
while(Serial.available() <= 0){}
inByte = Serial.read();
if(inByte == 'R')
{
if(redValue + redDirection > 255 || redValue + redDirection < 0)
redDirection *= -1;
redValue+=redDirection;
}
}
else if(inByte == 'G' && stoneCode != 'G')
{
while(Serial.available() <= 0){}
inByte = Serial.read();
if(inByte == 'G')
{
if(greenValue + greenDirection > 255 || greenValue + greenDirection < 0)
greenDirection *= -1;
greenValue+=greenDirection;
}
}
else if(inByte == 'B' && stoneCode != 'B')
{
while(Serial.available() <= 0){}
inByte = Serial.read();
if(inByte == 'B')
{
if(blueValue + blueDirection > 255 || blueValue + blueDirection < 0)
blueDirection *= -1;
blueValue+=blueDirection;
}
}
}
last_detection_time = millis();
}
} void emitData() { if(time - last_emission_time > 250)
{
Serial.print(stoneCode,BYTE);
Serial.print(stoneCode,BYTE);
last_emission_time = millis();
}
} |