arduino ac dimmer aansturen pwm?

Arduino specifieke Software
Berichten: 17
Geregistreerd: 23 Feb 2013, 14:51

arduino ac dimmer aansturen pwm?

Berichtdoor ddarduino » 23 Feb 2013, 15:14

hallo mensen,

ik ben met een arduino uno aan het stoeien met een ethernet shield erop.
daarmee wil ik iets maken om lichten aan en uit te zetten en te dimmen, dat eerste lukt perfect.
via de iphone app "ardumote" die verstuurt commando's p6h stuurt pin 6 aan, p6L stuurt pin 6 uit, en je kunt
er pwm mee verturen, hij verstuurt dan een waarde tussen de 0 en 255.
daarmee kun je perfect een led dimmen(of wat dan ook)
ik heb hierbij een schakeling om 230v lampen te dimmen, klein stukje programmering(demo) met vaste waarde en dat werkt perfect. nu wil ik die waarde kunnen sturen met waar ik normaal de led mee stuur om te dimmen.
(is het nog te snappen)
of als het anders, simpeler kan hoor ik het ook graag.
ik heb hieronder de code staan waarbij de 230v lamp automatisch dimt en weer op komt. maar dit moet dus via PWM(slider)
wie kan me helpen.


/*

AC Light Dimmer
AC Voltage dimmer with Zero cross detection

Attach the Zero cross pin of the module to Arduino External Interrupt pin
Select the correct Interrupt # from the below table

Pin | Interrrupt # | Arduino Platform
---------------------------------------
2 | 0 | All
3 | 1 | All
18 | 5 | Arduino Mega Only
19 | 4 | Arduino Mega Only
20 | 3 | Arduino Mega Only
21 | 2 | Arduino Mega Only

*/

int AC_LOAD = 3; // Output to Opto Triac pin
int dimming = 128; // Dimming level (0-128) 0 = ON, 128 = OFF

void setup()
{
pinMode(AC_LOAD, OUTPUT); // Set the AC Load as output
attachInterrupt(0, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
}

void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
// Firing angle calculation :: 50Hz-> 10ms (1/2 Cycle)
// (10000us - 10us) / 128 = 75 (Approx)
int dimtime = (75*dimming);
delayMicroseconds(dimtime); // Off cycle
digitalWrite(AC_LOAD, HIGH); // triac firing
delayMicroseconds(10); // triac On propogation delay
digitalWrite(AC_LOAD, LOW); // triac Off
}

void loop()
{
dimming = 128;
delay(100);
dimming = 75;
delay(100);
dimming = 25;
delay(100);

}

Advertisement

Berichten: 27
Geregistreerd: 23 Feb 2013, 12:07

Re: arduino ac dimmer aansturen pwm?

Berichtdoor walker » 23 Feb 2013, 18:18

Je dimmer werkt met fase aansnijding en die kun je niet met PWM aansturen. maar je kunt wel de waarde die je binnen krijgt voor PWM omrekenen en voor dimmer waarde gebruiken.

De dimming waarde (in je code) bepaald hoeveel licht de lamp geeft, dimming = 0 is de lamp maximaal en bij dimming = 128 is de lamp uit. Bij PWM is met de waarde 0 de uitgang 0 en met 255 de uitgang continue hoog.
Omrekening: dimming = 128 - (pwm_value / 2).

Gebruikers-avatar
Berichten: 229
Geregistreerd: 20 Jan 2013, 12:01

Re: arduino ac dimmer aansturen pwm?

Berichtdoor astrofrostbyte » 23 Feb 2013, 18:20

Hey ddarduino, ik snap het denk ik niet helemaal,
Maar wat ik zie op de Ardumote website is dat voor PWM het paketje met een Header 'PWM' verstuurd wordt.

