Arduino Nano Ciruit not Executing Code as Programmed to Execute.

Hi,
I have designed an ESC using arduino nano and other complementary electronic components, written a code to control a three phase DC motor, compiled and uploaded the code successfully unto my arduino nano board. I replaced the six power transistors of the ESC inverter bridge with six LEDs in order to simulate and check that my code is responding as written, but to my greatest amazement the circuit executes only “step 2” of the entire programme and nothing else. Also, the potentiometer intended to control the speed of the motor only draw excess power and shuts down the arduino when the knob is varied. Can you please help me rectify these faults so that my circuit can function properly?

Below is my code and circuit diagram:

     /*Timers 1 and 2 are used to generate pulses for the the low side transistors.
*TCCR1A = pin9, TCCR1B = pin10, TCCR2A = pin11 and TCCR2B = pin3
*Timer 1 is set to 8-bit counting for synchronization with Timer 2 which is an 8-bit Timer
*phase correct PWM modes are used for all Timers (WGM settings) and in its non-inverted PWM mode set by the COM bits for each Timer. */



int step = 0;
int potentiometer = A3;  //The speed control is done using a potentiometer connected on pin A3
int motorspeed = 4000;
unsigned long beginMillis = 0;  //time counting using the microfunction

void setup() {
Serial.begin (250000);
  TCCR1A = 0;
  TCCR1B = 0;
  OCR1A = 77;// 30% duty cycle
  OCR1B = 77;
TCCR1A |= (1 << COM1A1) | (0 << COM1A0)  | (1 << COM1B1) | (0 << COM1B0) | (0 << WGM11) | (1 << WGM10);
  TCCR1B |= (0 << WGM13) | (0 << WGM12) | (1 << CS12) | (0 << CS11) | (1 << CS10); // 30.637 Hz frequency
  TCCR2A = 0;
  TCCR2B = 0;
  OCR2A = 77; // 30% duty cycle
  TCCR2A |= (1 << COM2A1) | (0 << COM2A0)  | (1 << COM2B1) | (0 << COM2B0) | (0 << WGM21) | (1 << WGM20);
  TCCR2B |= (0 << WGM22) | (1 << CS22) | (1 << CS21) | (1 << CS20); // 30.637 Hz frequency
  DDRB = DDRB | B00111111;
  PORTB = B00000000;

  pinMode(potentiometer,  INPUT);  //The potentiometer pin A3 is set as an input pin
  beginMillis = micros();  
}


void loop() {

  unsigned long currentMillis = micros();
  Serial.println(motorspeed);
    
  if(currentMillis - beginMillis >= motorspeed){
      beginMillis += motorspeed;
  
  // Rotor moves from Phase A to C'
  switch(step){
  case 1:
      step0();
        break;
 
  // Rotor moves from coil C' to B
  case 2:
      step1();
        break;
  
  // Rotor moves from coil B to A'
  case 3:
     step2();
        break;
  
  // Rotor moves from coil A' to C
  case 4:
   step3();
        break;
  
  // Rotor moves from coil C to B'
  case 5:
   step4();
        break;
  // Rotor moves from coil B' back to A
  case 6:
   step5();
        break;
  int t = analogRead(potentiometer); //reads values from the potentiometer as it is turned. 
motorspeed = map(t,0,1024,1,1000); //we set the time taken for the execution of each step function from 1 to 1000µs using the potentiometer

  if (step<6){
    step=step+1;
    }
  else{
    step=0;
  }
    
     }
  }
}

// HINA (pin13, Q1), LINA (pin11, Q2),     HINB (pin12, Q3), LINB (pin10, Q4),     HINC (pin8, Q5), LINC (pin9, Q6).

     void step0() {
        PORTB = B00100110; // turns on Q1 while pulsing Q4 and Q6 at 30.637 Hz frequency. All other Qs are off.
       }                  
      
      void step1() {
        PORTB = B00110010; // turns on Q1 and Q3 while pulsing Q6 at 30.637 Hz frequency. All other Qs are off.
      }

      void step2() {
        PORTB = B00011010; // turns on Q3 while pulsing Q2 and Q6 at 30.637 Hz frequency. All other Qs are off.
      }
      
      void step3() {
        PORTB = B00011001; // turns on Q3 and Q5 while pulsing Q2 at 30.637 Hz frequency. All other Qs are off.
      }
     
      void step4() {
        PORTB = B00001101; // turns on Q5 while pulsing Q2 and Q4 at 30.637 Hz frequency. All other Qs are off.
      }
      
      void step5() {
        PORTB = B00100101; // turns on Q1 and Q5 while pulsing Q4 at 30.637 Hz frequency. All other Qs are off.
      }

I would be really grateful if my problem can be solved soonest. Thanks.

The circuit

Presumably you have tried adding some Serial.print()s to see the value of pertinent variables at strategic places in the program. The value of step before the switch/case would be interesting don't you think ?

Incidentally, you don't actually need the switch/case as you could put the values for PORTB in an array and use the value of step as the index to them which would be much neater

I think you've got some code inside your switch case block that should be outside.

Yes, reading the pot where it is does look strange

I’m surprised that the compiler doesn’t warn you that code after the ‘break;’ in case 6: will never be executed. You probably intended that code to be after the closing bracket of the switch() statement, but that’s not what you wrote.

      case 6:
        step5();
        break;

        int t = analogRead(potentiometer); //reads values from the potentiometer as it is turned.
        motorspeed = map(t, 0, 1024, 1, 1000); //we set the time taken for the execution of each step function from 1 to 1000µs using the potentiometer

        if (step < 6)
        {
          step = step + 1;
        }
        else
        {
          step = 0;
        }
    }

UKHeliBob:
Presumably you have tried adding some Serial.print()s to see the value of pertinent variables at strategic places in the program. The value of step before the switch/case would be interesting don't you think ?

Incidentally, you don't actually need the switch/case as you could put the values for PORTB in an array and use the value of step as the index to them which would be much neater

Thanks for your reply. Could you kindly reorganise the code to eliminate the switch/case like you suggested? Maybe that could solve my problem. Am still a little new in arduino coding. Would really love to try out this suggestion.

GypsumFantastic:
I think you've got some code inside your switch case block that should be outside.

Thanks for your time. Will check that.

Could you kindly reorganise the code to eliminate the switch/case like you suggested?

Why not give it a try yourself ?

You need an array of 6 elements, each of them a value written by one of the stepX() functions, then you use the value of the step variable to write the corresponding value to PORTB

UKHeliBob:
Why not give it a try yourself ?

You need an array of 6 elements, each of them a value written by one of the stepX() functions, then you use the value of the step variable to write the corresponding value to PORTB

Would love to try. The technical language is just so unfamiliar, could you break this down as simple as possible?

Declare a global array like this

const byte dataArray[] = {0b00100110, 0b00110010, 0b00011010, 0b00011001, 0b00001101, 0b00100101};

Then when you have set the value of step do this instead of switch/case and calling functions

PORTB = dataArray[step];

UKHeliBob:
Declare a global array like this

const byte dataArray[] = {0b00100110, 0b00110010, 0b00011010, 0b00011001, 0b00001101, 0b00100101};

Then when you have set the value of step do this instead of switch/case and calling functions

PORTB = dataArray[step];

Thank you, thank you, thank you. After applying this new approach, I will update the outcome of the new code.

johnwasser:
I’m surprised that the compiler doesn’t warn you that code after the ‘break;’ in case 6: will never be executed. You probably intended that code to be after the closing bracket of the switch() statement, but that’s not what you wrote.

      case 6:

step5();
        break;

int t = analogRead(potentiometer); //reads values from the potentiometer as it is turned.
        motorspeed = map(t, 0, 1024, 1, 1000); //we set the time taken for the execution of each step function from 1 to 1000µs using the potentiometer

if (step < 6)
        {
          step = step + 1;
        }
        else
        {
          step = 0;
        }
    }

Thank you, John, for your deligent observation. I will definitely correct that and repost the result.

/*
   Timers 1 and 2 are used to generate pulses for the the low side transistors.
   TCCR1A = pin9, TCCR1B = pin10, TCCR2A = pin11 and TCCR2B = pin3
   Timer 1 is set to 8-bit counting for synchronization with Timer 2 which is an 8-bit Timer
   phase correct PWM modes are used for all Timers (WGM settings) and in its non-inverted PWM
   mode set by the COM bits for each Timer.
*/


const byte Steps[6] =
{
  B00100110, // turns on Q1 while pulsing Q4 and Q6 at 30.637 Hz frequency. All other Qs are off.
  B00110010, // turns on Q1 and Q3 while pulsing Q6 at 30.637 Hz frequency. All other Qs are off.
  B00011010, // turns on Q3 while pulsing Q2 and Q6 at 30.637 Hz frequency. All other Qs are off.
  B00011001, // turns on Q3 and Q5 while pulsing Q2 at 30.637 Hz frequency. All other Qs are off.
  B00001101, // turns on Q5 while pulsing Q2 and Q4 at 30.637 Hz frequency. All other Qs are off.
  B00100101  // turns on Q1 and Q5 while pulsing Q4 at 30.637 Hz frequency. All other Qs are off.
};


byte StepCount = 0;
const byte PotentiometerAIPin = A3;  //The speed control is done using a potentiometer connected on pin A3
unsigned motorspeed = 4000;
unsigned long beginMicros = 0;  //time counting using the microfunction


void setup()
{
  Serial.begin (250000);
  while (!Serial); // In case a version ever runs on a 32U4 processor (Leonardo, Micro, etc)
  
  TCCR1A = 0;
  TCCR1B = 0;
  OCR1A = 77;// 30% duty cycle
  OCR1B = 77;
  TCCR1A |= (1 << COM1A1) | (0 << COM1A0)  | (1 << COM1B1) | (0 << COM1B0) | (0 << WGM11) | (1 << WGM10);
  TCCR1B |= (0 << WGM13) | (0 << WGM12) | (1 << CS12) | (0 << CS11) | (1 << CS10); // 30.637 Hz frequency
  TCCR2A = 0;
  TCCR2B = 0;
  OCR2A = 77; // 30% duty cycle
  TCCR2A |= (1 << COM2A1) | (0 << COM2A0)  | (1 << COM2B1) | (0 << COM2B0) | (0 << WGM21) | (1 << WGM20);
  TCCR2B |= (0 << WGM22) | (1 << CS22) | (1 << CS21) | (1 << CS20); // 30.637 Hz frequency
  
  DDRB = DDRB | B00111111;
  PORTB = B00000000;


  // Don't use pinMode() on analog inputs
  // pinMode(PotentiometerAIPin,  INPUT);


  beginMicros = micros();
}




void loop()
{
  unsigned long currentMicros = micros();


  // Set the time between steps 1 to 1000µs using the potentiometer
  motorspeed = map(analogRead(PotentiometerAIPin), 0, 1024, 1, 1000);


  // Display the motor speed if it has been changed
  static unsigned oldMotorSpeed = 0;
  if (motorspeed != oldMotorSpeed)
  {
    oldMotorSpeed = motorspeed;
    Serial.println(motorspeed);
  }


  // Is it time for a step?
  if (currentMicros - beginMicros >= motorspeed)
  {
    beginMicros += motorspeed;  // Step taken


    // Rotor moves from Phase A to C'
    PORTB = Steps[StepCount];
    StepCount = (StepCount + 1) % 6;  // 0, 1, 2, 3, 4, 5, 0, 1, 2, ...
  }
}

johnwasser:

/*

Timers 1 and 2 are used to generate pulses for the the low side transistors.
  TCCR1A = pin9, TCCR1B = pin10, TCCR2A = pin11 and TCCR2B = pin3
  Timer 1 is set to 8-bit counting for synchronization with Timer 2 which is an 8-bit Timer
  phase correct PWM modes are used for all Timers (WGM settings) and in its non-inverted PWM
  mode set by the COM bits for each Timer.
*/

const byte Steps[6] =
{
  B00100110, // turns on Q1 while pulsing Q4 and Q6 at 30.637 Hz frequency. All other Qs are off.
  B00110010, // turns on Q1 and Q3 while pulsing Q6 at 30.637 Hz frequency. All other Qs are off.
  B00011010, // turns on Q3 while pulsing Q2 and Q6 at 30.637 Hz frequency. All other Qs are off.
  B00011001, // turns on Q3 and Q5 while pulsing Q2 at 30.637 Hz frequency. All other Qs are off.
  B00001101, // turns on Q5 while pulsing Q2 and Q4 at 30.637 Hz frequency. All other Qs are off.
  B00100101  // turns on Q1 and Q5 while pulsing Q4 at 30.637 Hz frequency. All other Qs are off.
};

byte StepCount = 0;
const byte PotentiometerAIPin = A3;  //The speed control is done using a potentiometer connected on pin A3
unsigned motorspeed = 4000;
unsigned long beginMicros = 0;  //time counting using the microfunction

void setup()
{
  Serial.begin (250000);
  while (!Serial); // In case a version ever runs on a 32U4 processor (Leonardo, Micro, etc)
 
  TCCR1A = 0;
  TCCR1B = 0;
  OCR1A = 77;// 30% duty cycle
  OCR1B = 77;
  TCCR1A |= (1 << COM1A1) | (0 << COM1A0)  | (1 << COM1B1) | (0 << COM1B0) | (0 << WGM11) | (1 << WGM10);
  TCCR1B |= (0 << WGM13) | (0 << WGM12) | (1 << CS12) | (0 << CS11) | (1 << CS10); // 30.637 Hz frequency
  TCCR2A = 0;
  TCCR2B = 0;
  OCR2A = 77; // 30% duty cycle
  TCCR2A |= (1 << COM2A1) | (0 << COM2A0)  | (1 << COM2B1) | (0 << COM2B0) | (0 << WGM21) | (1 << WGM20);
  TCCR2B |= (0 << WGM22) | (1 << CS22) | (1 << CS21) | (1 << CS20); // 30.637 Hz frequency
 
  DDRB = DDRB | B00111111;
  PORTB = B00000000;

// Don’t use pinMode() on analog inputs
  // pinMode(PotentiometerAIPin,  INPUT);

beginMicros = micros();
}

void loop()
{
  unsigned long currentMicros = micros();

// Set the time between steps 1 to 1000µs using the potentiometer
  motorspeed = map(analogRead(PotentiometerAIPin), 0, 1024, 1, 1000);

// Display the motor speed if it has been changed
  static unsigned oldMotorSpeed = 0;
  if (motorspeed != oldMotorSpeed)
  {
    oldMotorSpeed = motorspeed;
    Serial.println(motorspeed);
  }

// Is it time for a step?
  if (currentMicros - beginMicros >= motorspeed)
  {
    beginMicros += motorspeed;  // Step taken

// Rotor moves from Phase A to C’
    PORTB = Steps[StepCount];
    StepCount = (StepCount + 1) % 6;  // 0, 1, 2, 3, 4, 5, 0, 1, 2, …
  }
}

Hi johnwasser, you have been so helpful. Thank you. I have tried all the options suggested here, but the problems enountered with the earlier code still persist. I am beginning to think that the problem might have its origin from the setup loop section of the program.

