我正在使用 AC dimmersoftware serial 的灯光调暗为 wireless communication . 生成无线通信中断时,它会禁用软件串行库!结果导致轻弹/眨眼 . 有没有办法同时使用这两种方法?我也尝试过arduino内置noInterrupt()和interrupt()函数,但没有结果 .

删除软件串行实例 hc12 时,不会发生闪烁/闪烁

全局变量

SoftwareSerial hc12(9,10); // TX, RX 
int count = 0;
int count2 = 0;
double ledPin = 13;
double ledPin2 = 12;
double ledPin3 = 11;

volatile int i=0;  
double DETECT = 3;
// Variable to use as a counter
int AC_pin1 = 4;                // Output to Opto Triac load#1
int AC_pin2 = 5;    // Output to Triac load #2
int dim1 = 256;
int dim2 = 256;                  // Dimming level (0-128)  0 = on, 128 = 0ff
int inc=1;

int freqStep = 65; // This is the delay-per-brightness step in microseconds.
// It is calculated based on the frequency of your voltage supply (50Hz or 60Hz)
// and the number of brightness steps you want. 

// 1000000 us / 120 Hz = 8333 uS, length of one half-wave.
// 8333 uS / 128 steps = 65 uS / step

无效设置()

void setup() {    

hc12.begin(9600); // software serial 
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
//////////hc12/////////////////////
Serial.begin(9600);
pinMode(DETECT, INPUT); //zero cross detect1
digitalWrite(DETECT, HIGH); //enable pull-up resistor
pinMode(AC_pin1, OUTPUT);         // Set the Triac pin as output
pinMode(AC_pin2, OUTPUT);
digitalWrite(AC_pin1,HIGH);
digitalWrite(AC_pin2,HIGH);
attachInterrupt(1, zero_cross_detect, RISING);// Attach an Interupt to Pin 2 
(interupt 0) for Zero Cross Detection
Timer1.initialize(freqStep);// Initialize TimerOne library for the freq we 
need
Timer1.attachInterrupt(dim_check, freqStep);      
// Use the TimerOne Library to attach an interrupt
// to the function we use to check to see if it is 
// the right time to fire the triac.  This function 
// will now run every freqStep (65 uS for a 60Hz sinewave).                                            

}

调光器检查

// Turn on the TRIAC at the appropriate time
void dim_check() {                   
if(i>=dim1) {                     
  digitalWrite(AC_pin1, HIGH); // turn on triac #1
  }
 if (i>=dim2) {
  digitalWrite(AC_pin2, HIGH); // turn on triac #2
  }                                
 i++; // increment time step counter
}

无效循环

boolean ledStateR1 = digitalRead(ledPin);//check if the LED is turned on or 
off. Returns 1 or 0
 if(ledStateR1 == 0 && input == 77101){//if LED is off and button code is ok
       hc12.println(771011);
       Serial.print("771011");
       delay(20);
       hc12.println(7766111);/////dimmer
       dim1 =0;
       digitalWrite(ledPin,1);
       count=4;


   }