Hello... i am new to the Arduino and i am having a little trouble with timing and interrupts on the project i am currently working on and i was hoping someone on this forum could help / point me in the right direction as i have been stuck on this for a few days and now i am just re-reading all the sites on google that i have previously read that is close to my problem... but i have not found a solution, so here goes...
I have built a circuit to control an AC bulb with a MOSFET from the Arduino. the circuit has a zero cross detector and will be used to change from leading and trailing edge dimming. the circuit works fine and if i create a loop to increment the dim time then it all works fine and smooth... the problems start when i add the Arduino Yun bridge into the mix which i think is also interrupt based and seems that the values i send in over NETIO are just being lost straight away and it will not operate... i have got it to work nearly correct but i have had to enable interrupts in two places (see code below) but this has its own problems again... when the value is low and thus the dim time is low then it will fade smoothly on the NETIO slider... the problems start when i get past about half way... because of the delayMicro in the zero interrupt section it then takes longer to complete and has less time for void loop... so it is nearly working but i cant seem to put the finishing touches on it and i am a little lost how to proceed from this point.
i will post the code below and hope that someone can steer me in the right direction to get this slider smooth. if you need any more information then please ask and i will provide... Thank You for looking.
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
YunServer server(23);
String cmd;
int AC_LOAD = 11;
int dimVal = 0; // 0-128
void setup()
{
Serial.begin(9600);
pinMode(AC_LOAD, OUTPUT);
attachInterrupt(0, zero_cross_int, RISING);
pinMode(13,OUTPUT);
digitalWrite(13, LOW);
Bridge.begin();
digitalWrite(13, HIGH);
server.noListenOnLocalhost();
server.begin();
}
void zero_cross_int()
{
interrupts();
int dimtime = (75*dimVal);
digitalWrite(AC_LOAD, HIGH);
delayMicroseconds(dimtime);
digitalWrite(AC_LOAD, LOW);
}
void loop()
{
YunClient client = server.accept();
client.setTimeout(0);
while(client.connected())
{
if(client.available())
{
interrupts();
cmd = client.readString();
client.print(cmd);
dimVal = cmd.toInt();
}
}
}