Hello
I'm starting to push myself abit harder with the arduino and have hit a point were I could use a nudge in the right direction please.
The project I am making is what I have called "An Emotional Companion Cube". Basically its an translucent box with some RGBs and arduino and a ultrasonic sensor. The idea is, at a distance it glows blue and green and shifts gently between them, if you get within 50cm of it, it changes to concerned yellows and oranges, and shifts faster. Then if you get within 25cm of it its gets angry and flashes red and purple and orange.
I have tested the principle with the RGBs and had them change colour as I get closer, now I have advanced that into the 3 moods, with it fading using the PWM. Here is the code: (theres a few redundant integers for expanding later)
const int Trigger = 8;
const int Receiver = 7;
const int ROne = 11;
const int GOne = 10;
const int BOne = 9;
const int RTwo = 6;
const int GTwo = 5;
const int BTwo = 3;
const int ContentDelay = 30;
const int ContentConcernDelay = 20;
const int ConcernDelay = 10;
const int ConcernWarningDelay = 5;
const int WarningDelay = 1;
const int WarningContentDelay = 20;
int RBrightness = 0;
int GBrightness = 0;
int BBrightness = 0;
int Responce = 0;
int Distance = 0;
void setup() {
Serial.begin(9600);
pinMode (ROne, OUTPUT);
pinMode (GOne, OUTPUT);
pinMode (BOne, OUTPUT);
pinMode (RTwo, OUTPUT);
pinMode (GTwo, OUTPUT);
pinMode (BTwo, OUTPUT);
pinMode (Trigger, OUTPUT);
pinMode (Receiver, INPUT);
}
void loop() {
digitalWrite(Trigger, LOW);
delayMicroseconds(5);
digitalWrite(Trigger, HIGH);
delayMicroseconds(5);
digitalWrite(Trigger, LOW);
Responce = pulseIn(Receiver,HIGH);
Distance = (Responce/58);
Serial.print(Distance);
Serial.print("cm");
if (Distance >75){
//Content
// Blue to Turquoise
RBrightness = 255;
analogWrite(ROne, RBrightness);
BBrightness = 0;
analogWrite(BOne, BBrightness);
for (GBrightness = 255; GBrightness > 0; GBrightness--) {
analogWrite(GOne, GBrightness);
delay(ContentDelay);
}
//Turquoise to Green
for (BBrightness = 0 ; BBrightness < 255; BBrightness++) {
analogWrite(BOne, BBrightness);
delay(ContentDelay);
}
// Green to Turquoise
for (BBrightness = 255 ; BBrightness > 0; BBrightness--) {
analogWrite(BOne, BBrightness);
delay(ContentDelay);
}
//Turquoise to Blue
for (GBrightness = 0; GBrightness < 255; GBrightness++) {
analogWrite(GOne, GBrightness);
delay(ContentDelay);
}
}
else if(Distance > 25) {
//Concern
// Pale Yellow to Strong Yellow
GBrightness = 0;
analogWrite(GOne, GBrightness);
BBrightness = 255;
analogWrite(BOne, BBrightness);
for (RBrightness = 55; RBrightness > 0; RBrightness--) {
analogWrite(ROne, RBrightness);
delay(ConcernDelay);
}
//Strong Yellow to Amber
for (GBrightness = 0 ; GBrightness < 123; GBrightness++) {
analogWrite(GOne, GBrightness);
delay(ConcernDelay);
}
//Amber to Strong Yellow
for (GBrightness = 123 ; GBrightness > 0; GBrightness--) {
analogWrite(GOne, GBrightness);
delay(ConcernDelay);
}
//Strong Yellow to Pale Yellow
for (RBrightness = 0; RBrightness < 55; RBrightness++) {
analogWrite(ROne, RBrightness);
delay(ConcernDelay);
}
delay(500);
}
else if (Distance <25) {
//Warning
// Strong Orange to Red
RBrightness = 0;
analogWrite(GOne, GBrightness);
BBrightness = 255;
analogWrite(BOne, BBrightness);
for (GBrightness = 197; GBrightness < 255; GBrightness++) {
analogWrite(GOne, GBrightness);
delay(WarningDelay);
}
//Red to Warm Orange
for (GBrightness = 255 ; GBrightness > 142; GBrightness--) {
analogWrite(GOne, GBrightness);
delay(WarningDelay);
}
//Warm Orange to Fluorescent Pink
for (GBrightness = 142 ; GBrightness < 255; GBrightness++) {
analogWrite(GOne, GBrightness);
delay(WarningDelay);
}
for (BBrightness = 255 ; BBrightness > 146; BBrightness--) {
analogWrite(BOne, BBrightness);
delay(WarningDelay);
}
//Fluorescent Pink to Strong Orange
for (GBrightness = 255 ; GBrightness > 197; GBrightness--) {
analogWrite(GOne, GBrightness);
delay(WarningDelay);
}
for (BBrightness = 146 ; BBrightness < 255; BBrightness++) {
analogWrite(BOne, BBrightness);
delay(WarningDelay);
}
}
}
My next step is to have it fade neatly between 2 moods, which I think I will be ok with.
My problem is I'm getting a delay whilst the code finishes its cycle before going back to check the Distance.
So my questions are:
- Whats the best way to read the ultrasonic sensor in real time, so its not waiting for the fade cycle to finish?
- Also, I would like the LEDs to go up and down in brightness as they switch between colours.Can anyone think of a cleaner/easier way to dim the LEDs whilst it is fading? Obviously I can drop the R/G/BBrightness values, but that could end up a very long code. Is there anyway to say, every .5 seconds divide R/G/BBrightness value by 10 for a couple of hundred milliseconds, then knock it back up again?
3: Finally, any tips on cleaning up the above/better ways to do this?
Many thanks!
David