Skip to content

Flying Fader Arduino doesn't do variable duty-cycle PWM, unlike Circuit Python version #2875

Open
@aspdigital

Description

@aspdigital

(Refer to the Adafruit article)
Starting here: Flying_Fader_Arduino, in the go_to_position function starting at line 100:

void go_to_position(int new_position) {
  fader_pos = int(analogRead(fader) / 4);
  while (abs(fader_pos - new_position) > 4) {
   if (fader_pos > new_position) {
    speed = 2.25 * abs(fader_pos - new_position) / 256 + 0.2;
    speed = constrain(speed, -1.0, 1.0);
      if (speed > 0.0) {
        analogWrite(pwmA, 255);
        analogWrite(pwmB, 0);
      }
   }
   if (fader_pos < new_position) {
      speed = 2.25 * abs(fader_pos - new_position) / 256 - 0.2;
      speed = constrain(speed, -1.0, 1.0);
        if (speed > 0.0) {
          analogWrite(pwmA, 0);
          analogWrite(pwmB, 255);
        }
      }
      
    fader_pos = int(analogRead(fader) / 4);
    
  }
  analogWrite(pwmA, 0);
  analogWrite(pwmB, 0);
}

Looks to me like the duty cycle is always 100% when the fader is moving. The speed calculation is not really useful.

Compare to the Circuit Python version (starting at line 60):

def go_to_position(new_position):
    global fader_pos  # pylint: disable=global-statement
    fader_pos = int(fader.value//256)
    while abs(fader_pos - new_position) > 2 :
        if fader_pos > new_position :
            speed = 2.25 * abs(fader_pos - new_position) / 256 + 0.12
            speed = clamp(speed, -1.0, 1.0)
            motor1.throttle = speed
            led[0] = (fader_pos, 0, 0)

            if MIDI_DEMO:
                global fader_cc  # pylint: disable=global-statement
                fader_cc = int(fader_pos / 2)  # cc is 0-127
                midi.send(ControlChange(fader_cc_number, fader_cc))

        if fader_pos < new_position:
            speed = -2.25 * abs(fader_pos - new_position) / 256 - 0.12
            speed = clamp(speed, -1.0, 1.0)
            motor1.throttle = speed
            led[0] = (fader_pos, 0, 0)

            if MIDI_DEMO:
                fader_cc = int(fader_pos / 2)  # cc is 0-127
                midi.send(ControlChange(fader_cc_number, fader_cc))

        fader_pos = int(fader.value//256)
    motor1.throttle = None

Here we see the PWM duty cycle, which is the throttle member of the motor1 instance, being set to the (clamped) calculated speed.

Why the difference? A typo? Untested code? Just wondering.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions