Go Down

Topic: second dim function for home system (Read 928 times) previous topic - next topic

Mar 01, 2013, 09:51 am Last Edit: Mar 01, 2013, 08:00 pm by ddarduino Reason: 1
Hey There,

i'm making a home lighting system with arduino and a ethernetshield.
i can switch lights on and off with my iphone/ipad/ipod and witch pulsswithses in the wall.
and i can dim one of them, but i want to dimming more then one.
but i can get this work. can someone help me?

Code: [Select]
this is de code:
#include <SPI.h> // voor Arduino versie 0018 en hoger
#include <EthernetUdp.h>
#include <Ethernet.h>
////////// NETWORK INFO ////////////////
byte mac[] = { 0x90, 0xa2, 0xda, 0x00, 0xf9, 0x64 }; // mac adres van deethernet shield
byte ip[] = { 192, 168, 1, 8 }; // het ip adres dat je aan je arduio wilt geven
unsigned int localPort = 7777; // de poort die je wilt gebruiken
IPAddress iPhoneIP(192, 168, 1, 15); //ip adres van je iphone/ipad/ipod
unsigned int iPhonePort = 7777; // poort van de iphone/ipad/ipod (zelfde als localport)
///////////////// UDP Variables //////////////////
char packBuff[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
///////// Pin verklaring /////////////////
int LED_Pin = 6; // maakt LED_Pin van Pin 6
const int dim_up = 7; // dim up
int dim_down = 9; //dim down
int AC_LOAD = 3; // Output to Opto Triac pin
///////// Variabelle //
int dimming = 128; // Dimming level (0-128) 0 = ON, 128 = OFF
int pwmVal = 0; // Integer that will hold our PWM values for later use
int buttonState = 0;
EthernetUDP Udp;
//-------------------------------------------------------------------------
void setup() {
Ethernet.begin(mac,ip);
Udp.begin(localPort);
Serial.begin(9600);
pinMode(LED_Pin, OUTPUT);
pinMode(AC_LOAD, OUTPUT);
pinMode(dim_up, INPUT);
pinMode(dim_down, INPUT);
attachInterrupt(0, zero_crosss_int, RISING);
}
//-------------------------------------------------------------------------
void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{ // Firing angle calculation :: 50Hz-> 10ms (1/2 Cycle)
int dimtime = (75*dimming); // (10000us - 10us) / 128 = 75 (Approx)
delayMicroseconds(dimtime); // Off cycle
digitalWrite(AC_LOAD, HIGH); // triac firing
delayMicroseconds(10); // triac On propogation delay
digitalWrite(AC_LOAD, LOW); // triac Off
}
//-------------------------------------------------------------------------
void loop()
{
// read the state of the pushbutton value:
buttonState = digitalRead(dim_up);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
Serial.println("button higher");
dimming -= 2;
if (dimming < 20) dimming = 20;
}
{
// read the state of the pushbutton value:
buttonState = digitalRead(dim_down);
if (buttonState == HIGH) {
Serial.println("button lower");
// turn higher on:
dimming += 2;
if (dimming > 112) dimming = 125;
}
int packetSize = Udp.parsePacket();
if(packetSize)
{ packetSize = packetSize - 8; // subtract 8 byte header
Udp.read(packBuff,UDP_TX_PACKET_MAX_SIZE);
if (packBuff[0] = 'P' && packBuff[1]=='W' && packBuff[2]=='M') // Wait for "PWMXXX", use XXX as PWM value
{
pwmVal = (packBuff[3] - '0')*100 + (packBuff[4] - '0')*10 + (packBuff[5] - '0');
if (pwmVal > 250) pwmVal= 250; // limit
if (pwmVal < 15) pwmVal= 15; // limit
dimming = (pwmVal>>1); // dimming = pwm / 2;
Serial.println("Dimming Waarde is: "); Serial.println(dimming); // Write notification
}
else if (packBuff[0] = 'P' && packBuff[1]=='6' && packBuff[2]=='H') // If message "P6H", set LED_Pin (6) HIGH
{
digitalWrite(LED_Pin,HIGH); // Turn on LED_Pin
Serial.println("LED ON"); // Write notification
Udp.beginPacket(iPhoneIP,iPhonePort);
Udp.write("LED 6 is ON"); // Send Message back to iPhone
Udp.endPacket();
}
else if (packBuff[0] = 'P' && packBuff[1]=='6' && packBuff[2]=='L') // If message "P6L", set LED_Pin (6) LOW
{
digitalWrite(LED_Pin,LOW); // Turn off LED_Pin
Serial.println("LED OFF"); // Write notification
Udp.beginPacket(iPhoneIP, iPhonePort);
Udp.write("LED 6 is OFF"); // Send Message back to iPhone
Udp.endPacket();
}


delay(20);


}}}

zoomkat

#1
Mar 01, 2013, 04:37 pm Last Edit: Mar 01, 2013, 04:59 pm by zoomkat Reason: 1
I suggest you use the code auto format tool in the IDE, then put your code in a code box when posting. You can use the "modify" to fix your post. Check the below post.

http://arduino.cc/forum/index.php/topic,148850.0.html
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy