#include <math.h>
#define TRIGGER1 4
#define TRIGGER2 7
#define TRIGGER3 8
#define RED 9
#define GREEN 6
#define BLUE 5
#define FAN 11
unsigned long long i_breathe = 0;
int breathe_delay = 15; // delay between loops
unsigned long breathe_time = millis();
char inbyte[64]; String line = "";
byte R{0}, G{0} , B{0} , ana_effect{0};
//This function splits a string tstr into array of strings str_arr with spaces
void split(String tstr , String str_ar[] , int len , char sp = ' ') {
for (int i{0} , j{0}; i < tstr.length(); i++) {
if (tstr[i] == sp) {
j++;
i++;
}
if (j >= len) return;
str_ar[j] += tstr[i];
}
}
void ana_continous(byte red , byte green , byte blue)
{
analogWrite(RED, red); analogWrite(GREEN, green); analogWrite(BLUE, blue);
}
void ana_breath()
{
if ( (breathe_time + breathe_delay) < millis() ) {
breathe_time = millis();
float val = (exp(sin(i_breathe / 2000.0 * PI * 10)) - 0.36787944) * 108.0;
// this is the math function recreating the effect
analogWrite(RED, (val / 253.27) * R);
analogWrite(GREEN, (val / 253.27) * G);
analogWrite(BLUE, (val / 253.27) * B);
i_breathe = i_breathe + 1;
}
}
void ana_cycle()
{
for (int j = 0; j < 7; j++ ) {
for (int k = 0; k < 256; k++) {
switch (j) {
case 0:
ana_continous (k , k , 0);
break;
case 1:
ana_continous (0 , k , k);
break;
case 2:
ana_continous (k , 0 , k);
break;
case 3:
ana_continous (k , 0 , 0);
break;
case 4:
ana_continous (0 , k , 0);
break;
case 5:
ana_continous (0 , 0 , k);
break;
case 6:
ana_continous (k , k , k);
break;
}
delay(3);
}
}
/*for (int j = 0; j < 7; j++ ) {
for (int k = 0; k < 256; k++) {
switch (j) {
case 0:
ana_continous (k , k , k);
break;
case 1:
ana_continous (0 , 0 , k);
break;
case 2:
ana_continous (0 , k , 0);
break;
case 3:
ana_continous (k , 0 , 0);
break;
case 4:
ana_continous (k , 0 , k);
break;
case 5:
ana_continous (0 , k , k);
break;
case 6:
ana_continous (k , k , 0);
break;
}
delay(3);
}
}*/
}
void ana_flicker()
{
ana_continous(R , G , B);
delay(50);
ana_continous(0 , 0 , 0);
delay(50);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
char go = 0; int can_cont = 0;
while (go != '%')
{ //Handshake with the software happens here
Serial.println("Detection);
delay(100);
if (Serial.available())
go = Serial.read();
}
Serial.println("Detection Started");
pinMode(TRIGGER1 , OUTPUT); pinMode(TRIGGER2 , OUTPUT); pinMode(TRIGGER3 , OUTPUT);
pinMode(RED , OUTPUT); pinMode(GREEN , OUTPUT); pinMode(BLUE , OUTPUT);
pinMode(FAN , OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int i = 0;
if (Serial.available() > 0) {
delay(10);
inbyte[i] = Serial.read();
while (Serial.available() && inbyte[i] != '%' && i < 62) {
inbyte[i + 1] = Serial.read();
i += 1;
}
// 1) if (i = 1 && inbyte[i] == '%') Serial.println("RainPow");
if (inbyte[i] == '%') i -= 1;
inbyte[i + 1] = '\0';
line = inbyte;
}
if (i > 0) {
//Process line
Serial.print("From Ard: ");
Serial.println(line);
if (line[0] == 'T' && line.length() >= 3)
{
//Trigger Clicked
int trig = ((char)line[1]) - '0';
int stat = ((char)line[2]) - '0';
if (trig == 1) {
digitalWrite(TRIGGER1 , stat);
delay(200);
digitalWrite(TRIGGER1 , LOW);
} else if (trig == 2) {
digitalWrite(TRIGGER2 , stat);
delay(200);
digitalWrite(TRIGGER2 , LOW);
} else if (trig == 3) {
digitalWrite(TRIGGER3 , stat);
delay(200);
digitalWrite(TRIGGER3 , LOW);
}
} else if (line[0] == 'F' && line.length() >= 2)
{
int spd = ((char)line[1]) - '0';
float f_spd = ((spd * 1.0) / 9.0) * 255;
analogWrite(FAN , f_spd);
} else if (line[0] == 'A')
{
String in[5];
split(line, in , 5);
ana_effect = in[1].toInt();
R = in[2].toInt(); //analogWrite(RED , R);
G = in[3].toInt(); //analogWrite(GREEN , G);
B = in[4].toInt(); //analogWrite(BLUE , B);
}
i = 0;
}
//Processing loop is here#
if (ana_effect == 0)
ana_continous(R , G , B);
else if (ana_effect == 1)
ana_breath();
else if (ana_effect == 2)
ana_cycle();
else if (ana_effect == 3)
ana_flicker();
}
For the Schematic, its pretty easy. Its just an RGB-LED 5mm diameter directly connected to the pins of the Arduinos. Resistor for every color Pin is 180 Ohms and they have common Cathode.