@techbutler, I’ll do some compile testing in the IDE a bit later today and get back to you.
@peekay123, I tried deleting the include library statement and then went back through the include process in the IDE and for whatever reason that got it to compile. But as the age old saying goes, just because it compiles doesn’t mean it works…which it doesn’t.
I tried a much simpler app using only delayMicroseconds() which also was successful with arduino, but no go on the core either. That code:
int AC_LOAD = D5; // Output to Opto Triac pin
int dimming = 128; // Dimming level (0-128) 0 = ON, 128 = OFF
void zero_cross_int();
void setup()
{
pinMode(AC_LOAD, OUTPUT);// Set AC Load pin as output
attachInterrupt(D1, zero_cross_int, RISING);
}
//the interrupt function must take no parameters and return nothing
void zero_cross_int() //function to be fired at the zero crossing to dim the light
{
// Every zerocrossing thus: For 60Hz => 8.33ms (10.000/120)
//
// 10ms=10000us
// (10000us - 8.33us) / 128 = 65
int dimtime = (65*dimming); // For 60Hz =>65
delayMicroseconds(dimtime); // Wait till firing the TRIAC
digitalWrite(AC_LOAD, HIGH); // Fire the TRIAC
delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
digitalWrite(AC_LOAD, LOW); // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
}
void loop() {
for (int i=5; i <= 128; i++){
dimming=i;
delay(10);
}
}
@techbutler, I am not sure why you are having compile issues but I will be doing some test to see what the issue might be. One your second version which uses delayMicroseconds(), this approach will cause huge delays in the interrupt service routine and will most likely stall your code or it will not work at all. I will get back to you soon
@techbutler, I copied your code (using SparkIntervalTimer) to a new application in the web IDE, add the SparkIntervalTimer library using “include in app” and selecting my new app. I then removed the #include line at the top of my new app added by the IDE since it is already in your code and hit verify and it all worked just fine!! Any thoughts?
Per my last post I was able to get it to compile doing exactly the same thing you did. My problem now is the code doesn’t work. I fully understand the gross inefficiencies of the delay microseconds approach and believe me I want to stick with your library, I’m just trying to find something simple to run that will run successfully on the Arduino and then on the core.
@techbutler can you point me to the hardware setup you are using?
@techbutler, the Arduino (Uno I assume) uses 5V input/output whereas the Spark is uses 3.3V. Fortunately, most of the digital input pins are 5V tolerant, including D1 used as your zero crossing input. I assume you are using the circuit as-is and powering from Vin or a 5V supply, correct?
The problem may be in driving the opto-isolated triac. The resistor selection is designed for 5V operation and using 3.3V may not be enough to drive the opto LED. This could explain why it works on the Arduino and not on the Spark. You could use a logic level shifter to boost the Spark output to 5V.
Questions:
- Have you determined if the Spark is receiving (on pin D1) and responding to zero-crossing events?
- Do you have scope to look at the optio-triac input and the AC output?
@peekay123, been working it this morning… First off, I have been using a SparkFun bi-directional logic converter from the git-go.
I added a line in setup to fire the AC pin High and the light comes on and stay on. I added a delay line after that and it was ignored…the light came on and stayed on.
I don’t have a scope per se, just my meter that I have been checking voltages everywhere as well as my pro mini to reference successful runs.
There doesn’t seem to be any voltage changes at D1 or the AC output. Its like the code just stops at the interrupt.
I will note that when I have the setup line that drives the AC pin high, the voltage coming out of the high side of the logic converter is only 2.6v. I would have expected in the neighbor hood of 5v since that is the ref for the high side. But 3.3 => 2.6 I don’t get. i swapped out converters and got the same thing.
I’m fully aware that mysteries like usually point to me as the culprit…we just have to figure out what I did or didn’t do
@peekay123, So to date my arduino comparisons have been with a 5v pro mini. I wired up a 3.3v pro mini with a logic converter and the results were similar to the Spark as far as the converter is concerned…pushing out 2.7v to the dimmer board for what is supposed to be HIGH. What wasn’t the same is that the D1 input voltage was at a level I’ve seen on previous successful arduino runs.
Hi, I have a problem with emon (Open Energy Monitor project). When I use IntervalTimer to append counter variable. And then in main loop I measure energy then IntervalTimer dies (stop countring). What I noticed was that emon uses around 1000 samples (iteration) to calculate current and seems like this is too large loop for IntervalTimer. But if I’ll set samples 100 then IntervalTimer does not die. I’m using following code to start timer:
x.begin(my_func, 5000, uSec);
Any thoughts?
@techbutler, if you disconnect the level converter from the opto-triac and measure the output do you get 5V or so? Do you have the level converter properly wired?
From your code, D1 is the INPUT from the zero crossing and the D5 is your OUTPUT to the triac. I would add pinMode(D1, INPUT);
in setup just to be sure. Trying to measure D1 with a meter won’t work since it changing at 60Hz. You could add code in zero_cross_detect() to toggle the onboard LED on D7. If the zero-cross signal is working on D1 then you should see LED D7 pulsing at 30Hz or so - it will appear ON but if it is not ON at all then the interrupts are not working.
I you want a cheap and decent starter scope to debug this kind of low voltage stuff, you may want to look at what Gabotronics has to offer. I have the XprotoLab portable but the XminiLab has a nicer screen.
I seem to have a strange problem with this library - I’m sure it’s something I’m doing wrong, but for the life of me I cannot figure out what is going on. I’m using a temperature sensor and the relay shield to turn on/off a heat lamp for my chicks.
Everything works until my relay (D0) is turned on, at which point my timer seems to be around 4 times faster. If I use TIMER4 this does not happen, rather it gets slower at some apparently random time and then “magically” speeds up again and works fine.
I’m using 1 SparkIntervalTimer as follows:
IntervalTimer tenMsTimer;
Then in setup() I use:
tenMsTimer.begin(tenMsTimerFunc, 20, hmSec); // every 10ms
My timer function looks like this:
void tenMsTimerFunc(){
tenMsScalar++;
// every 100ms
if (tenMsScalar >= 10){
tenMsScalar = 0;
hundredMsScalar++;
// every 500ms
if (hundredMsScalar % 5 == 0){
flagUpdate500ms = true;
}
// every sec
if (hundredMsScalar >= 10){
hundredMsScalar = 0;
secondScalar++;
flagUpdate1s = true;
// every 2 seconds
if (secondScalar % 2 == 0){
flagUpdate2s = true;
}
// every 5 seconds
//if (secondScalar % 5 == 0){
// flagUpdate5s = true;
//}
// every 10 seconds
if (secondScalar % 10 == 0){
flagUpdate10s = true;
}
// every 30 seconds
if (secondScalar % 30 == 0){
flagUpdate30s = true;
}
// every min
if (secondScalar >= 60){
secondScalar = 0;
flagUpdate1m = true;
}
}
}
}
In my main loop I then check for the specific flags and read my temp sensor (every 500ms), read the ambient temp and humidity (with a DHT22 every 2 seconds), add the readings to circular buffers, sort them, calculate the median, and every 10 seconds make a decision (with hysteresis) to turn on or off the heat lamp attached to the relay (D0).
I’m not sure what I’m doing wrong - I’m relatively new to embedded programming and don’t always think “embedded”.
Any help is greatly appreciated!
@davethebrave, I would have to see the loop() code to provide any recommendations. One thing to remember is that the DHT22 code disables user interrupts while it is reading the DHT sensor.
@peekay123 Yes, I did notice that and did not like it - However, I found a DHT22 Arduino library that does not disable interrupts - it is however blocking: http://www.github.com/markruys/arduino-DHT
For that reason I made a define to quickly disable the DHT22 - however it does not seem to change the behavior.
Here is my main loop:
void loop(){
if (flagUpdate500ms){
flagUpdate500ms = false;
tmp36.read();
tmp36Filter.addValue(tmp36.getTemperature());
}
if (flagUpdate1s){
flagUpdate1s = false;
// decrement cool down timer
if (coolDownTime){
coolDownTime--;
}
}
if (flagUpdate2s){
flagUpdate2s = false;
#ifdef USE_ENV_SENSOR
dht22.read();
envTempFilter.addValue(dht22.getTemperature());
humidityFilter.addValue(dht22.getHumidity());
#endif
}
//if (flagUpdate5s){
// flagUpdate5s = false;
//
//}
if (flagUpdate10s){
flagUpdate10s = false;
// update 10s history
tempHistory[0][0] = tempHistory[0][1];
tempHistory[0][1] = tmp36Filter.getValue();
#ifdef USE_ENV_SENSOR
envTempHistory[0][0] = envTempHistory[0][1];
envTempHistory[0][1] = envTempFilter.getValue();
humidityHistory[0][0] = humidityHistory[0][1];
humidityHistory[0][1] = humidityFilter.getValue();
#endif
if (coolDownTime == 0){
decideHeatingCooling();
}
}
if (flagUpdate30s){
flagUpdate30s = false;
// update 30s history
tempHistory[1][0] = tempHistory[1][1];
tempHistory[1][1] = tempHistory[0][1];
#ifdef USE_ENV_SENSOR
envTempHistory[1][0] = envTempHistory[1][1];
envTempHistory[1][1] = envTempHistory[0][1];
humidityHistory[1][0] = humidityHistory[1][1];
humidityHistory[1][1] = humidityHistory[0][1];
#endif
}
if (flagUpdate1m){
flagUpdate1m = false;
// publish temperature
sprintf(pubStr, "{\"temp\":[%.2f,%.2f,%.2f]}", tmp36Filter.getValue(), tempHistory[0][0], tempHistory[1][0]);
Spark.publish("temp", pubStr, 300, PRIVATE);
#ifdef USE_ENV_SENSOR
// publish environment
sprintf(pubStr, "{\"temp\":[%.2f,%.2f,%.2f]},\"hum\":[%.2f,%.2f,%.2f]}", envTempFilter.getValue(), envTempHistory[0][0], envTempHistory[1][0], humidityFilter.getValue(), humidityHistory[0][0], humidityHistory[1][0]);
Spark.publish("env", pubStr, 300, PRIVATE);
#endif
}
}
@davethebrave, which DHT22 library were you using before? Can you show me setup() and the code where you change pin D0?
@peekay123 I haven’t used a different library - during my research I noticed that it disabled the interrupts and figured this would interfere with the timer library so I decided to not use the DHT22 until I could find a way around that.
My setup is really simple:
pinMode(D0, OUTPUT);
This is where I control the state of D0:
void heatControl(byte state){
static byte heatState = LOW;
if (state != heatState){
digitalWrite(D0, state);
sprintf(pubStr, "{\"heat\":%u}", state);
Spark.publish("ctrl", pubStr, 180, PRIVATE);
heatState = state;
}
}
@davethebrave, I really need to see your entire code. Do you have any hysteresis (delay) where you call heatControl()?
@peekay123 I’ll create a pastebin or something and post the link. Or is there a way to share from the WebIDE?
@davethebrave, there is no way to share IDE code