Skip to content

Instantly share code, notes, and snippets.

@cydcipolla
Created August 13, 2018 10:54
Embed
What would you like to do?
Programming electronic flowers on Arduino.
//IR LED must be attached to pin 3//
//color transmit, 420365f6hex or 1107518966=red, b424ad80 or 3022302592=blue, 5be5ec99 or 1541794969=green
#include <IRremote.h>
//CUSTOMIZE YOUR FLOWER!!//
//Change the values below to change your flower colors and behaviors.
//For the red, blue, and green, find the RGB value of the color you want to use and input the numbers below.
int red = random(256); // red RGB value
int green = random(256);// green RGB value
int blue = random(256);// blue RGB value
int sensitive = true; //Set this to "true" if you want your flower to react a lot to its neighbors, to "false" if you want it to be more robust.
int talkative = true; // Set this to "true" if you want your flower to send information frequently, to "false" if you want it to listen longer.
//End customization.
int RECV_PIN = 11;//IR Receiver Pin
int transmitPin = 2;//transmission indicator pin, for testing
int receiveBluePin = 6;//blue receipt indicator pin
int receiveGreenPin = 5;//green receipt indicator pin
int receiveRedPin = 4;//red receipt indicator pin
int redPin = 12; //RGB lED pins
int greenPin = 10;
int bluePin = 9;
int redCount = 1; // deciding how long to broadcast over IR
int blueCount = 1;
int greenCount = 1;
int redValue = 255 - red;// we use common anode LEDs so the values need to be inverted
int blueValue = 255 - blue;
int greenValue = 255 - green;
int interval = 500;
int shiftAmount = 25; //This controls how much the colors shift. Low numbers = slow, high = fast.
IRsend irsend;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
pinMode(transmitPin, OUTPUT);
pinMode(receiveBluePin, OUTPUT);
pinMode(receiveGreenPin, OUTPUT);
pinMode(receiveRedPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
if (talkative == true){
interval = 200;
}
if (sensitive == true){
shiftAmount = 50;
}
Serial.println("Enabling IRin");
irrecv.enableIRIn(); // Start the receiver
Serial.println("Enabled IRin");
}
void loop() {
colorCheck();\
transmitRed(redCount);
delay(interval);
transmitGreen(greenCount);
delay(interval);
transmitBlue(blueCount);
delay(interval);
if (irrecv.decode(&results)) {
Serial.println(results.value);
switch (results.value) {
case 1107518966:
redShift();
setColor();
break;
case 1541794969:
greenShift();
setColor();
break;
case 3022302592:
blueShift();
setColor();
break;
default:
Serial.println("Unreadable Code");
if (sensitive == true){
redValue = redValue - 1;
blueValue = blueValue - 1;
greenValue = greenValue - 1;
setColor();
}
}
irrecv.enableIRIn();
reportData();
}
}
void setColor(){ //push the RGB value to the LED
analogWrite(redPin, constrain(redValue, 0, 255));
analogWrite(greenPin, constrain(greenValue, 0, 255));
analogWrite(bluePin, constrain(blueValue, 0, 255));
}
void reportData(){//print to serial for debugging
Serial.print("red = ");
Serial.print(redValue);
Serial.print(" count = ");
Serial.print(redCount);
Serial.print(" blue = ");
Serial.print(blueValue);
Serial.print(" count = ");
Serial.print(blueCount);
Serial.print(" green = ");
Serial.print(greenValue);
Serial.print(" count = ");
Serial.print(greenCount);
Serial.print("\n");
}
void transmitRed(int redTime){//changes transmit to red for a period of time, then back to receive
digitalWrite(transmitPin, HIGH);//lights up transmit pin for debugging
for (int j = 0; j <redTime; j++){
for (int i = 0; i < 3; i++) {
irsend.sendNEC(0x420365f6, 32);
delay(40);
}
delay(100);
}
digitalWrite(transmitPin, LOW);//turns off debugging pin
irrecv.enableIRIn();
}
void transmitBlue(int blueTime){//changes transmit to blue for a period of time, then back to receive
digitalWrite(transmitPin, HIGH);
for (int j = 0; j <blueTime; j++){
for (int i = 0; i < 3; i++) {
irsend.sendNEC(0xb424ad80, 32);
delay(40);
}
delay(100);
}
digitalWrite(transmitPin, LOW);
irrecv.enableIRIn();
}
void transmitGreen(int greenTime){//changes transmit to green for a period of time, then back to receive
digitalWrite(transmitPin, HIGH);
for (int j = 0; j <greenTime; j++){
for (int i = 0; i < 3; i++) {
irsend.sendNEC(0x5be5ec99, 32);
delay(40);
}
delay(100);
}
digitalWrite(transmitPin, LOW);
irrecv.enableIRIn();
}
void redShift(){ //makes receipt LED red, shifts main RGB LED towards red
digitalWrite(receiveRedPin, LOW);
digitalWrite(receiveBluePin, HIGH);
digitalWrite(receiveGreenPin, HIGH);
redCount = redCount + 1;
if (redValue >= 0){
redValue = redValue - shiftAmount;
}
if (blueValue < 255){
blueValue = blueValue + shiftAmount;
}
if (greenValue < 255){
greenValue = greenValue + shiftAmount;
}
}
void blueShift(){//makes receipt LED blue, shifts main RGB LED towards blue
digitalWrite(receiveBluePin, LOW);
digitalWrite(receiveRedPin, HIGH);
digitalWrite(receiveGreenPin, HIGH);
blueCount = blueCount + 1;
if (blueValue >= 0){
blueValue = blueValue - shiftAmount;
}
if (redValue < 255){
redValue = redValue + shiftAmount;
}
if (greenValue < 255){
greenValue = greenValue + shiftAmount;
}
}
void greenShift(){//makes receipt LED green, shifts main RBG LED towards green
digitalWrite(receiveGreenPin, LOW);
digitalWrite(receiveRedPin, HIGH);
digitalWrite(receiveBluePin, HIGH);
greenCount = greenCount + 1;
if (greenValue >= 0){
greenValue = greenValue - shiftAmount;
}
if (blueValue < 255){
blueValue = blueValue + shiftAmount;
}
if (redValue < 255){
redValue = redValue + shiftAmount;
}
}
void turnOff(){//turns off main RGB LED
redValue = 255;
blueValue = 255;
greenValue = 255;
setColor();
delay(150);
}
void makeRed(){//makes main RGB pure red
redValue = 0;
blueValue = 255;
greenValue = 255;
setColor();
delay(150);
}
void makeBlue(){//makes main RGB LED pure blue
redValue = 255;
blueValue = 0;
greenValue = 255;
setColor();
delay(150);
}
void makeGreen(){//makes main RGB LED pure green
redValue = 255;
blueValue = 255;
greenValue = 0;
setColor();
delay(150);
}
void makeWhite(){//main main RGB LED white
redValue = 100;
blueValue = 100;
greenValue = 100;
setColor();
delay(150);
}
void colorCheck(){
if (redCount >= 10){
for (int k = 0; k < 5; k++){
turnOff();
makeRed();
}
runRainbow();
runRainbow();
redCount = 0;
}
else if (greenCount >= 10){
for (int k = 0; k < 5; k++){
turnOff();
makeGreen();
}
runRainbow();
runRainbow();
greenCount = 0;
}
else if (blueCount >= 10){
for (int k = 0; k < 5; k++){
turnOff();
makeBlue();
}
runRainbow();
runRainbow();
blueCount = 0;
}
}
void runRainbow() {
unsigned int rgbColour[3];
rgbColour[0] = 255; // Start off with red.
rgbColour[1] = 0;
rgbColour[2] = 0;
for (int decColour = 0; decColour < 3; decColour += 1) { // Choose the colours to increment and decrement.
int incColour = decColour == 2 ? 0 : decColour + 1;
for(int i = 0; i < 255; i += 1) { // cross-fade the two colours.
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
delay(10);
}
}
}
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {//modification to make runRainbow work with common anode
analogWrite(redPin, 255 - red);
analogWrite(greenPin, 255 - green);
analogWrite(bluePin, 255 - blue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment