Problem with cycling though outputs on timer, and (related?) Serial output problem, ESP32 (Arduino)

Thread Starter

LLofYorkshire

Joined Jan 15, 2020
21
I'm getting some strange behaviour with this, that maybe somebody could help with please?
The purpose of the code is to cycle sequentially though 11 different configurations, with each configuration (I call it a crank position) calling for 3 out of 11 PWM channels to be activated. A different 3 channels for each crank position. I don't have much experience of using timers, or coding in general.

The behavior I am trying for is that the crank position should cycle through positions every 50ms then when it gets to position 11 should go back to position 1, so a cycle time of 550ms. The problem is that I am not getting PWM on the last 3 channels (8,9,10), so not reaching Crank Pos 7.

I checked things in general before introducing the Crank feature by just pulsing each channel simultaneously and attaching to LEDs and that worked fine, so I now I'm connected to pins that can be PWM'd. I don't have a scope unfortunately.


I experimented with changing the "if (gCrankPos >= 11) gCrankPos = 0;" down from 11, and it does behave to fire less of the channels, but increasing has no effect.

Debugging is proving tricky also because the serial monitor doesn't seem to be working properly for some reason. I don't know if related or not, but it works no probs with simple sketches. Baud rates math but output looks similar to when they don't. Output sample:
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode: DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:808
load:0x40078000,len:6084
load:0x40080000,len:6696
entry 0x400802e4
The ààààààààààààààààà

Then it just repeats that last character forever.


The Code:
C:
// test5 is for 11 channel open loop speed control with crank duration to set speed.  no (or untested) termistor and current sense


// initiallise the timer conditions
hw_timer_t * timer = NULL;
hw_timer_t * timer2 = NULL;
volatile bool gGetADCflag;
volatile bool gCrankPosChangeflag;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;

// PWMs are for 11 channel individual Mosfets

// setting things up for PWM out - use first channel of 16 channels (started from zero)
#define LEDC_CHANNEL_0     0
#define LEDC_CHANNEL_1     1
#define LEDC_CHANNEL_2     2
#define LEDC_CHANNEL_3     3
#define LEDC_CHANNEL_4     4
#define LEDC_CHANNEL_5     5
#define LEDC_CHANNEL_6     6
#define LEDC_CHANNEL_7     7
#define LEDC_CHANNEL_8     8
#define LEDC_CHANNEL_9     9
#define LEDC_CHANNEL_10    10



// use 13 bit precission for LEDC timer
#define LEDC_TIMER_13_BIT  13

// use 5000 Hz as a LEDC base frequency
#define LEDC_BASE_FREQ     25000  // max frequency for IBT-2 is 25kHz

// fade LED PIN (replace with LED_BUILTIN constant for built-in LED)
#define PWM1               21
#define PWM2               1
#define PWM3               3
#define PWM4               22
#define PWM5               19
#define PWM6               23
#define PWM7               18
#define PWM8               5
#define PWM9               15
#define PWM10              25
#define PWM11              33




unsigned long durationNow = 0;
unsigned long previous = 0;


int PWMVal = 120;    // 0-255
int analog_value = 0;   // 0-4095

int outputValue = 0;        // value output to the PWM (analog out)
int rateOfChange = 0;   // Capture the rate of change of the input at each time step (in theory could be a deimal but if <1 then 0 is fine)
int rateLimit = 50;         // ADC increments
int oldDemand = 0;
int newDemand = 0;
int mAmp = 0;



String gcommand;
int gDuty=0;
int gCrankPos = 1; //  initialisation of the crank position (1-11 variable)

// timer related functions
void ADCTimer(){
  gGetADCflag = true;
}

void startADCTimer() {
  timer = timerBegin(0, 80, true); // timer_id = 0; divider=80; countUp = true;
  timerAttachInterrupt(timer, &ADCTimer, true); // edge = true
  timerAlarmWrite(timer, 1000, true);  //1 ms
  timerAlarmEnable(timer);
}

void endADCTimer() {
  timerEnd(timer);
  timer = NULL;
}

void crankTimer(){
  gCrankPosChangeflag = true;
}

void startCrankTimer() {
  timer2 = timerBegin(1, 80, true); // timer_id = 1; divider=80; countUp = true;
  timerAttachInterrupt(timer2, &crankTimer, true); // edge = true
  timerAlarmWrite(timer2, 50000, true);  //50 ms (0.5s)
  timerAlarmEnable(timer2);
}

void endCrankTimer() {
  timerEnd(timer2);
  timer2 = NULL;
}

void incCrankPos(){
  // anything in here that involves shifting around the 11 channel postions
  Serial.print("Crank Pos:  ");
  Serial.println(gCrankPos);
  if (gCrankPos >= 11) gCrankPos = 0;
  gCrankPos++; //
  gCrankPosChangeflag = false;
}

/*
// the function that get's Called once the Timer Interrupt flag is True
void getADC() {
  // do suff in here that runs outside of the ISR but is triggered by the ISR
  // the timer should be set so that it triggers 10x faster than the data tare you want to see, as it will be averaged then thrown away every 10 cycles
// Note that this simplified approach will introduce a phase shift or delay to the figures, as the average figure is being reported against the final sample timestamp.  a rolling average should be used if needs to be avoided
 
  // any variables that need to not be reset each time the function is called need to be declared as static
  static unsigned int counter = 1;
  static int rawInput[NOOFAVERAGES] = {0}; // array for storing the 10 values
  static int aveADC = 0; // variable for storing the potentiometer value
  static int sumInput = 0; // variable to hold the sum of inputs prior to averaging
  static int current1 = 0; // convert ADC counts to current mA, with offset and multiplier
  static int offset1 = -210; // ADC offset for 1st ACS725 sensor
 
 
  rawInput[counter-1] = analogRead(ANALOG_PIN_1);
  sumInput = sumInput + rawInput[counter-1]; 
// take the NOOFAVERAGES readings from the Array and average them
  if (counter == NOOFAVERAGES){
   
    aveADC = sumInput/NOOFAVERAGES; // add up and divide by 10
    current1 = (aveADC+offset1)*1000/300;  // Convert ADC to mA, 264mV/A - chaged to 300mV/A
   
    Serial.print("ADCTimer ");
    Serial.print(counter);
    Serial.print(" at ");
    Serial.print(millis());
    Serial.print(" ms ");
    Serial.print(" ADC value ");
    Serial.print(aveADC);
    Serial.print(" Current(mA) ");
    Serial.println(current1);
    sumInput = 0;
    counter = 0;
   }

  gGetADCflag = false;
  counter++;
 
}
*/

// Arduino like analogWrite
// value has to be between 0 and valueMax

void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
  // calculate duty, 8191 from 2 ^ 13 - 1
  uint32_t duty = (8191 / valueMax) * min(value, valueMax);

  // write duty to LEDC
  ledcWrite(channel, duty);
}

void setup() {
  delay(1000);
  Serial.begin(115200);
  delay(1000);
//  SerialBT.begin("ESP32hula"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  // Setup timer and attach timer to a pwm pin
  ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  ledcSetup(LEDC_CHANNEL_1, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  ledcSetup(LEDC_CHANNEL_2, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  ledcSetup(LEDC_CHANNEL_3, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  ledcSetup(LEDC_CHANNEL_4, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  ledcSetup(LEDC_CHANNEL_5, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  ledcSetup(LEDC_CHANNEL_6, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  ledcSetup(LEDC_CHANNEL_7, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  ledcSetup(LEDC_CHANNEL_8, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  ledcSetup(LEDC_CHANNEL_9, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  ledcSetup(LEDC_CHANNEL_10, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  ledcAttachPin(PWM1, LEDC_CHANNEL_0);
  ledcAttachPin(PWM2, LEDC_CHANNEL_1);
  ledcAttachPin(PWM3, LEDC_CHANNEL_2);
  ledcAttachPin(PWM4, LEDC_CHANNEL_3);
  ledcAttachPin(PWM5, LEDC_CHANNEL_4);
  ledcAttachPin(PWM6, LEDC_CHANNEL_5);
  ledcAttachPin(PWM7, LEDC_CHANNEL_6);
  ledcAttachPin(PWM8, LEDC_CHANNEL_7);
  ledcAttachPin(PWM9, LEDC_CHANNEL_8);
  ledcAttachPin(PWM10, LEDC_CHANNEL_9);
  ledcAttachPin(PWM11, LEDC_CHANNEL_10);
//  pinMode(ANALOG_PIN_0, INPUT);
//  pinMode(ANALOG_PIN_1, INPUT);
//  analogSetPinAttenuation(ANALOG_PIN_1, ADC_11db);  // for ESP32 max vooltages are 0db = 1.1v, 2.5db = 1.4v, 6db = 1.9v, 11db = 3.2v, representing 4095 ADC (12bit)

  Serial.println("pin modes set");
  startADCTimer();
  startCrankTimer();
  delay(1000);
  Serial.println("exiting setup");
}

/*
void parseCommand (String com) {
  String part1;
  String part2;
 
  // split into 2 parts.  1st part will be a letter, either an override status or a motor speed change.  2nd part will be either override on/off or motor speed 0-100.
  part1 = com.substring(0,1);
  part2 = com.substring(1,com.indexOf("."));
//  Serial.print("part1= ");
//  Serial.println(part1);
//  Serial.print("part2= ");
//  Serial.println(part2); 
  if (part1.equalsIgnoreCase("o")){
    gOverrideStatus = part2.toInt();  // change the global override status to either 1 or 0;
//    Serial.print("gOverride status changed to ");
//    Serial.println(gOverrideStatus);
  }
  else if(part1.equalsIgnoreCase("v")){
    if (gOverrideStatus == 1){
      gDuty = part2.toInt();
//      Serial.print("duty (%) changed to ");
//      Serial.println(gDuty);
    }
  }
  else{
    part1="";
    part2="";
    gcommand="";
  }
 
}
*/
/*
int rateLimiter(int latestValue){
    durationNow = millis();
  // check the input value:
  int sensorValue = latestValue;
  newDemand = sensorValue;
  rateOfChange = (newDemand-oldDemand)/(durationNow-previous);

  if (rateOfChange > rateLimit)
  {
    if (newDemand>oldDemand)
    {
      newDemand = oldDemand+rateLimit;
    }
    if (newDemand<=oldDemand)
    {
      newDemand = oldDemand-rateLimit;
    }
  }

  else
  {
    newDemand = newDemand;
  }
  //reset for next loop
  oldDemand = newDemand;
  previous = durationNow;
  return newDemand;
}
*/

void loop() {



  // Reading ADC value (current sense input)
  if (gGetADCflag == true) {
  //  Serial.println("ADCFlag = TRUE");
  //  getADC();
  }

  if (gCrankPosChangeflag == true) {
    incCrankPos();
  }


// check motor speed is within acceptable boundaries and limit
  if (PWMVal <= 0){
    PWMVal = 0;
  }
  if (PWMVal >120){
    PWMVal = 120;  // set to be approx 2A max (true maximum 255, but would result in peak current draw)
  }
    
  // call PWM funtions for each coil
  // use switch cases depeding on crank position
  switch (gCrankPos) {
  case 1:
    ledcAnalogWrite(LEDC_CHANNEL_0, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_1, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_2, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_3, 0);
    ledcAnalogWrite(LEDC_CHANNEL_4, 0);
    ledcAnalogWrite(LEDC_CHANNEL_5, 0);
    ledcAnalogWrite(LEDC_CHANNEL_6, 0);
    ledcAnalogWrite(LEDC_CHANNEL_7, 0);
    ledcAnalogWrite(LEDC_CHANNEL_8, 0);
    ledcAnalogWrite(LEDC_CHANNEL_9, 0);
    ledcAnalogWrite(LEDC_CHANNEL_10, 0);
    break;
  case 2:
    ledcAnalogWrite(LEDC_CHANNEL_0, 0);
    ledcAnalogWrite(LEDC_CHANNEL_1, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_2, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_3, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_4, 0);
    ledcAnalogWrite(LEDC_CHANNEL_5, 0);
    ledcAnalogWrite(LEDC_CHANNEL_6, 0);
    ledcAnalogWrite(LEDC_CHANNEL_7, 0);
    ledcAnalogWrite(LEDC_CHANNEL_8, 0);
    ledcAnalogWrite(LEDC_CHANNEL_9, 0);
    ledcAnalogWrite(LEDC_CHANNEL_10, 0);
    break;
  case 3:
    ledcAnalogWrite(LEDC_CHANNEL_0, 0);
    ledcAnalogWrite(LEDC_CHANNEL_1, 0);
    ledcAnalogWrite(LEDC_CHANNEL_2, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_3, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_4, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_5, 0);
    ledcAnalogWrite(LEDC_CHANNEL_6, 0);
    ledcAnalogWrite(LEDC_CHANNEL_7, 0);
    ledcAnalogWrite(LEDC_CHANNEL_8, 0);
    ledcAnalogWrite(LEDC_CHANNEL_9, 0);
    ledcAnalogWrite(LEDC_CHANNEL_10, 0);
    break;
  case 4:
    ledcAnalogWrite(LEDC_CHANNEL_0, 0);
    ledcAnalogWrite(LEDC_CHANNEL_1, 0);
    ledcAnalogWrite(LEDC_CHANNEL_2, 0);
    ledcAnalogWrite(LEDC_CHANNEL_3, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_4, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_5, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_6, 0);
    ledcAnalogWrite(LEDC_CHANNEL_7, 0);
    ledcAnalogWrite(LEDC_CHANNEL_8, 0);
    ledcAnalogWrite(LEDC_CHANNEL_9, 0);
    ledcAnalogWrite(LEDC_CHANNEL_10, 0);
    break;
  case 5:
    ledcAnalogWrite(LEDC_CHANNEL_0, 0);
    ledcAnalogWrite(LEDC_CHANNEL_1, 0);
    ledcAnalogWrite(LEDC_CHANNEL_2, 0);
    ledcAnalogWrite(LEDC_CHANNEL_3, 0);
    ledcAnalogWrite(LEDC_CHANNEL_4, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_5, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_6, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_7, 0);
    ledcAnalogWrite(LEDC_CHANNEL_8, 0);
    ledcAnalogWrite(LEDC_CHANNEL_9, 0);
    ledcAnalogWrite(LEDC_CHANNEL_10, 0);
    break;
  case 6:
    ledcAnalogWrite(LEDC_CHANNEL_0, 0);
    ledcAnalogWrite(LEDC_CHANNEL_1, 0);
    ledcAnalogWrite(LEDC_CHANNEL_2, 0);
    ledcAnalogWrite(LEDC_CHANNEL_3, 0);
    ledcAnalogWrite(LEDC_CHANNEL_4, 0);
    ledcAnalogWrite(LEDC_CHANNEL_5, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_6, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_7, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_8, 0);
    ledcAnalogWrite(LEDC_CHANNEL_9, 0);
    ledcAnalogWrite(LEDC_CHANNEL_10, 0);
    break;
  case 7:
    ledcAnalogWrite(LEDC_CHANNEL_0, 0);
    ledcAnalogWrite(LEDC_CHANNEL_1, 0);
    ledcAnalogWrite(LEDC_CHANNEL_2, 0);
    ledcAnalogWrite(LEDC_CHANNEL_3, 0);
    ledcAnalogWrite(LEDC_CHANNEL_4, 0);
    ledcAnalogWrite(LEDC_CHANNEL_5, 0);
    ledcAnalogWrite(LEDC_CHANNEL_6, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_7, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_8, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_9, 0);
    ledcAnalogWrite(LEDC_CHANNEL_10, 0);
    break;
  case 8:
    ledcAnalogWrite(LEDC_CHANNEL_0, 0);
    ledcAnalogWrite(LEDC_CHANNEL_1, 0);
    ledcAnalogWrite(LEDC_CHANNEL_2, 0);
    ledcAnalogWrite(LEDC_CHANNEL_3, 0);
    ledcAnalogWrite(LEDC_CHANNEL_4, 0);
    ledcAnalogWrite(LEDC_CHANNEL_5, 0);
    ledcAnalogWrite(LEDC_CHANNEL_6, 0);
    ledcAnalogWrite(LEDC_CHANNEL_7, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_8, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_9, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_10, 0);
    break;
  case 9:
    ledcAnalogWrite(LEDC_CHANNEL_0, 0);
    ledcAnalogWrite(LEDC_CHANNEL_1, 0);
    ledcAnalogWrite(LEDC_CHANNEL_2, 0);
    ledcAnalogWrite(LEDC_CHANNEL_3, 0);
    ledcAnalogWrite(LEDC_CHANNEL_4, 0);
    ledcAnalogWrite(LEDC_CHANNEL_5, 0);
    ledcAnalogWrite(LEDC_CHANNEL_6, 0);
    ledcAnalogWrite(LEDC_CHANNEL_7, 0);
    ledcAnalogWrite(LEDC_CHANNEL_8, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_9, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_10, PWMVal);
    break;
  case 10:
    ledcAnalogWrite(LEDC_CHANNEL_0, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_1, 0);
    ledcAnalogWrite(LEDC_CHANNEL_2, 0);
    ledcAnalogWrite(LEDC_CHANNEL_3, 0);
    ledcAnalogWrite(LEDC_CHANNEL_4, 0);
    ledcAnalogWrite(LEDC_CHANNEL_5, 0);
    ledcAnalogWrite(LEDC_CHANNEL_6, 0);
    ledcAnalogWrite(LEDC_CHANNEL_7, 0);
    ledcAnalogWrite(LEDC_CHANNEL_8, 0);
    ledcAnalogWrite(LEDC_CHANNEL_9, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_10, PWMVal);
    break;
  case 11:
    ledcAnalogWrite(LEDC_CHANNEL_0, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_1, PWMVal);
    ledcAnalogWrite(LEDC_CHANNEL_2, 0);
    ledcAnalogWrite(LEDC_CHANNEL_3, 0);
    ledcAnalogWrite(LEDC_CHANNEL_4, 0);
    ledcAnalogWrite(LEDC_CHANNEL_5, 0);
    ledcAnalogWrite(LEDC_CHANNEL_6, 0);
    ledcAnalogWrite(LEDC_CHANNEL_7, 0);
    ledcAnalogWrite(LEDC_CHANNEL_8, 0);
    ledcAnalogWrite(LEDC_CHANNEL_9, 0);
    ledcAnalogWrite(LEDC_CHANNEL_10, PWMVal);
    break;

  default:
    // if nothing else matches, do the default
    ledcAnalogWrite(LEDC_CHANNEL_0, 0);
    ledcAnalogWrite(LEDC_CHANNEL_1, 0);
    ledcAnalogWrite(LEDC_CHANNEL_2, 0);
    ledcAnalogWrite(LEDC_CHANNEL_3, 0);
    ledcAnalogWrite(LEDC_CHANNEL_4, 0);
    ledcAnalogWrite(LEDC_CHANNEL_5, 0);
    ledcAnalogWrite(LEDC_CHANNEL_6, 0);
    ledcAnalogWrite(LEDC_CHANNEL_7, 0);
    ledcAnalogWrite(LEDC_CHANNEL_8, 0);
    ledcAnalogWrite(LEDC_CHANNEL_9, 0);
    ledcAnalogWrite(LEDC_CHANNEL_10, 0);
    break;
  }
}
 
Last edited by a moderator:

ErnieM

Joined Apr 24, 2011
8,018
I used the ESP32 in a project last year with no serious issues. My ESP32 lives on a DOIT DevKit V1 board which provides a 30 pin base with most signals you will use, excepting the mini USB for programming and debug print info.

While I have not found a way to step thru running code I do use the serial print capability for sanity debugging checks.

My project was a 4 channel RGBW controller (works with Alexa!) that use all 16 PWM modules so each of 4 PWM drivers for each channel can be individually controlled.

Development went well without any serial monitor issues. All 16 PWMs can be controlled just fine. I use these GPIO pins:

4,12,13,14,15,17,18,19,21,22,23,25,26,27,3,33

I do not use GPIO 1 and 3 as you do; I do not use these as they drive the serial to USB converter. That may be one of your issues.


I always suggest building parts of the code for a larger project. Test each section of the PWM code till it works and can be inserted into the larger project.
 

ErnieM

Joined Apr 24, 2011
8,018
sagor: while I would not write incCrankPos () that way it will run from 1 to 11 and wrap back to 1, and the select case has 1 to 11 cases.

It does debug print the previous value which is not very helpful.
 

sagor

Joined Mar 10, 2019
152
Is it possible the 50ms timer is running twice within the main loop? That would reset the change flag to True, causing a second increment.
I see no code that turns off the timer.
Just a thought....
 

Thread Starter

LLofYorkshire

Joined Jan 15, 2020
21
thanks guys. I'll sort out the serial console issue first by temporarily trying different pins and hopefully I an tell then what's going on with the timer/case selection side
 

Thread Starter

LLofYorkshire

Joined Jan 15, 2020
21
Serial comms working now that I switched the pins. Checked to make sure the different Case's are being entered and they are. I also reflashed some ode that just pulsed LEDS on all channels simultaneously, just to make sure the hardware was still working fine and it was, so no probs there.
Am out of ideas at the moment, so any ideas for things to try is appreciated.
 

Thread Starter

LLofYorkshire

Joined Jan 15, 2020
21
A follow up to close this.

I still don't know exactly what was causing this issue, but I suspect it is something to do with the Arduino wrapper and the implementation of the 8 "Low Speed" PWM channels, as I was only getting problems on those channels and only when I was also using a separate timer interrupt to change the PWM duty.
I got around the problem by using some IDF code to control the PWMs more closely. The code is more unwieldy but I get the result I need. For reference:

Code:
#include "driver/ledc.h"

//https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-ledc.c#L240
/*
 * LEDC Chan to Group/Channel/Timer Mapping
** ledc: 0  => Group: 0, Channel: 0, Timer: 0
** ledc: 1  => Group: 0, Channel: 1, Timer: 0
** ledc: 2  => Group: 0, Channel: 2, Timer: 1
** ledc: 3  => Group: 0, Channel: 3, Timer: 1
** ledc: 4  => Group: 0, Channel: 4, Timer: 2
** ledc: 5  => Group: 0, Channel: 5, Timer: 2
** ledc: 6  => Group: 0, Channel: 6, Timer: 3
** ledc: 7  => Group: 0, Channel: 7, Timer: 3
** ledc: 8  => Group: 1, Channel: 0, Timer: 0
** ledc: 9  => Group: 1, Channel: 1, Timer: 0
** ledc: 10 => Group: 1, Channel: 2, Timer: 1
** ledc: 11 => Group: 1, Channel: 3, Timer: 1
** ledc: 12 => Group: 1, Channel: 4, Timer: 2
** ledc: 13 => Group: 1, Channel: 5, Timer: 2
** ledc: 14 => Group: 1, Channel: 6, Timer: 3
** ledc: 15 => Group: 1, Channel: 7, Timer: 3
*/

#define PWM1               21
#define PWM2               32 // was 1
#define PWM3               4  // was 3
#define PWM4               22
#define PWM5               19
#define PWM6               23
#define PWM7               18
#define PWM8               5
#define PWM9               15
#define PWM10              25
#define PWM11              33

// initiallise the timer conditions
hw_timer_t * timer2 = NULL;

volatile bool gCrankPosChangeflag;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
int gCrankPos = 1; //  initialisation of the crank position (1-11 variable)



int brightness = 4;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// Arduino like analogWrite
// value has to be between 0 and valueMax
void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
  // calculate duty, 8191 from 2 ^ 13 - 1
  uint32_t duty = (8191 / valueMax) * min(value, valueMax);

  // write duty to LEDC
  ledcWrite(channel, duty);
}

void crankTimer(){
  gCrankPosChangeflag = true;
}

void startCrankTimer() {
  timer2 = timerBegin(1, 80, true); // timer_id = 1; divider=80; countUp = true;
  timerAttachInterrupt(timer2, &crankTimer, true); // edge = true
  timerAlarmWrite(timer2, 1000000, true);  //500 ms (0.5s)
  timerAlarmEnable(timer2);
}

void endCrankTimer() {
  timerEnd(timer2);
  timer2 = NULL; 
}

void incCrankPos(){
  // anything in here that involves shifting around the 11 channel postions
  
  if (gCrankPos > 11) gCrankPos = 1; 
  Serial.print("Crank Pos:  ");
  Serial.println(gCrankPos);
     
  // call PWM funtions for each coil
  // use switch cases depeding on crank position
  if (gCrankPos==1) {
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );
    Serial.println("in Case1");
  }
  else if (gCrankPos==2) {
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );    
    Serial.println("in Case2");
  }
  else if (gCrankPos==3) {
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );    
    Serial.println("in Case3");
  }
  else if (gCrankPos==4) {
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );     
    Serial.println("in Case4");
  }
  else if (gCrankPos==5) {
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );         
    Serial.println("in Case5");
  }
  else if (gCrankPos==6) {
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0) );      
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );     
    Serial.println("in Case6");
  }
  else if (gCrankPos==7) {
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0) );      
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );         
    Serial.println("in Case7");
  }
  else if (gCrankPos==8) {
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0) );      
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );           
    Serial.println("in Case8");
  }
  else if (gCrankPos==9) {
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );         
    Serial.println("in Case9");
  }
  else if (gCrankPos==10) {
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );
                     
    Serial.println("in Case10");
  }
  else if (gCrankPos==11) {
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 4) );      
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 4) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );    
    Serial.println("in Case11");
  }
  else {
    
    // if nothing else matches, do the default
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_5) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_6) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_7) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1) );
      ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0) );
      ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2) );        
    Serial.println("in Case default");
  }
  
  

  gCrankPos++; //
  gCrankPosChangeflag = false;
}

void setup() {
  Serial.begin(115200);

  // High Speed LEDTimer0 setup
  ledc_timer_config_t ledc_timer0;
  ledc_timer0.speed_mode = LEDC_HIGH_SPEED_MODE;           // timer mode
  ledc_timer0.duty_resolution = LEDC_TIMER_3_BIT; // resolution of PWM duty
  ledc_timer0.timer_num = LEDC_TIMER_0;            // timer index
  ledc_timer0.freq_hz = 20000;                      // frequency of PWM signal
  // Set configuration of timer0 for high speed channels
  esp_err_t result = ledc_timer_config(&ledc_timer0);
  if (result == ESP_OK)
     Serial.printf("timer0 frequency: %d", ledc_get_freq(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_0));

 
// High Speed LEDTimer1 setup
  ledc_timer_config_t ledc_timer1;
  ledc_timer1.speed_mode = LEDC_HIGH_SPEED_MODE;           // timer mode
  ledc_timer1.duty_resolution = LEDC_TIMER_3_BIT; // resolution of PWM duty
  ledc_timer1.timer_num = LEDC_TIMER_1;            // timer index
  ledc_timer1.freq_hz = 20000;                      // frequency of PWM signal
  // Set configuration of timer2 for high speed channels
  esp_err_t result1 = ledc_timer_config(&ledc_timer1);
  if (result1 == ESP_OK)
     Serial.printf("timer2 frequency: %d", ledc_get_freq(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_1));

// High Speed LEDTimer2 setup
  ledc_timer_config_t ledc_timer2;
  ledc_timer2.speed_mode = LEDC_HIGH_SPEED_MODE;           // timer mode
  ledc_timer2.duty_resolution = LEDC_TIMER_3_BIT; // resolution of PWM duty
  ledc_timer2.timer_num = LEDC_TIMER_2;            // timer index
  ledc_timer2.freq_hz = 20000;                      // frequency of PWM signal
  // Set configuration of timer3 for high speed channels
  esp_err_t result2 = ledc_timer_config(&ledc_timer2);
  if (result2 == ESP_OK)
     Serial.printf("timer3 frequency: %d", ledc_get_freq(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_2));


// High Speed LEDTimer3 setup
  ledc_timer_config_t ledc_timer3;
  ledc_timer3.speed_mode = LEDC_HIGH_SPEED_MODE;           // timer mode
  ledc_timer3.duty_resolution = LEDC_TIMER_3_BIT; // resolution of PWM duty
  ledc_timer3.timer_num = LEDC_TIMER_3;            // timer index
  ledc_timer3.freq_hz = 20000;                      // frequency of PWM signal
  // Set configuration of timer1 for high speed channels
  esp_err_t result3 = ledc_timer_config(&ledc_timer3);
  if (result3 == ESP_OK)
     Serial.printf("timer4 frequency: %d", ledc_get_freq(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_3));

  Serial.println("Low Speed LEDC Channel setup:");



 // Low Speed LEDTimer4 setup
  ledc_timer_config_t ledc_timer4;
  ledc_timer4.speed_mode = LEDC_LOW_SPEED_MODE;           // timer mode
  ledc_timer4.duty_resolution = LEDC_TIMER_3_BIT; // resolution of PWM duty
  ledc_timer4.timer_num = LEDC_TIMER_0;            // timer index
  ledc_timer4.freq_hz = 20000;                      // frequency of PWM signal
  // Set configuration of timer1 for high speed channels
  esp_err_t result4 = ledc_timer_config(&ledc_timer4);
  if (result4 == ESP_OK)
     Serial.printf("timer4 frequency: %d", ledc_get_freq(LEDC_LOW_SPEED_MODE, LEDC_TIMER_0));


  // Low Speed LEDTimer5 setup
  ledc_timer_config_t ledc_timer5;
  ledc_timer5.speed_mode = LEDC_LOW_SPEED_MODE;           // timer mode
  ledc_timer5.duty_resolution = LEDC_TIMER_3_BIT; // resolution of PWM duty
  ledc_timer5.timer_num = LEDC_TIMER_1;            // timer index
  ledc_timer5.freq_hz = 20000;                      // frequency of PWM signal
  // Set configuration of timer1 for low speed channels
  esp_err_t result5 = ledc_timer_config(&ledc_timer5);
  if (result5 == ESP_OK)
     Serial.printf("timer5 frequency: %d", ledc_get_freq(LEDC_LOW_SPEED_MODE, LEDC_TIMER_1));

// Low Speed LEDTimer6 setup
  ledc_timer_config_t ledc_timer6;
  ledc_timer6.speed_mode = LEDC_LOW_SPEED_MODE;           // timer mode
  ledc_timer6.duty_resolution = LEDC_TIMER_3_BIT; // resolution of PWM duty
  ledc_timer6.timer_num = LEDC_TIMER_2;            // timer index
  ledc_timer6.freq_hz = 20000;                      // frequency of PWM signal
  // Set configuration of timer2 for low speed channels
  esp_err_t result6 = ledc_timer_config(&ledc_timer6);
  if (result6 == ESP_OK)
     Serial.printf("timer6 frequency: %d", ledc_get_freq(LEDC_LOW_SPEED_MODE, LEDC_TIMER_2));

// Low Speed LEDTimer7 setup
  ledc_timer_config_t ledc_timer7;
  ledc_timer7.speed_mode = LEDC_LOW_SPEED_MODE;           // timer mode
  ledc_timer7.duty_resolution = LEDC_TIMER_3_BIT; // resolution of PWM duty
  ledc_timer7.timer_num = LEDC_TIMER_3;            // timer index
  ledc_timer7.freq_hz = 20000;                      // frequency of PWM signal
  // Set configuration of timer3 for low speed channels
  esp_err_t result7 = ledc_timer_config(&ledc_timer7);
  if (result7 == ESP_OK)
     Serial.printf("timer7 frequency: %d", ledc_get_freq(LEDC_LOW_SPEED_MODE, LEDC_TIMER_3));
  
// LEDC Channel 0 setup:  
  ledc_channel_config_t ledc_channel0 = {
          .gpio_num   = PWM1,
          .speed_mode = LEDC_HIGH_SPEED_MODE,
          .channel    = LEDC_CHANNEL_0,
          .intr_type  = LEDC_INTR_DISABLE,
          .timer_sel  = LEDC_TIMER_0,
          .duty       = 4,
          .hpoint     = 0
  };
  // Set LED Controller with previously prepared configuration
  ledc_channel_config(&ledc_channel0);
  Serial.println("Channel0 set");

// LEDC Channel 1 setup:  
  ledc_channel_config_t ledc_channel1 = {
          .gpio_num   = PWM2,
          .speed_mode = LEDC_HIGH_SPEED_MODE,
          .channel    = LEDC_CHANNEL_1,
          .intr_type  = LEDC_INTR_DISABLE,
          .timer_sel  = LEDC_TIMER_0,
          .duty       = 4,
          .hpoint     = 0
  };
  // Set LED Controller with previously prepared configuration
  ledc_channel_config(&ledc_channel1);
  Serial.println("Channel1 set");

  // LEDC Channel 2 setup:  
  ledc_channel_config_t ledc_channel2 = {
          .gpio_num   = PWM3,
          .speed_mode = LEDC_HIGH_SPEED_MODE,
          .channel    = LEDC_CHANNEL_2,
          .intr_type  = LEDC_INTR_DISABLE,
          .timer_sel  = LEDC_TIMER_1,
          .duty       = 4,
          .hpoint     = 0
  };
  // Set LED Controller with previously prepared configuration
  ledc_channel_config(&ledc_channel2);
  Serial.println("Channel2 set");

  // LEDC Channel 3 setup:  
  ledc_channel_config_t ledc_channel3 = {
          .gpio_num   = PWM4,
          .speed_mode = LEDC_HIGH_SPEED_MODE,
          .channel    = LEDC_CHANNEL_3,
          .intr_type  = LEDC_INTR_DISABLE,
          .timer_sel  = LEDC_TIMER_1,
          .duty       = 4,
          .hpoint     = 0
  };
  // Set LED Controller with previously prepared configuration
  ledc_channel_config(&ledc_channel3);
  Serial.println("Channel3 set");

   // LEDC Channel 4 setup:  
  ledc_channel_config_t ledc_channel4 = {
          .gpio_num   = PWM5,
          .speed_mode = LEDC_HIGH_SPEED_MODE,
          .channel    = LEDC_CHANNEL_4,
          .intr_type  = LEDC_INTR_DISABLE,
          .timer_sel  = LEDC_TIMER_2,
          .duty       = 4,
          .hpoint     = 0
  };
  // Set LED Controller with previously prepared configuration
  ledc_channel_config(&ledc_channel4);
  Serial.println("Channel4 set");

     // LEDC Channel 5 setup:  
  ledc_channel_config_t ledc_channel5 = {
          .gpio_num   = PWM6,
          .speed_mode = LEDC_HIGH_SPEED_MODE,
          .channel    = LEDC_CHANNEL_5,
          .intr_type  = LEDC_INTR_DISABLE,
          .timer_sel  = LEDC_TIMER_2,
          .duty       = 4,
          .hpoint     = 0
  };
  // Set LED Controller with previously prepared configuration
  ledc_channel_config(&ledc_channel5);
  Serial.println("Channel5 set");

     // LEDC Channel 6 setup:  
  ledc_channel_config_t ledc_channel6 = {
          .gpio_num   = PWM7,
          .speed_mode = LEDC_HIGH_SPEED_MODE,
          .channel    = LEDC_CHANNEL_6,
          .intr_type  = LEDC_INTR_DISABLE,
          .timer_sel  = LEDC_TIMER_3,
          .duty       = 4,
          .hpoint     = 0
  };
  // Set LED Controller with previously prepared configuration
  ledc_channel_config(&ledc_channel6);
  Serial.println("Channel6 set");

     // LEDC Channel 7 setup:  
  ledc_channel_config_t ledc_channel7 = {
          .gpio_num   = PWM8,
          .speed_mode = LEDC_HIGH_SPEED_MODE,
          .channel    = LEDC_CHANNEL_7,
          .intr_type  = LEDC_INTR_DISABLE,
          .timer_sel  = LEDC_TIMER_3,
          .duty       = 4,
          .hpoint     = 0
  };
  // Set LED Controller with previously prepared configuration
  ledc_channel_config(&ledc_channel7);
  Serial.println("Channel7 set");  

//  **************************  Low Speed channels   *********************

// LEDC Channel 8 setup:  
  ledc_channel_config_t ledc_channel8 = {
          .gpio_num   = PWM9,
          .speed_mode = LEDC_LOW_SPEED_MODE,
          .channel    = LEDC_CHANNEL_0,
          .intr_type  = LEDC_INTR_DISABLE,
          .timer_sel  = LEDC_TIMER_0,
          .duty       = 4,
          .hpoint     = 0
  };
  // Set LED Controller with previously prepared configuration
  ledc_channel_config(&ledc_channel8);
  Serial.println("Channel8 set");

// LEDC Channel 9 setup:  
  ledc_channel_config_t ledc_channel9 = {
          .gpio_num   = PWM10,
          .speed_mode = LEDC_LOW_SPEED_MODE,
          .channel    = LEDC_CHANNEL_1,
          .intr_type  = LEDC_INTR_DISABLE,
          .timer_sel  = LEDC_TIMER_0,
          .duty       = 4,
          .hpoint     = 0
  };
  // Set LED Controller with previously prepared configuration
  ledc_channel_config(&ledc_channel9);
  Serial.println("Channel9 set");

  // LEDC Channel 10 setup:  
  ledc_channel_config_t ledc_channel10 = {
          .gpio_num   = PWM11,
          .speed_mode = LEDC_LOW_SPEED_MODE,
          .channel    = LEDC_CHANNEL_2,
          .intr_type  = LEDC_INTR_DISABLE,
          .timer_sel  = LEDC_TIMER_1,
          .duty       = 4,
          .hpoint     = 0
  };
  // Set LED Controller with previously prepared configuration
  ledc_channel_config(&ledc_channel10);
  Serial.println("Channel10 set");

  startCrankTimer();
  ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 8) );
  ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0) );
  ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 8) );
  ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
  Serial.println("Duty Updated");

}

void loop() {

  if (gCrankPosChangeflag == true) {
    incCrankPos();
  }
}
 
Top