Deadtime for interruption

Hello. Can you help me, please, is the delay in the place into the loop is correct? Will it delay calls to void hall_BC() programs and so on?

void loop() {
  int power = analogRead(A0) / 4;  // read the potentiometer that adjust PWM
  noInterrupts();  // Disable interrupt while volatile variable are updated
  PWM = power;
  interrupts();
if (millis() - timing > deadtime){ 
  timing = millis(); 
 }
}

The whole scetch is



volatile int PWM = 0;  // PWM signal for Hall sensor ISRs
const byte deadtime = 10; //exclude short circuit of half-bridge transistors
unsigned long timing; // Variable to store the reference point

const byte HallAPin = 0;  // yellow to D0 /  Note: Would conflict with Serial1 on this Leonardo
const byte HallBPin = 1;  // green to D1 /  Note: Would conflict with Serial1 on this Leonardo
const byte HallCPin = 2; // blue to D2

const byte APlusPin = 3;    // (A+)=pin3, transistor М1
const byte BMinusPin = 11;   // (B-)=pin11, transistor М6
const byte CPlusPin = 10;    // (C+)=pin10, transistor М5
const byte AMinusPin = 9;   // (A-)=pin9, transistor М4
const byte BPlusPin = 6;   // (B+)=pin6, transistor М3
const byte CMinusPin = 5;  // (C-)=pin5, transistor М2

void AllTransistorsOff() {
  digitalWrite(APlusPin, LOW);   
  digitalWrite(BMinusPin, LOW);  
  digitalWrite(CPlusPin, LOW);   
  digitalWrite(AMinusPin, LOW);  
  digitalWrite(BPlusPin, LOW);   
  digitalWrite(CMinusPin, LOW);  
}

void setup() {
  Serial.begin(115200);
  delay(200);
  // Wait for USB connection or 5 seconds
  while (!Serial && millis() < 5000) {}

  AllTransistorsOff();
   
  pinMode(AMinusPin, OUTPUT);  
  pinMode(CPlusPin, OUTPUT);   
  pinMode(BMinusPin, OUTPUT);  
  pinMode(APlusPin, OUTPUT);   
  pinMode(CMinusPin, OUTPUT);  
  pinMode(BPlusPin, OUTPUT);   

digitalWrite(LED_BUILTIN, LOW);
  pinMode(LED_BUILTIN, OUTPUT);

  // Leonardo reads hall sensors of the BLDC motor
  pinMode(HallAPin, INPUT_PULLUP);  //  hallA - pin D0 yellow
  pinMode(HallBPin, INPUT_PULLUP);  //  hallB- pin D1 green
  pinMode(HallCPin, INPUT_PULLUP);  // hallC - pin D2 blue

  attachInterrupt(digitalPinToInterrupt(HallAPin), HallAPinISR, CHANGE);  // hallA - pin0 - yellow
  attachInterrupt(digitalPinToInterrupt(HallBPin), HallBPinISR, CHANGE);  // hallB - pin1 - green
  attachInterrupt(digitalPinToInterrupt(HallCPin), HallCPinISR, CHANGE);  // hallC  - pin2- blue
}

void loop() {
  int power = analogRead(A0) / 4;  // read the potentiometer that adjust PWM
  noInterrupts();  // Disable interrupt while volatile variable are updated
  PWM = power;
  interrupts();
if (millis() - timing > deadtime){ 
  timing = millis(); 
 }
}

// pin0 / hallA interaption
void HallAPinISR() {
  if (digitalRead(0) == HIGH) {
    digitalWrite(LED_BUILTIN, HIGH);
    hallA_C();  // code change from 001 to 101 / A+, B-
  } else {
    digitalWrite(LED_BUILTIN, LOW);
    hall_B_();  // code change from 110 to 010 / A-, B+
  }
 }

// pin1 / hallB interaption
void HallBPinISR() {
  if (digitalRead(1) == HIGH)
    hallAB_();  // code change from 100 to 110 / B+, C-
  else
    hall__C();  // code change from 011 to 001 /  B-, C+
}

// pin2 / hallC interaption
void HallCPinISR() {
  if (digitalRead(2) == HIGH)
    hall_BC();  // code change from 010 to 011 / A-, B+
  else
    hallA__();  // code change from 101 to 100 /  A+, C-
}


// A-, C+
void hall_BC() {
  AllTransistorsOff();
  analogWrite(AMinusPin, PWM); 
  analogWrite(CPlusPin, PWM); 
  //Serial.println("A-, C+");  
   }

// B-, C+
void hall__C() {
  AllTransistorsOff();
  analogWrite(BMinusPin, PWM);  
  analogWrite(CPlusPin, PWM); 
  Serial.println("B-, C+");  
}

// A+, B-
void hallA_C() {
  AllTransistorsOff();
  analogWrite(APlusPin, PWM);  
  analogWrite(BMinusPin, PWM);  
  Serial.println("A+, B-");
}


// A+, C-
void hallA__() {
  AllTransistorsOff();
  analogWrite(APlusPin, PWM);   
  analogWrite(CMinusPin, PWM);  
 Serial.println("A=, C-");
}

// B+, C-
void hallAB_() {
  AllTransistorsOff();
  analogWrite(BPlusPin, PWM);   
  analogWrite(CMinusPin, PWM); 
  Serial.println("B+, C-"); 
}


// A-, B+
void hall_B_() {
  AllTransistorsOff();
  analogWrite(AMinusPin, PWM);  
  analogWrite(BPlusPin, PWM);   
  Serial.println("A-, B+");
}















It’s not a good idea to do anything ‘slow’ inside an ISR.

i.e. a Serial.print is s l o w., so it’s better not to do that inside the isr code.

I will cancel Serial.print from the final version

There is no delay in the loop.


Probably more logical to call this after setting the pin as output?


What’s the point of this??


Use variable names instead of magic numbers like 0, 1 and 2

As your comment also indicated not sure 0 is a good pin to use. What’s your arduino?

The delay you have in the loop() function will not affect calls to hall_BC() and other similar functions that are being triggered by interrupts. The delay you have here will only cause a delay in the loop() function itself, but it will not affect the processing of the interrupt-based functions. Interrupts take priority over the loop() function, so even if the loop() function is delayed, the interrupt-based functions will still be executed in a timely manner.

which delay?

To do like this?

void hall_BC() {
  AllTransistorsOff();
delayMicroseconds(5);  
  analogWrite(AMinusPin, PWM); 
  analogWrite(CPlusPin, PWM); 
   }

If you want a delay in your loop, just put one in your loop.

void loop() {
  int power = analogRead(A0) / 4;  // read the potentiometer that adjust PWM
  noInterrupts();  // Disable interrupt while volatile variable are updated
  PWM = power;
  interrupts();

  delay(deadline);
}

That thing you had with millis() was ineffective. Run your finger over it pretending to be a dumb microprocessor and you will see that.

a7