Ik heb dit ff snel op de Ardumote site gevonden, Kijk eens naar de variabele 'pwmVal' hier staat na ontvangs gewoon de verzonden waarde in [0..255] , die je practisch direct in je dimming code gebruiken:
Code: Alles selecteren
int packetSize = Udp.parsePacket(); // note that this includes the UDP header
  if(packetSize)
  { packetSize = packetSize - 8;      // subtract the 8 byte header
    Serial.print("Packet size: ");
    Serial.println(packetSize);

    // read the packet into packetBuffer and get the senders IP addr and port number
    Udp.read(packBuff,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Message: ");
    Serial.println(packBuff);

 /* PWM - If we move a slider on Ardumote, it sends in a 3 digit value attached to the message of the slider.
     For example, if your message is set to be "PWM" and your slider is halfway set (slider value is 127),
     then your actual sent message will be received as "PWM127".  Therefore, to set the Pin's PWM value, you simply
     extract the last 3 digits of your message and use that as your PWM value (see below):
  */
    pwmVal = (packBuff[3] - '0')*100 + (packBuff[4] - '0')*10 + (packBuff[5] - '0');    //Get PWMXXX message, and use XXX to set an int between 0 and 255.
       
    if (packBuff[0] = 'P' && packBuff[1]=='W' && packBuff[2]=='M')  // Wait for "PWMXXX" and use XXX as value for PWM
    {
      analogWrite(LED_Pin,pwmVal);    //Set LED_Pin to PWM Value
      Serial.println("PWM on Pin 6");    //Write notification 
    }
    else if (packBuff[0] = 'P' && packBuff[1]=='6' && packBuff[2]=='H')  // If we get the message "P6H", then 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 we get the message "P6L", then 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();
    }   
  }
aarrrghh waneer krijgen we nu syntax highlighting


Trouwens erg cool tooltje die Ardomote , thnks voor de tip. Kan hem mischien toepassen voor een "Android naar KlikAan-KlikUit" bediening
Gear: Arduino- Uno,Due,Ethernet,Mega2560 , OLS LogicAnalyser, TDS1002, Rigol DG1022, J-Link EDU, BusPirate

Berichten: 17
Geregistreerd: 23 Feb 2013, 14:51

Re: arduino ac dimmer aansturen pwm?

Berichtdoor ddarduino » 23 Feb 2013, 22:44

iemand die me kan uitleggen hoe ik die pwm waarde kan koppelen aan die dimming waarde????
misschien staat de tip al wel hierboven, maar komt het niet door bij mij.... traag van begrip?? hihi

groet..

Gebruikers-avatar
Berichten: 229
Geregistreerd: 20 Jan 2013, 12:01

Re: arduino ac dimmer aansturen pwm?

Berichtdoor astrofrostbyte » 23 Feb 2013, 23:30

Laat ff de volledige code zien, het is volgens mij gewoon 'pwmVal' doorsluizen naar 'dimming'.
Gear: Arduino- Uno,Due,Ethernet,Mega2560 , OLS LogicAnalyser, TDS1002, Rigol DG1022, J-Link EDU, BusPirate

Gebruikers-avatar
Berichten: 229
Geregistreerd: 20 Jan 2013, 12:01

Re: arduino ac dimmer aansturen pwm?

Berichtdoor astrofrostbyte » 24 Feb 2013, 09:21

