Hi,
i want to dim a led strip. How can i limit the dim so that when it goes to 0, It stop, not returning to 255. and when it goes to 255, not returning to 0.
Thanks in advance
Hi,
i want to dim a led strip. How can i limit the dim so that when it goes to 0, It stop, not returning to 255. and when it goes to 255, not returning to 0.
Thanks in advance
The dim function is activate with a web server.
Everything you need is in what you said, or perhaps posting your code will help ...
here a partial of the code where i need help, i could post all the code but it very very long
case 'j':
//+ brightness
r = (brightness - r);
g = (brightness - g);
b = (brightness - b);
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
Serial.println("+ brightness");
break;
case 'k':
//- brightness
r = (brightness + r);
g = (brightness + g);
b = (brightness + b);
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
Serial.println("- brightness");
break;
here all the code:
//Michael sketche
//Arduino server for 8 relay, stove and RGB led strip
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//address will look like http://192.168.1.102:84 for the ledserver
//address will look like http://192.168.1.102:85 for the relayserver
//for use with W5100 based ethernet shields
#include <SPI.h>
#include <Ethernet.h>
boolean reading = false;
// Debug mode
#define DEBUG true
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 2, 16 }; // ip in lan
EthernetServer Ledserver(84); //Led server port
EthernetServer Relayserver(85); //Relay server port
//For arduino input
#define Doorbelloff "76" //Scene for pin 2 LOW (door bell On)
#define Doorbellon "83" //Scene for pin 2 high (door bell OFF)
#define Phoneon "81" //Scene for pin 3 LOW (telephoe ring On)
#define Phoneoff "82" //Scene for pin 3 high (telephoe ring OFF)
#define Doorbell 2
#define Phone 3
boolean DoorbellState = false;
boolean PhoneState = false;
boolean Phonedelay = false;
//Variable for debonce phone ring
unsigned long pressed;
int pwrtime = 600;
unsigned long time;
unsigned long unpressed;
char serverName[] = "192.168.2.19"; //Vera ip address
EthernetClient client;
unsigned long ledstatut;
int r;
int g;
int b;
int brightness = 30;
String readString;
//RGB led pin
const int redPin = 4;
const int greenPin = 5;
const int bluePin = 6;
//////////////////////
void setup(){
//start Ethernet
Ethernet.begin(mac, ip);
Ledserver.begin();
//Led pin
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
r = constrain(r, 0, 255);
g = constrain(g, 0, 255);
b = constrain(b, 0, 255);
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
//input
pinMode(Doorbell, INPUT);
pinMode(Phone, INPUT);
//relay pin
pinMode(22, OUTPUT); //output relay pin
pinMode(23, OUTPUT); //output relay pin
pinMode(24, OUTPUT); //output relay pin
pinMode(25, OUTPUT); //output relay pin
pinMode(26, OUTPUT); //output relay pin
pinMode(27, OUTPUT); //output relay pin
pinMode(28, OUTPUT); //output relay pin
pinMode(29, OUTPUT); //output relay pin
pinMode(29, OUTPUT); //output relay pin
Serial.begin(9600);
Serial.println("Arduino starting"); // so I can keep track of what is loaded
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
else{
Serial.println("Ethernet ready local IP:");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
}
void loop(){
Led();
Relay();
DoorbellDetection();
TelephoneDetection();
}
void Led(){
// Create a client connection
EthernetClient client = Ledserver.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE>Michael led server</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>Michael test server</H1>");
client.println("<a href=\"/?on\"\">ON</a>");
client.println("<a href=\"/?off\"\">OFF</a>");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
readString = readString.substring(5, readString.length() - 11);
Serial.println(readString); //print to serial monitor for debuging
int commaIndex = readString.indexOf(',');
// Search for the next comma just after the first
int secondCommaIndex = readString.indexOf(',', commaIndex+1);
String firstValue = readString.substring(0, commaIndex);
String secondValue = readString.substring(commaIndex+1, secondCommaIndex);
String thirdValue = readString.substring(secondCommaIndex+1); // To the end of the string
r = firstValue.toInt();
g = secondValue.toInt();
b = thirdValue.toInt();
ledstatut = r + b + g;
analogWrite(redPin, 255 - r);
analogWrite(greenPin, 255 - g);
analogWrite(bluePin, 255 - b);
Serial.println(r);
Serial.println(g);
Serial.println(b);
//clearing string for next read
readString="";
}
}
}
}
}
void Relay(){
EthernetClient client = Relayserver.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
boolean sentHeader = false;
while (client.connected()) {
if (client.available()) {
if(!sentHeader){
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
sentHeader = true;
}
char c = client.read();
if(reading && c == ' ') reading = false;
if(c == '?') reading = true; //found the ?, begin reading the info
if(reading){
Serial.print(c);
switch (c) {
case '1':
//relay pin 22 on
digitalWrite(22, HIGH);
Serial.println("Relay 1 on");
break;
case '2':
//relay pin 22 off
digitalWrite(22, LOW);
Serial.println("Relay 1 off");
break;
case '3':
//relay pin 23 on
digitalWrite(23, HIGH);
Serial.println("Relay 2 on");
break;
case '4':
//relay pin 23 off
digitalWrite(23, LOW);
Serial.println("Relay 2 off");
break;
case '5':
//relay pin 24 on
digitalWrite(24, HIGH);
Serial.println("Relay 3 on");
break;
case '6':
//relay pin 24 off
digitalWrite(24, LOW);
Serial.println("Relay 3 off");
break;
case '7':
//relay pin 25 on
digitalWrite(25, HIGH);
Serial.println("Relay 4 on");
break;
case '8':
//relay pin 25 off
digitalWrite(25, LOW);
Serial.println("Relay 4 off");
break;
case '9':
//relay pin 26 on
digitalWrite(26, HIGH);
Serial.println("Relay 5 on");
break;
case '0':
//relay pin 26 off
digitalWrite(26, LOW);
Serial.println("Relay 5 off");
break;
case 'a':
//relay pin 27 on
digitalWrite(27, HIGH);
Serial.println("Relay 6 on");
break;
case 'b':
//relay pin 27 off
digitalWrite(27, LOW);
Serial.println("Relay 6 off");
break;
case 'c':
//relay pin 28 on
digitalWrite(28, HIGH);
Serial.println("Relay 7 on");
break;
case 'd':
//relay pin 28 off
digitalWrite(28, LOW);
Serial.println("Relay 7 off");
break;
case 'e':
//relay pin 29 on
digitalWrite(29, HIGH);
Serial.println("Relay 8 on");
break;
case 'f':
//relay pin 29 off
digitalWrite(29, LOW);
Serial.println("Relay 8 off");
break;
case 'g':
//relay pin 29 on
digitalWrite(30, HIGH);
Serial.println("Foyer on");
break;
case 'h':
//relay pin 29 off
digitalWrite(30, LOW);
Serial.println("Foyer off");
break;
case 'i':
//Flash led pin
if (ledstatut > 0){
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
delay(600);
analogWrite(redPin, 255 - r);
analogWrite(greenPin, 255 - g);
analogWrite(bluePin, 255 - b);
delay(600);
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
delay(600);
analogWrite(redPin, 255 - r);
analogWrite(greenPin, 255 - g);
analogWrite(bluePin, 255 - b);
delay(600);
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
delay(600);
analogWrite(redPin, 255 - r);
analogWrite(greenPin, 255 - g);
analogWrite(bluePin, 255 - b);
delay(600);
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
delay(600);
analogWrite(redPin, 255 - r);
analogWrite(greenPin, 255 - g);
analogWrite(bluePin, 255 - b);
}
else{
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
delay(600);
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
delay(600);
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
delay(600);
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
delay(600);
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
delay(600);
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
delay(600);
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
delay(600);
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
Serial.println("Led flash");
break;
case 'j':
//+ brightness
r = (brightness - r);
g = (brightness - g);
b = (brightness - b);
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
Serial.println("+ brightness");
break;
case 'k':
//- brightness
r = (brightness + r);
g = (brightness + g);
b = (brightness + b);
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
Serial.println("- brightness");
break;
}
}
}
if (c == '\n' && currentLineIsBlank) break;
if (c == '\n') {
currentLineIsBlank = true;
}else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection:
}
}
void DoorbellDetection(){
////
// Arduino input detection code
////
if (digitalRead(Doorbell) == HIGH && DoorbellState == false) // switch on pinDevid1 is ON
{
if(DEBUG){Serial.println("Doorbell off");}
DoorbellState = true;
//Sending request to Vera
sendToVera(Doorbelloff);
}
if (digitalRead(Doorbell) == LOW && DoorbellState == true) // switch on pinDevid1 is OFF
{
if(DEBUG){Serial.println("Doorbell on");}
DoorbellState = false;
//Sending request to Vera
sendToVera(Doorbellon);
}
}
void TelephoneDetection (){
time = millis();
////
// Listening for the pin3 state
////
if (digitalRead(Phone) == HIGH && PhoneState == false) // switch on pinDevid1 is ON
{
PhoneState = true;
if(DEBUG){Serial.println("Phone off");}
sendToVera(Phoneoff);
}
if (digitalRead(Phone) == LOW && PhoneState == true) // switch on pinDevid1 is OFF
{
pressed = time,
PhoneState = false;}
if ((PhoneState == false) && ((time - pressed) > pwrtime))
{
if(DEBUG){Serial.println("Phone on");}
Phonedelay = true;
sendToVera(Phoneon);
}
}
void sendToVera(String devid){
if (client.connect(serverName, 49451)) {
if(DEBUG){Serial.println("connected to Vera");}
if(DEBUG){Serial.println("sendind request");}
client.print("GET /data_request?id=lu_action&output_format=xml&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum=");
client.print(devid);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(serverName);
client.println("User-Agent: Arduino");
client.println();
}
else {
if(DEBUG){Serial.println("connection failed");}
}
// if there are incoming bytes available
// from the server, read them and print them:
if(DEBUG){
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
if(DEBUG){Serial.println();}
if(DEBUG){Serial.println("disconnecting.");}
client.stop();
if (Phonedelay == true)
{
delay(1500);
Phonedelay = false;
}
}
case 'j':
//+ brightness
r = (brightness - r);
g = (brightness - g);
b = (brightness - b);
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
Serial.println("+ brightness");
break;
"r" is an "int", so can be less than zero.
If "r" goes less than zero, set it (and "g" and "b") to zero.
I'll leave the other case as an exercise.
i'm sorry, i understand what you say but where to but it: (If "r" goes less than zero, set it (and "g" and "b") to zero) i could put it after the analogwrite but did the light will flash before return to 0
Then place it where it has the bahaviour you require. If you need it to go to 255 or 0 before you do the analogWrite then write your condition accordingly, or change the order of your logic.
If I understand correctly if any component of the rgb triple reaches 255 don't increment any of the components of the rgb triple.
And on the reverse end if any component of the rgb triple reaches 0 don't decrement any of the components of the rgb triple.
Is this correct?
And what is 'brightness' used for?
smallpoul:
i want to dim a led strip. How can i limit the dim so that when it goes to 0, It stop, not returning to 255. and when it goes to 255, not returning to 0.
This code of yours does not look correct:
r = (brightness - r);
g = (brightness - g);
b = (brightness - b);
I think it's designed to reduce the brightness of all colours by brightness, but it doesn't. What I think you meant is:
r = r - brightness;
g = g - brightness;
b = b - brightness;
To prevent the values going below 0 or above 255 you can use the constrain() function. You could combine the update and the constraint like this:
// + brightness
r = constrain(r + brightness, 0, 255);
g = constrain(g + brightness, 0, 255);
b = constrain(b + brightness, 0, 255);
// - brightness
r = constrain(r - brightness, 0, 255);
g = constrain(g - brightness, 0, 255);
b = constrain(b - brightness, 0, 255);
I'm assuming here that the int variables r, g, b represent the current brightness of the LEDs and the variable brightness is the amount to adjust the brightness by.
Wow, you're printing things to the Serial port to aid in debugging - cool!
If might make a suggestion ...
#define DEBUG
#if defined(DEBUG)
#define DebugStr(STR) Serial.print(STR)
#else
#define DebugStr(STR)
#endif
... and then in your code do ...
DebugStr("Doorbell off");
... and when you want to stop outputting debug info to the Serial port simply comment out ...
//#define DEBUG
this will remove debugging output in mass with a simple change and compile.
Thanks guy.
I end up with this and it do the tricks
case 'j':
//led dim
r = r - 51;
g = g - 51;
b = b - 51;
if (r < 0) r = 0;
if (g < 0) g = 0;
if (b < 0) b = 0;
analogWrite(redPin, 255 - r);
analogWrite(greenPin, 255 - g);
analogWrite(bluePin, 255 - b);
Serial.println("led dim");
Serial.println(r);
Serial.println(g);
Serial.println(b);
break;
case 'k':
//led dim
r = r + 51;
g = g + 51;
b = b + 51;
if (r > 255) r = 255;
if (g > 255) g = 255;
if (b > 255) b = 255;
analogWrite(redPin, 255 - r);
analogWrite(greenPin, 255 - g);
analogWrite(bluePin, 255 - b);
Serial.println("led bright");
Serial.println(r);
Serial.println(g);
Serial.println(b);
break;
did there a simple way to fade between change up value.
I also add this to the code cause when certain color have a 0 value and i press the brightness, it change the color so now in one off the rgb is 0 and i want more brightness the one with the 0 value stay 0 and the color does not change.
case 'k':
//led dim
if (r > 0)
{r = r + 51;}
if (g > 0)
{g = g + 51;}
if (b > 0)
{b = b + 51;}
if (r > 255) r = 255;
if (g > 255) g = 255;
if (b > 255) b = 255;
analogWrite(redPin, 255 - r);
analogWrite(greenPin, 255 - g);
analogWrite(bluePin, 255 - b);
Serial.println("led bright");
Serial.println(r);
Serial.println(g);
Serial.println(b);
break;