What I intend is just for the arduino to run through six steps functions continuously with the potentiometer controlling the time spent by each step (between 1 and 1000 microseconds)

Here are the six step funtions:

B00100110, // turns on Q1 steady while pulsing Q4 and Q6 at 30.637 Hz frequency. All other Qs are off.
B00110010, // turns on Q1 and Q3 steady while pulsing Q6 at 30.637 Hz frequency. All other Qs are off.
B00011010, // turns on Q3 steady while pulsing Q2 and Q6 at 30.637 Hz frequency. All other Qs are off.
B00011001, // turns on Q3 and Q5 steady while pulsing Q2 at 30.637 Hz frequency. All other Qs are off.
B00001101, // turns on Q5 steady while pulsing Q2 and Q4 at 30.637 Hz frequency. All other Qs are off.
B00100101 // turns on Q1 and Q5 steady while pulsing Q4 at 30.637 Hz frequency. All other Qs are off.

Any chance you could, in your own way, convert all these English grammar into what the arduino will understand?

Turning the bit in the PORTB register ON or OFF is not sufficient to turn a PWM pin on or off. To do that you would change the bits in the TCCRxA register to set the OCRxA or OCRxB pin modes. Each pin has two bits. Set them to 0,0 for Normal (digitalRead()/digitalWrite()) mode. Set them to 1,0 for PWM.

It looks like the 8 bits of the PORTB register are assigned like this:
X X Q1 Q3 Q2 Q4 Q6 Q5

That maps to Arduino pin numbers like this:

const byte Q5Mask = 0b00000001;  // Arduino Pin 8
const byte Q6Mask = 0b00000010;  // Arduino Pin  9  (PWM on OCR1A)
const byte Q4Mask = 0b00000100;  // Arduino Pin 10 (PWM on OCR1B)
const byte Q2Mask = 0b00001000;  // Arduino Pin 11 (PWM on OCR2A)
const byte Q3Mask = 0000010000;  // Arduino Pin 12
const byte Q1Mask = 0b00100000;  // Arduino Pin 13

Now we have to add code to set the states of the PWM pins as well as setting the PORTB bits.

void SetPins(byte bitmask)
{
  // For the three PWM pins, enable PWM if the bit is 1, otherwise disable PWM on that pin

  // The COM1A0, COM1B0 and COM2A0 bits are always 0
  // Setting the COM1A1, COM1B1 or COM2A1 bits to 1 will set that pin to PWM
  // Setting the COM1A1, COM1B1 or COM2A1 bits to 0 will set that pin to Normal
  bitWrite(TCCR1A, COM1A1, (bitmask & Q2Mask));
  bitWrite(TCCR1A, COM1B1, (bitmask & Q4Mask));
  bitWrite(TCCR2A, COM2A1, (bitmask & Q6Mask));

  // Now write all of the bits to PORTB.  The Normal pins will be set to the state
  // in the bit: 0=LOW, 1=HIGH.  The pins set to PWM will override the PORTB values.
  PORTB = bitmask;
}

"Arduino Nano Ciruit not Executing Code as Programmed to Execute."

Despite what you may think, it is. :slight_smile:

But it probably is not programmed to execute what you want. :slight_smile:

...R

Thank you johnwasser for following me up with patience even in my ignorance. I am not willing to give up, not until my problem is solved. Please check what I drew up from your explanation and verify if the program is correct. Don't get pissed if am messing up things the more, am new to this.

/*
   Timers 1 and 2 are used to generate pulses for the the low side transistors.
   TCCR1A = pin9, TCCR1B = pin10, TCCR2A = pin11 and TCCR2B = pin3
   Timer 1 is set to 8-bit counting for synchronization with Timer 2 which is an 8-bit Timer
   phase correct PWM modes are used for all Timers (WGM settings) and in its non-inverted PWM
   mode set by the COM bits for each Timer.
*/

