Error message when trying to send pwm signal in Arduino Due

Hello, I am trying to connect a transmitter/receiver combo (FS-i6X/FS-iA10B) to my Arduino Due, and when I run my code to send a pwm signal to channel 2, I receive an error and it crashes the serial monitor.

This is my code:

//int ch1; //axis 2
int ch2; //thrust
//int ch3; //axis 1

int pwm = 8;
int thrust;

void setup() {

  //pinMode(5, INPUT); // Set input pins 
  pinMode(6, INPUT);
  //pinMode(7, INPUT);
  pinMode(pwm, OUTPUT);
  

  Serial.begin(9600);
  analogWrite (pwm, 0);
}

void loop() {

 //ch1 = pulseIn(5, HIGH); // Read the pulse width of each channel
  ch2 = pulseIn(6, HIGH); 
 //ch3 = pulseIn(7, HIGH);

  //Serial.print("Channel 1:"); // Print the value of 
  //Serial.println(ch1);        // each channel [1498 - 2996]

  //Serial.println();

  Serial.print("Channel 2:");
  Serial.println(ch2);          // [1498 - 2996]

  //Serial.println();

  //Serial.print("Channel 3:");
  //Serial.println(ch3);

  Serial.println();

  thrust = map(ch2, 1498, 2996, 0, 255); //map channel 2 to arduino
  constrain (thrust, 0, 255);

  Serial.print("Thrust:");
  Serial.println(thrust);
  
  Serial.println();

  analogWrite(pwm, thrust);
  pwm = min(pwm, 255);
  pwm = max(pwm, -255);


  Serial.print("PWM:");
  Serial.println(pwm);

  Serial.println();
}

All of the commented out stuff is for the other channels, when I have the other motors ready to use.

This is the error I get:

assertion "duty <= pPwm->PWM_CH_NUM[ul_channel].PWM_CPRD" failed: file "../source/pwmc.c", line 272, function: PWMC_SetDutyCycle

A quick google search told me it had something to do with analogWrite, and the maximum values for the map function, but I have no idea as to how to fix this, or if there is another way I could implement the same functions without map or analogWrite.

(deleted)

analogWrite(pwm, thrust);
pwm = min(pwm, 255);
pwm = max(pwm, -255);

After the first pass through loop, what pin do you think you're writing to?

Regards,
Ray L.

spycatcher2k:

pwm = max(pwm, -255);

Between 0 & 255 :slight_smile:

I did that as well and I still got the same result.

RayLivingston:

analogWrite(pwm, thrust);

pwm = min(pwm, 255);
pwm = max(pwm, -255);




After the first pass through loop, what pin do you _**think**_ you're writing to?

Regards,
Ray L.

I think I am writing to pin 8, the pwm pin.

nsrinath97:
I think I am writing to pin 8, the pwm pin.

Then why do you feel it is necessary to hve those two lines of code to constrain the pin number to be between 255 and -255 on every pass of loop?
Regards,
Ray L.

RayLivingston:
Then why do you feel it is necessary to hve those two lines of code to constrain the pin number to be between 255 and -255 on every pass of loop?
Regards,
Ray L.

I'm basically trying to do anything I can to constrain the value of the pwm. I had only the thrust value constrained at one point, and just wrote that to pin 8, but it made no difference and gave me the same error so I thought maybe constraining the pwm value as well might do something.

Edit: and also the only time the error goes away is when I comment off analogWrite(pwm, thrust)

nsrinath97:
I'm basically trying to do anything I can to constrain the value of the pwm. I had only the thrust value constrained at one point, and just wrote that to pin 8, but it made no difference and gave me the same error so I thought maybe constraining the pwm value as well might do something.

Edit: and also the only time the error goes away is when I comment off analogWrite(pwm, thrust)

Those two lines DO NOTHING to constrain the calculated pwm VALUE. They constrain the pwm PIN NUMBER.
Regards,
Ray L.

RayLivingston:
Those two lines DO NOTHING to constrain the calculated pwm VALUE. They constrain the pwm PIN NUMBER.
Regards,
Ray L.

I removed them, but im still at a loss on how to get past that error.

When you encounter an error you'll see a button on the right side of the orange bar in the Arduino IDE "Copy error messages" (or the icon that looks like two pieces of paper in the Arduino Web Editor). Click that button. Paste the error in a reply here using code tags.

If the text exceeds the forum's 9000 character limit, save it to a .txt file and post it as an attachment. If you click the "Reply" button here, you will see an "Attachments and other settings" link.

pert:
When you encounter an error you'll see a button on the right side of the orange bar in the Arduino IDE "Copy error messages" (or the icon that looks like two pieces of paper in the Arduino Web Editor). Click that button. Paste the error in a reply here using code tags.

If the text exceeds the forum's 9000 character limit, save it to a .txt file and post it as an attachment. If you click the "Reply" button here, you will see an "Attachments and other settings" link.

The error comes up in the serial monitor

assertion "duty <= pPwm->PWM_CH_NUM[ul_channel].PWM_CPRD" failed: file "../source/pwmc.c", line 272, function: PWMC_SetDutyCycle

This line does nothing useful:

  constrain (thrust, 0, 255);

I recommend that you read its documentation to understand how to use it.