首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用timer,Simpletimer或Timer_Three,Timer_Five作为中断/Better解决方案?

使用timer,Simpletimer或Timer_Three,Timer_Five作为中断/Better解决方案?
EN

Stack Overflow用户
提问于 2016-01-17 08:02:35
回答 1查看 167关注 0票数 0

我在试着把素描画出来,但还没成功。

我想设置引脚高每100ms,这是50赫兹交流电源的5个完整的波。当打开时,我想启动第二个定时器,以便在0-100ms后将其关闭。因此,我可以捕获1-10个半波与ssr包含。零交叉..。

尝试使用SimpleTimer不起作用

代码语言:javascript
复制
#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);  //
}

不幸的是,禁用不起作用!它不会禁用像mad那样运行的enything...just :-)

代码语言:javascript
复制
void loop()
{
   int off_time=MOTORspeed/100;
   mot_off.setInterval(100*off_time, off); 
}

我试着用Timer3和Timer 5...也不走运: skript不会停止计时器!

代码语言:javascript
复制
#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);
}
EN

回答 1

Stack Overflow用户

发布于 2016-01-17 20:35:06

答案来了!它适用于所有具有过零功能的SSR,可以在10步内改变速度(您可以在脚本中更改它,以获得最多100000步:-)

使用SimpleTimer:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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() 
{  

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34833550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档