const byte Q5Mask = 0b00000001;  // Arduino Pin 8
const byte Q6Mask = 0b00000010;  // Arduino Pin  9  (PWM on OCR1A)
const byte Q4Mask = 0b00000100;  // Arduino Pin 10 (PWM on OCR1B)
const byte Q2Mask = 0b00001000;  // Arduino Pin 11 (PWM on OCR2A)
const byte Q3Mask = 0000010000;  // Arduino Pin 12
const byte Q1Mask = 0b00100000;  // Arduino Pin 13




void SetPins(byte bitmask)
{
  // For the three PWM pins, enable PWM if the bit is 1, otherwise disable PWM on that pin

  // The COM1A0, COM1B0 and COM2A0 bits are always 0
  // Setting the COM1A1, COM1B1 or COM2A1 bits to 1 will set that pin to PWM
  // Setting the COM1A1, COM1B1 or COM2A1 bits to 0 will set that pin to Normal
  bitWrite(TCCR1A, COM1A1, (bitmask & Q2Mask));
  bitWrite(TCCR1A, COM1B1, (bitmask & Q4Mask));
  bitWrite(TCCR2A, COM2A1, (bitmask & Q6Mask));

  // Now write all of the bits to PORTB.  The Normal pins will be set to the state
  // in the bit: 0=LOW, 1=HIGH.  The pins set to PWM will override the PORTB values.
  PORTB = bitmask;

  const byte Steps[6] =
{
  B00100110, // turns on Q1 while pulsing Q4 and Q6 at 30.637 Hz frequency. All other Qs are off.
  B00110010, // turns on Q1 and Q3 while pulsing Q6 at 30.637 Hz frequency. All other Qs are off.
  B00011010, // turns on Q3 while pulsing Q2 and Q6 at 30.637 Hz frequency. All other Qs are off.
  B00011001, // turns on Q3 and Q5 while pulsing Q2 at 30.637 Hz frequency. All other Qs are off.
  B00001101, // turns on Q5 while pulsing Q2 and Q4 at 30.637 Hz frequency. All other Qs are off.
  B00100101  // turns on Q1 and Q5 while pulsing Q4 at 30.637 Hz frequency. All other Qs are off.
};


byte StepCount = 0;
const byte PotentiometerAIPin = A3;  //The speed control is done using a potentiometer connected on pin A3
unsigned motorspeed = 4000;
unsigned long beginMicros = 0;  //time counting using the microfunction

}



void setup()
{
  Serial.begin (250000);
  while (!Serial); // In case a version ever runs on a 32U4 processor (Leonardo, Micro, etc)
  
  beginMicros = micros();
}




void loop()
{
  unsigned long currentMicros = micros();


  // Set the time between steps 1 to 1000µs using the potentiometer
  motorspeed = map(analogRead(PotentiometerAIPin), 0, 1024, 1, 1000);


  // Display the motor speed if it has been changed
  static unsigned oldMotorSpeed = 0;
  if (motorspeed != oldMotorSpeed)
  {
    oldMotorSpeed = motorspeed;
    Serial.println(motorspeed);
  }


  // Is it time for a step?
  if (currentMicros - beginMicros >= motorspeed)
  {
    beginMicros += motorspeed;  // Step taken


    // Rotor moves from Phase A to C'
    PORTB = Steps[StepCount];
    StepCount = (StepCount + 1) % 6;  // 0, 1, 2, 3, 4, 5, 0, 1, 2, ...
  }

}

ogahpaul:
Thank you johnwasser for following me up with patience even in my ignorance. I am not willing to give up, not until my problem is solved. Please check what I drew up from your explanation and verify if the program is correct. Don’t get pissed if am messing up things the more, am new to this.

/*

Timers 1 and 2 are used to generate pulses for the the low side transistors.
  TCCR1A = pin9, TCCR1B = pin10, TCCR2A = pin11 and TCCR2B = pin3
  Timer 1 is set to 8-bit counting for synchronization with Timer 2 which is an 8-bit Timer
  phase correct PWM modes are used for all Timers (WGM settings) and in its non-inverted PWM
  mode set by the COM bits for each Timer.
*/

