Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- StateLamp by Fabio Carnino
- code inspired by arkadian.eu
- http://www.arkadian.eu/pages/219/arduino-controlled-ikea-lamp
- Grono lamp changing behaviour by changing its face
- Hackea Workshop held by Tinker in Torino for Torino Design Week
- http://tinker.it/
- http://www.torinodesignweek.org/
- */
- //tilts
- int buttonPin1 = 3;
- int buttonPin2 = 2;
- int buttonPin3 = 4;
- //value to store tilts
- int buttonPin1Val = 0;
- int buttonPin2Val = 0;
- int buttonPin3Val = 0;
- int fadeval = 0;
- int fadedIn = 0;
- int fadedOut = 0;
- int ledPin4 = 11; // (Yellow)
- int ledRGB[] = {9,10,11}; // rgb led
- int states[13];
- long debounces[13];
- long debounceDelay = 50;
- unsigned long lastAction = millis();
- //Defined Colors (different RGB (red, green, blue) values for colors
- const byte RED[] = {255, 0, 0};
- const byte ORANGE[] = {83, 4, 0};
- const byte YELLOW[] = {255, 255, 0};
- const byte GREEN[] = {0, 255, 0};
- const byte BLUE[] = {0, 0, 255};
- const byte WHITE[] = {255, 255, 255};
- const byte BLACK[] = {0, 0, 0};
- const byte PINK[] = {158, 4, 79};
- const byte B2[] = {0, 128, 255};
- const byte G2[] = {128, 255, 0};
- const byte R2[] = {255, 128, 0};
- const byte B3[] = {128, 0, 255};
- const byte G3[] = {0, 255, 128};
- const byte R3[] = {229, 215, 180};
- byte coloreAttuale[2];
- void setup() {
- pinMode(buttonPin1, INPUT);
- pinMode(buttonPin2, INPUT);
- pinMode(buttonPin3, INPUT);
- for(int i = 0; i < 3; i++)
- {
- pinMode(ledRGB[i],OUTPUT);
- }
- pinMode(ledPin4, OUTPUT);
- Serial.begin(9600);
- digitalWrite(buttonPin1, LOW); // pull-down
- digitalWrite(buttonPin2, LOW);
- digitalWrite(buttonPin3, LOW);
- debounces[buttonPin1] = 0;
- debounces[buttonPin2] = 0;
- debounces[buttonPin3] = 0;
- states[buttonPin1] = 0;
- states[buttonPin2] = 0;
- states[buttonPin3] = 0;
- }
- void loop() {
- readState(buttonPin1);
- readState(buttonPin2);
- readState(buttonPin3);
- buttonPin1Val = states[buttonPin1];
- buttonPin2Val = states[buttonPin2];
- buttonPin3Val = states[buttonPin3];
- /* uncomment for debugging
- Serial.print("buttonPin1Val = ");
- Serial.print(buttonPin1Val);
- Serial.print("___");
- Serial.print("buttonPin2Val = ");
- Serial.print(buttonPin2Val);
- Serial.print("___");
- Serial.print("buttonPin3Val = ");
- Serial.println(buttonPin3Val);
- */
- // quattro possibilità / four states
- if (buttonPin2Val == 0 && buttonPin3Val ==0) { // extra
- fadeToColor(ledRGB, coloreAttuale, GREEN,10);
- color(GREEN);
- }
- if (buttonPin2Val == 1 && buttonPin3Val ==0) { // fade
- fadeToColor(ledRGB, coloreAttuale, WHITE,10);
- }
- if (buttonPin2Val == 0 && buttonPin3Val ==1) { // off
- // digitalWrite(ledRGB,LOW);
- digitalWrite(9, LOW);
- digitalWrite(10, LOW);
- digitalWrite(11, LOW);
- }
- if (buttonPin2Val == 1 && buttonPin3Val ==1) { // fade
- fadeToColor(ledRGB, coloreAttuale, PINK, 1);
- }
- }
- /* else if (buttonPin2Val == 1) {
- if (fadedOut == 0) {
- fadeout();
- }
- }
- */
- //}
- void fadeToColor(int* led, byte* startColor, const byte* endColor, int fadeSpeed){
- int changeRed = endColor[0] - startColor[0]; //the difference in the two colors for the red channel
- int changeGreen = endColor[1] - startColor[1]; //the difference in the two colors for the green channel
- int changeBlue = endColor[2] - startColor[2]; //the difference in the two colors for the blue channel
- int steps = max(abs(changeRed),max(abs(changeGreen), abs(changeBlue))); //make the number of change steps the maximum channel change
- for(int i = 0 ; i < steps; i++){ //iterate for the channel with the maximum change
- byte newRed = startColor[0] + (i * changeRed / steps); //the newRed intensity dependant on the start intensity and the change determined above
- byte newGreen = startColor[1] + (i * changeGreen / steps); //the newGreen intensity
- byte newBlue = startColor[2] + (i * changeBlue / steps); //the newBlue intensity
- byte newColor[] = {newRed, newGreen, newBlue}; //Define an RGB color array for the new color
- color(newColor); //Set the LED to the calculated value
- //readButton();
- //if(myOption==0){delay(fadeSpeed);}
- //delay(fadeSpeed);
- }
- color(endColor); //The LED should be at the endColor but set to endColor to avoid rounding errors
- }
- void readState(int tiltPin) {
- int reading = digitalRead(tiltPin);
- //Serial.println(reading);
- //Serial.println(reading);
- if (reading != states[tiltPin]) {
- if ((millis() - debounces[tiltPin]) > debounceDelay) {
- // whatever the reading is at, it's been there for longer
- // than the debounce delay, so take it as the actual current state:
- states[tiltPin] = reading;
- debounces[tiltPin] = millis(); // reset the debouncing timer
- }
- }
- }
- void color(const byte rgb[]){
- for(int i = 0; i < 3; i++)
- {
- analogWrite(ledRGB[i],rgb[i]);
- coloreAttuale[i] = rgb[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement