Go Down

Topic: Output is stepping? (Read 84 times) previous topic - next topic

bhay

Mar 01, 2015, 10:35 pm Last Edit: Mar 01, 2015, 11:55 pm by bhay Reason: I've got information man. New shit has come to light! -Lebowski
I'm writing a fairly basic 1 input, 1 output code right now but I've got a problem where my out put has 3 distinct steps but it is supposed to gradually rise and fall with the varying input. Here is my code:

Code: [Select]
// Initializes pin values
int ledPin = 9;
int throttle = 7;

void setup() {
  // initialize throttle as input and ledPin as output
  pinMode(throttle, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // takes the time the pulse is high and low and then calculates the duty cylce
  double inputHigh = pulseIn(7, HIGH);
  double inputLow = pulseIn(7, LOW);
  double dutyCycle = (inputHigh / (inputHigh + inputLow)) * 100;
 
  // maps the min and max duty cycle to the min and max brightness of the LED
  double outputValue = map(dutyCycle, 5, 9.1, 0, 255);
  analogWrite(ledPin, outputValue);
 
  // Turns the LED off if there is no input or too much input
  if(dutyCycle < 5 or dutyCycle > 9.1){
    analogWrite(ledPin, 0);
  }
}


Why is the LED brightness distinctly stepping instead of fading?

PaulS

Quote
but I'm pretty sure I need to use a write or analogWrite command but I'm blanking
Correct. You do.

bhay

Sorry, big edit, my problem completely changed

PaulS

Code: [Select]
  // maps the min and max duty cycle to the min and max brightness of the LED
  double outputValue = map(dutyCycle, 5, 9.1, 0, 255);
  analogWrite(ledPin, outputValue);

Why do you assume that the measured duty cycle will be between 5 and 9 (not 9.1)?

Code: [Select]
Why is the LED brightness distinctly stepping instead of fading?
Because there are only 5 distinct output values, if the input is in range. The input is an int. The output is an int.

bhay

#4
Mar 02, 2015, 12:07 am Last Edit: Mar 02, 2015, 12:10 am by bhay
Before writing the code I hooked the throttle output of the receiver for my controller up to an oscilloscope. When the throttle stick was at rest the duty cycle ranged between 5-5.2% and when the throttle was maxed out it alternated between 8.9-9.1%

Also what are some changes I could make so that the output will fade and not step?

PaulS

Quote
Also what are some changes I could make so that the output will fade and not step?
You are computing duty cycle as a double (float, really), and then using an integer function for mapping.

The input is truncated to an int. The output is an int. Clearly, you want to use some other method of getting from a range of 5 to 9.1 to a range of 0 to 255.Simply subtracting 5.0 and then multiplying by 255/4.1 would be the easiest way.

johnwasser

#6
Mar 02, 2015, 04:29 am Last Edit: Mar 02, 2015, 04:30 am by johnwasser
Before writing the code I hooked the throttle output of the receiver for my controller up to an oscilloscope. When the throttle stick was at rest the duty cycle ranged between 5-5.2% and when the throttle was maxed out it alternated between 8.9-9.1%
RC receiver outputs generally go to servos or ESC's which use the length of the positive pulse, not the duty cycle.  You will typically get pulse values between 1000 and 2000 microseconds.
Code: [Select]
unsigned long inputHigh = pulseIn(7, HIGH);
  // maps the min and max pulse length to the min and max brightness of the LED
  int outputValue = map(inputHigh, 1000, 2000, 0, 255);
  analogWrite(ledPin, constrain(outputValue, 0, 255));
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up