Thank you so much. I don't know all the details yet. In a real project, I will remove all Serial.prints. Can you please rate the program as a whole?
// set all halls as zero
int hallA = LOW; int hallB = LOW; int hallC = LOW;
volatile int PWM = 0; // PWM signal will be appears while some interruptions due to hall commands
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);pinMode(5, OUTPUT); pinMode(6, OUTPUT);pinMode(9, OUTPUT); pinMode(10, OUTPUT);pinMode(11, OUTPUT);
// First step: close all transistors
digitalWrite(3, LOW); digitalWrite(5, LOW);digitalWrite(6, LOW);digitalWrite(9, LOW); digitalWrite(10, LOW);digitalWrite(11, LOW);
// Arduino reads hall sensors of the BLDC motor (interraption sets for Leonardo)
pinMode(0, INPUT_PULLUP); // hallA - pin D0 - int 2
pinMode(1, INPUT_PULLUP); // hallB - pin D1 - int 3
pinMode(2, INPUT_PULLUP); // hallC- pin D2 - int 1
attachInterrupt(3, hall_CB, RISING); // hall_B RISING, Code(halls ACB) = 011, D1 - int3
attachInterrupt(1, hall__B, FALLING); // hall_C FALLING, Code = 001, D2 - int1
attachInterrupt(2, hallA_B, RISING); // hall_A RISING, Code = 101, D0 - int2
attachInterrupt(3, hallA__, FALLING); // hall_B FALLING, Code = 100, D1 - int3
attachInterrupt(1, hallAC_, RISING); // hall_C RISING, Code = 110, D2 - int1
attachInterrupt(2, hall_C_, FALLING); // hall_A FALLING, Code = 010, D0 - int2
int PWM = (analogRead(A0)/4);// read the potentiometer that adjust PWM
// at the first step, briefly turn the rotor once:
for (int i = 0; i < 1; i++) // at the first step, briefly turn the rotor once
{
int hallA = digitalRead(0);
int hallB = digitalRead(1);
int hallC = digitalRead(2);
{
if (hallA == LOW && hallC == HIGH && hallB == HIGH)
{
digitalWrite(9, PWM); // (A-)=pin9, transistor М4
digitalWrite(10, PWM); // (B+)= pin10, transistor М5
int X = 1;
}
if (hallA == LOW && hallC == LOW && hallB == HIGH)
{
digitalWrite(10, PWM); // (B+)=pin10, transistor М5
digitalWrite(11, PWM); // (C-)=pin11, transistor М6
int X = 2;
}
if (hallA == HIGH && hallC == LOW && hallB == HIGH)
{
digitalWrite(3, PWM); // (A+)=pin3, transistor М1
digitalWrite(11, PWM); // (C-)=pin11,transistor М6
int X = 3;
}
if (hallA == HIGH && hallC == LOW && hallB == LOW)
{
digitalWrite(3, PWM); // (A+)=pin3, transistor М1
digitalWrite(5, PWM); // (B-)=pin5, transistor М2
int X = 4;
}
if (hallA == HIGH && hallC == HIGH && hallB == LOW)
{
digitalWrite(6, PWM); // (C+)=pin6, transistor М3
digitalWrite(5, PWM); // (B-)=pin5, transistor М2
int X = 5;
}
if (hallA == LOW && hallC == HIGH && hallB == LOW)
{
digitalWrite(9, PWM); // (A-)=pin9, transistor М4
digitalWrite(6, PWM); // (C+)=pin6, transistor М3
int X = 6;
}
}
}
}
void loop() {
}
// programs that performed while interruptions at hall signals has been appeared. Considered phases are connected to + or -
// A-, B+
void hall_CB () {
digitalWrite(9, PWM); // (A-)=pin9, transistor М4
digitalWrite(10, PWM); // (B+)=pin10, transistor М5
}
// B+, C-
void hall__B () {
digitalWrite(10, PWM); // (B+)=pin10, transistor М5
digitalWrite(11, PWM); // (C-)=pin11, transistor М6
}
// A+, C-
void hallA_B () {
digitalWrite(3, PWM); // (A+)=pin3, transistor М1
digitalWrite(11, PWM); // (C-)=pin11, transistor М6
}
// A+, B-
void hallA__ () {
digitalWrite(3, PWM); // (A+)=pin3, transistor М1
digitalWrite(5, PWM); // (B-)=pin5, transistor М2
}
// C+, B-
void hallAC_ (){
digitalWrite(6, PWM); // (C+)=pin6, transistor М3
digitalWrite(5, PWM); // (B-)=pin5, transistor М2
}
// A-, C+
void hall_C_ (){
digitalWrite(9, PWM); // (A-)=pin9, transistor М4
digitalWrite(6, PWM); // (C+)= pin6, transistor М3
}