humz, :( merk trouwens net dat Ardomote er alleen voor de iPad is en niet voor Android, Ardumote is wel voor android maar is minder mooi.
Gear: Arduino- Uno,Due,Ethernet,Mega2560 , OLS LogicAnalyser, TDS1002, Rigol DG1022, J-Link EDU, BusPirate

Berichten: 17
Geregistreerd: 23 Feb 2013, 14:51

Re: arduino ac dimmer aansturen pwm?

Berichtdoor ddarduino » 24 Feb 2013, 13:31

hier de code die ik gebruik om pin6 aan te sturen via pwm en via de app op mijn telefoon kan ik deze led aan of uit sturen of dimmen dus via een slider.
en de code voor de ac 230v dimmer, met de loop erin dat die automatisch dimt, maar dat wil ik dus zelf kunnen aan sturen.
deze moeten dus gecombineerd worden. uiteraard ga ik meer schakelen en dimmen maar geen idee hoe ik moet beginnen.

#include <SPI.h> // for Arduino later than ver 0018
#include <EthernetUdp.h> // UDP library from bjoern@cs.stanford.edu
#include <Ethernet.h>

// source: http://code.google.com/p/arduino/source ... 2FEthernet


////////// NETWORK INFO ////////////////

byte mac[] = { 0x90, 0xa2, 0xda, 0x00, 0xf9, 0x64 }; //Set your Ethernet Shield's MAC address here - make sure you replace the ZZs with your shield's values!
byte ip[] = { 192, 168, 1, 8 }; // Set your shield's desired IP address here - check your network for configuration details
//byte gateway[] = { 192,168,1,1 }; //if you need to set a gateway IP
//byte subnet[] = { 255,255,255,0 }; // Change this to your subnet address

unsigned int localPort = 7777; // local port to listen on (set this the same as Port # on Ardumote Params Screen)


IPAddress iPhoneIP(192, 168, 1, 9); //Set the iPhone/iPod/iPad's IP address to send messages back to Ardumote...
unsigned int iPhonePort = 7777; //Set the Port # of the message table you configured in Ardumote (default is 7777)...


///////////////////////////////////////////


///////// Pin Assignments /////////////////

int LED_Pin = 6; //Set LED_Pin to Pin 6 - Place an LED on Pin 6 of your ethernet shield for testing this code

///////////////////////////////////////////



///////////////// UDP Variables //////////////////

// the next two variables are set when a packet is received
//byte remoteIp[4]; // holds received packet's originating IP
//unsigned int remotePort; // holds received packet's originating port

// buffers for receiving and sending data
char packBuff[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

/////////////////////////////////////////////////


EthernetUDP Udp;


void setup() {

// More info on Ethernet on Arduino's website: http://arduino.cc/en/Reference/EthernetBegin
// start the Ethernet and UDP:
Ethernet.begin(mac,ip); // If you don't need to set your default gateway or subnet manually, use this
// Ethernet.begin(mac,ip,gateway,subnet); // Use this line instead if you've manually set all the parameters


Udp.begin(localPort); //Setup UDP socket on port defined earlier

Serial.begin(9600); //Start Serial Communications with PC

pinMode(LED_Pin,OUTPUT); //Designate pin 6 as Output Pin



}

void loop()
{



int pwmVal; // Integer that will hold our PWM values for later use


// if there's data available, read a packet
int packetSize = Udp.parsePacket(); // note that this includes the UDP header
if(packetSize)
{
packetSize = packetSize - 8; // subtract the 8 byte header
Serial.print("Packet size: ");
Serial.println(packetSize);

// read the packet into packetBuffer and get the senders IP addr and port number
Udp.read(packBuff,UDP_TX_PACKET_MAX_SIZE);
Serial.println("Message: ");
Serial.println(packBuff);




/* PWM - If we move a slider on Ardumote, it sends in a 3 digit value attached to the message of the slider.

For example, if your message is set to be "PWM" and your slider is halfway set (slider value is 127),
then your actual sent message will be received as "PWM127". Therefore, to set the Pin's PWM value, you simply
extract the last 3 digits of your message and use that as your PWM value (see below):

*/

// Assuming our packBuff's contents at index values 3-5 are our PWM values, you can convert them to an int using this:

pwmVal = (packBuff[3] - '0')*100 + (packBuff[4] - '0')*10 + (packBuff[5] - '0'); //Get PWMXXX message, and use XXX to set an int between 0 and 255.

//////////////////////// Pin 6 (LED_Pin) /////////////////////////////////////

if (packBuff[0] = 'P' && packBuff[1]=='W' && packBuff[2]=='M') // Wait for "PWMXXX" and use XXX as value for PWM
{
analogWrite(LED_Pin,pwmVal); //Set LED_Pin to PWM Value

Serial.println("PWM on Pin 6"); //Write notification
}

else if (packBuff[0] = 'P' && packBuff[1]=='6' && packBuff[2]=='H') // If we get the message "P6H", then 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 we get the message "P6L", then 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);

}


en de code voor de ac 230v dimmer

/*

AC Light Dimmer
AC Voltage dimmer with Zero cross detection

Attach the Zero cross pin of the module to Arduino External Interrupt pin
Select the correct Interrupt # from the below table

Pin | Interrrupt # | Arduino Platform
---------------------------------------
2 | 0 | All
3 | 1 | All
18 | 5 | Arduino Mega Only
19 | 4 | Arduino Mega Only
20 | 3 | Arduino Mega Only
21 | 2 | Arduino Mega Only

*/

int AC_LOAD = 3; // Output to Opto Triac pin
int dimming = 128; // Dimming level (0-128) 0 = ON, 128 = OFF

void setup()
{
pinMode(AC_LOAD, OUTPUT); // Set the AC Load as output
attachInterrupt(0, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
}

void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
// Firing angle calculation :: 50Hz-> 10ms (1/2 Cycle)
// (10000us - 10us) / 128 = 75 (Approx)
int dimtime = (75*dimming);
delayMicroseconds(dimtime); // Off cycle
digitalWrite(AC_LOAD, HIGH); // triac firing
delayMicroseconds(10); // triac On propogation delay
digitalWrite(AC_LOAD, LOW); // triac Off
}

void loop()
{
dimming = 128;
delay(100);
dimming = 75;
delay(100);
dimming = 25;
delay(100);

}

hoop dat je er wat mee kan....

Gebruikers-avatar
Berichten: 229
Geregistreerd: 20 Jan 2013, 12:01

Re: arduino ac dimmer aansturen pwm?

Berichtdoor astrofrostbyte » 24 Feb 2013, 14:29

ik zie dat je de code van Ardomote en de DimmerCode nog niet in één sketch hebt staan. Ik heb de twee samengevoegd , ik heb wel veel commentaar gegehaalt om het leesbaar te krijgen.

Dit is het stukje waar de pwmVal ontvangen via ethernet in de Variabele 'dimming' gezet wordt, De zero cross interupt gebruikt deze variabele weer om de lamp aan te sturen.
Code: Alles selecteren
    if (packBuff[0] = 'P' && packBuff[1]=='W' && packBuff[2]=='M')
    {   
      pwmVal = (packBuff[3] - '0')*100 + (packBuff[4] - '0')*10 + (packBuff[5] - '0');
      if (pwmVal > 255) pwmVal=255;        // limit
      dimming = (pwmVal>>1);           // dimming = pwm / 2;
      analogWrite(LED_Pin,pwmVal);     // Set LED_Pin to PWM Value
      Serial.println("PWM on Pin 6");  // Write notification
    }


nog wat kleine uitzoek details:
Volgens mij kan de variabele dimming beter 'volatile' gezet worden omdat de loop() en de interrupt beide deze tegelijkertijd kunnen veranderen.
Moet de zerocrossing interupt niet optreden op Opgaande EN Neergaande flanken ? attachInterrupt(0, zero_crosss_int, RISING);




Dit is de volledige code. zonder syntax highlighting
Code: Alles selecteren
#include <SPI.h>         // for Arduino later than ver 0018
#include <EthernetUdp.h> // UDP library from bjoern@cs.stanford.edu
#include <Ethernet.h>
////////// NETWORK INFO ////////////////
byte mac[] = { 0x90, 0xa2, 0xda, 0x00, 0xf9, 0x64 }; //Set your Ethernet Shield's MAC address
byte ip[]  = { 192, 168, 1, 8 }; // Set your shield's desired IP address
unsigned int localPort = 7777; // local port to listen on (set this the same as Port # on Ardumote Params Screen)
IPAddress iPhoneIP(192, 168, 1, 9); //Set the iPhone/iPod/iPad's IP address to send messages back to Ardumote...
unsigned int iPhonePort = 7777; //Set the Port # of the message table you configured in Ardumote (default is 7777)...
///////////////// UDP Variables //////////////////
char packBuff[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
///////// Pin Assignments /////////////////
int LED_Pin = 6;   // Set LED_Pin to Pin 6
int AC_LOAD = 3;   // Output to Opto Triac pin
///////// Variables   ///////////////////////////
int dimming = 128; // Dimming level (0-128) 0 = ON, 128 = OFF
int pwmVal = 0;    // Integer that will hold our PWM values for later use

EthernetUDP Udp;
//-------------------------------------------------------------------------
void setup() {
  Ethernet.begin(mac,ip);     // If you don't need to set your default gateway or subnet manually, use this
  Udp.begin(localPort);       // Setup UDP socket on port defined earlier
  Serial.begin(9600);         // Start Serial Communications with PC
  pinMode(LED_Pin,OUTPUT);    // Designate pin 6 as Output Pin
  pinMode(AC_LOAD, OUTPUT);   // Set the AC Load as output
  attachInterrupt(0, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
}//setup

//-------------------------------------------------------------------------
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()
{
  int packetSize = Udp.parsePacket();      // note that this includes the UDP header
  if(packetSize)
  { packetSize = packetSize - 8;           // subtract 8 byte header
    Serial.print("Packet size: "); Serial.println(packetSize);
    // read the packet into packetBuffer and get the senders IP addr and port number
    Udp.read(packBuff,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Message: ");   Serial.println(packBuff);

    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 > 255) pwmVal=255;        // limit
      dimming = (pwmVal>>1);           // dimming = pwm / 2;
      analogWrite(LED_Pin,pwmVal);     // Set LED_Pin to PWM Value
      Serial.println("PWM on Pin 6");  // 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();
    }
  }//fi(packetSize)
  delay(20);
}//loop
//-------------------------------------------------------------------------

Gear: Arduino- Uno,Due,Ethernet,Mega2560 , OLS LogicAnalyser, TDS1002, Rigol DG1022, J-Link EDU, BusPirate

Berichten: 17
Geregistreerd: 23 Feb 2013, 14:51

Re: arduino ac dimmer aansturen pwm?

Berichtdoor ddarduino » 24 Feb 2013, 20:29

allereerst uiteraard bedankt,

alleen nu loop ik tegen het feit aan dat de 230v lamp knippert.
dit gaat me boven me pet...

iemand een idee heb al ff gegoogled maar zonder resultaat. en ook geen idee waar dat um in zit

Gebruikers-avatar
Berichten: 229
Geregistreerd: 20 Jan 2013, 12:01

Re: arduino ac dimmer aansturen pwm?

Berichtdoor astrofrostbyte » 24 Feb 2013, 20:43

Het is vrij normaal dat code niet direct goed loopt, debuggen hoort er gewoon bij.
Je moet nu gaan Debuggen...

voeg bv. een paar berichten toe voor de Serial Monitor in Arduino bv:
Serial.print("PWM Waarde is: "); Serial.println(pwmVal);
en
Serial.print("Dimming Waarde is: "); Serial.println(dimming);
Code: Alles selecteren
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');
      Serial.print("PWM Waarde is: ");   Serial.println(pwmVal);
      if (pwmVal > 255) pwmVal=255;        // limit
      dimming = (pwmVal>>1);           // dimming = pwm / 2;
      analogWrite(LED_Pin,pwmVal);     // Set LED_Pin to PWM Value
      Serial.print("Dimming Waarde is: ");   Serial.println(dimming);
    }
Gear: Arduino- Uno,Due,Ethernet,Mega2560 , OLS LogicAnalyser, TDS1002, Rigol DG1022, J-Link EDU, BusPirate

Volgende

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: eaupsowawar en 22 gasten