使用定时器,Simpletimer或Timer_Three,Timer_Five作为中断/更好的解决方案?

时间:2016-01-17 00:02:35

标签: c arduino

我试图让草图运行,但还没有运气。

我想每100ms将引脚设置为高电平,这是5个50hz交流电源的全波。当打开时,我想启动第二个计时器,在0-100ms后关闭它。所以我可以用ssr Incl来捕获1-10个半波。零交叉...

尝试使用SimpleTimer无法正常工作

#include <SimpleTimer.h>

SimpleTimer mot_on;
SimpleTimer mot_off;
int MOTORspeed=50;
int timer_id;

void setup()
{
   Serial.begin(115200);
   pinMode(19, OUTPUT);
   mot_on.setInterval(1000, on);       // initialize timer1, and set a 100ms= 5 full waves=10% of full cycle

//   mot_off.setInterval(10, off);    // initialize timer
   timer_id=mot_off.setInterval(1000, off);
   Serial.println(timer_id);
}

void on()
{
  if (MOTORspeed>90)
  {
     mot_off.enable(0);
  }
  digitalWrite(19, HIGH);   // Fire the TRIAC
}

void off()
{
   digitalWrite(19, LOW);   // Fire the TRIAC
   mot_off.disable(0);  //
}
遗憾的是禁用不会生效!它不会禁用enything ...只是疯狂地运行:-)

void loop()
{
   int off_time=MOTORspeed/100;
   mot_off.setInterval(100*off_time, off); 
}

我尝试使用Timer3和Timer 5 ......也没有运气: skript不会停止计时器!

#include <TimerThree.h>
#include <TimerFive.h>

int MOTORspeed=50;
int timer_id;

void setup()
{
   Serial.begin(115200);
   pinMode(19, OUTPUT);

  Timer3.attachInterrupt(on);
  Timer3.initialize(100000);

  Timer5.attachInterrupt(off);
  Timer5.initialize(10000);
}

void on()
{
  if (MOTORspeed>90)
   {Timer5.start();}//
   Serial.println("ON");
   digitalWrite(19, HIGH);   // Fire the TRIAC
}
void off()
{
   digitalWrite(19, LOW);   // Fire the TRIAC
   Serial.println("off");
   Timer5.stop();  //
}     
void loop()
{
  int off_time=MOTORspeed/100;
  Timer5.setPeriod(100*off_time);
}

1 个答案:

答案 0 :(得分:0)

我们走了,答案!!!! 它适用于所有具有过零功能的SSR,可以在10个步骤中更改速度(您可以在脚本中更改它以达到100000 :-)步骤)

使用SimpleTimer:

#include <SimpleTimer.h>

SimpleTimer mot_off;

volatile int i;               // Variable to use as a counter of dimming steps. It is volatile since it is passed between interrupts
int AC_pin = 19;                 // Output to Opto Triac

int mot_speed = 50;                  // in % Speed level (0-100)  0 = off, 100 = full speed
                                //1 Second = 1.000
int freqStep = 20;              // Means 20ms ONE FULL wave. Every 20ms we check if we should switch SSR off to get the right speed
                                //if speed is 50 (50%) we set Pin HIGH every 200ms, but after 5x calling Timer (i=50)Time = 100ms we set PIN low for next 5x calling of timer)
                                //if speed is 10 (10%) we set Pin HIGH every 200ms, but after 1x calling Timer (i=10)Time = 20ms we set PIN low for next 9x calling of timer)

void setup()
{
   Serial.begin(115200);
   pinMode(AC_pin, OUTPUT);   
   mot_off.setInterval(freqStep, off_check);
}

void off_check() {

    if(i>mot_speed) 
    {                     
      digitalWrite(AC_pin, LOW);  // turn off SSR 
//      Serial.println("OFF");
//      Serial.println(i);                 
    } 
    else 
    {
      digitalWrite(AC_pin, HIGH);
//      Serial.println("ON");
//      Serial.println(i);           
    } 
    i+=10;  
    if (i>100) {i=0;}                                 
}

void loop()
{
  mot_off.run();
}

和TimerOne:

#include <TimerOne.h>           // Avaiable from http://www.arduino.cc/playground/Code/Timer1

volatile int i;               // Variable to use as a counter of dimming steps. It is volatile since it is passed between interrupts
int AC_pin = 19;              // Output to Opto Triac

int mot_speed = 50;           // in % Speed level (0-100)  0 = off, 100 = full speed

                                  //1 Second = 1.000.000
int freqStep = 20000;             // Means 20ms ONE FULL wave. Every 20ms we check if we should switch SSR off to get the right speed
                                  //if speed is 50 (50%) we set Pin HIGH every 200ms, but after 5x calling Timer (i=50)Time = 100ms we set PIN low for next 5x calling of timer)
                                  //if speed is 10 (10%) we set Pin HIGH every 200ms, but after 1x calling Timer (i=10)Time = 20ms we set PIN low for next 9x calling of timer)



void setup() {  // Begin setup
  Serial.begin(115200);   
  pinMode(AC_pin, OUTPUT);                          // Set the Triac pin as output
  Timer1.initialize(freqStep);                      // Initialize TimerOne library for the freq we need
  Timer1.attachInterrupt(off_check, freqStep);      // 
  // Use the TimerOne Library to attach an interrupt
}

void off_check() {

    if(i>mot_speed) 
    {                     
      digitalWrite(AC_pin, LOW);  // turn off SSR 
//      Serial.println("OFF");
//      Serial.println(i);                 
    } 
    else 
    {
      digitalWrite(AC_pin, HIGH);
//      Serial.println("ON");
//      Serial.println(i);           
    } 
    i+=5;  
    if (i>100) {i=0;}

}

void loop() 
{  

}