const byte Q5Mask = 0b00000001;  // Arduino Pin 8
const byte Q6Mask = 0b00000010;  // Arduino Pin  9  (PWM on OCR1A)
const byte Q4Mask = 0b00000100;  // Arduino Pin 10 (PWM on OCR1B)
const byte Q2Mask = 0b00001000;  // Arduino Pin 11 (PWM on OCR2A)
const byte Q3Mask = 0000010000;  // Arduino Pin 12
const byte Q1Mask = 0b00100000;  // Arduino Pin 13

void SetPins(byte bitmask)
{
 // For the three PWM pins, enable PWM if the bit is 1, otherwise disable PWM on that pin

// The COM1A0, COM1B0 and COM2A0 bits are always 0
 // Setting the COM1A1, COM1B1 or COM2A1 bits to 1 will set that pin to PWM
 // Setting the COM1A1, COM1B1 or COM2A1 bits to 0 will set that pin to Normal
 bitWrite(TCCR1A, COM1A1, (bitmask & Q2Mask));
 bitWrite(TCCR1A, COM1B1, (bitmask & Q4Mask));
 bitWrite(TCCR2A, COM2A1, (bitmask & Q6Mask));

// Now write all of the bits to PORTB.  The Normal pins will be set to the state
 // in the bit: 0=LOW, 1=HIGH.  The pins set to PWM will override the PORTB values.
 PORTB = bitmask;

const byte Steps[6] =
{
 B00100110, // turns on Q1 while pulsing Q4 and Q6 at 30.637 Hz frequency. All other Qs are off.
 B00110010, // turns on Q1 and Q3 while pulsing Q6 at 30.637 Hz frequency. All other Qs are off.
 B00011010, // turns on Q3 while pulsing Q2 and Q6 at 30.637 Hz frequency. All other Qs are off.
 B00011001, // turns on Q3 and Q5 while pulsing Q2 at 30.637 Hz frequency. All other Qs are off.
 B00001101, // turns on Q5 while pulsing Q2 and Q4 at 30.637 Hz frequency. All other Qs are off.
 B00100101  // turns on Q1 and Q5 while pulsing Q4 at 30.637 Hz frequency. All other Qs are off.
};

byte StepCount = 0;
const byte PotentiometerAIPin = A3;  //The speed control is done using a potentiometer connected on pin A3
unsigned motorspeed = 4000;
unsigned long beginMicros = 0;  //time counting using the microfunction

}

void setup()
{
 Serial.begin (250000);
 while (!Serial); // In case a version ever runs on a 32U4 processor (Leonardo, Micro, etc)
 
 beginMicros = micros();
}

void loop()
{
 unsigned long currentMicros = micros();

// Set the time between steps 1 to 1000µs using the potentiometer
 motorspeed = map(analogRead(PotentiometerAIPin), 0, 1024, 1, 1000);

// Display the motor speed if it has been changed
 static unsigned oldMotorSpeed = 0;
 if (motorspeed != oldMotorSpeed)
 {
   oldMotorSpeed = motorspeed;
   Serial.println(motorspeed);
 }

// Is it time for a step?
 if (currentMicros - beginMicros >= motorspeed)
 {
   beginMicros += motorspeed;  // Step taken

// Rotor moves from Phase A to C’
   PORTB = Steps[StepCount];
   StepCount = (StepCount + 1) % 6;  // 0, 1, 2, 3, 4, 5, 0, 1, 2, …
 }

}

The attached document might give a little more insight to what the controls should look like.

motor controller switching summary.pdf (111 KB)

    PORTB = Steps[StepCount];

Replace “PORTB =“ with a call to SetPins().

Also, I think I got the use of Q2Mask and Q6Mask switched.