Moving servo w/ audio - Restricting Servo Movement

This is the second post, as there was an error posting the first, so I'll write to the point. First I am trying to have a servo move to audio signals from a 3.5mm audio jack, through the arduino. I have this mostly working; in this video link shows the movement to the audio from PC to a 3.5mm connected, with the monitor up as well:

The issue I'm having is I need the servo to move from a zero point, and forward...or at least for the motion to be constrained to only move in a single direction rather than slightly back and forth. The code attached will show I tried to use constrain to do this (trying constraining different things), but the outcome doesn't change, or at least not as intended.

// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  constrain(sensorValue, 521, 768);           //testing the restraint not working
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 113, 520, 0, 90);
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);

  // print the results to the Serial Monitor:
  Serial.print("sensor = ");
  Serial.print("\t output = ");

  // wait 30 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:

So my question is how can I make this constraint (lets say 0-90 degrees). I was thinking someway to constrain it, but there might be a way you all know that will work better.

Secondary, do not pay this question as much mind as the first, is there a way to smooth out the audio signal so the servo is less jittery? I've heard adding a capacitor does this in some applications, but I haven't seen it specifically for the control of a servo.

Thank you for reading. Picture of the board attached.

The issue I'm having is I need the servo to move from a zero point, and forward...or at least for the motion to be constrained to only move in a single direction rather than slightly back and forth.

Sorry but that doesn't make much sense. A normal servo cannot keep moving in the same direction. Its range of travel is only around 180 degrees. When it gets to 180 it can only move back towards 0 it can't keep going forward past 180.

Why are you trying to control the servo using analogWrite() rather than using the normal Servo.h library? Servos are not designed to be driven by that sort of wide-range relatively high speed PWM. I'm amazed that it works as well as it apparently does.


I think the starting position isn't 0 (in a 0-180). Hypothetically it's actually at 90 degrees. I want it to be able to start at 0 lets say, and only move towards 180. What I have now (I think) is it starting at the hypothetical 90, and I want it to go from 90-180, but occasionally it shoots towards 0 (like 70 degrees). So the issue can be it starting at 0 in the first place, but that would be part of this issue I'm trying to resolve.

The second part; I was dissecting other used code and trying to put something together myself that worked. Is it an issue that I should be worried about using analogueWrite() rather than the Servo.h library? If so, I need to look into what that library does specifically and change a bit. If this isn't a big issue, I hope the first section clarified my issue a little better.

I apologize, I fixed the issue. As it is now resting at 0, there is no "negative" direction like I was mentioning earlier. Now it's just about the constraint. Same as above, but locking the servo to not move pass 90 degrees.

Constrain doesn't change the numbers passed to it. It returns the constrained number.

When you have fixed that, your map function seems to be taking numbers that make no sense since you just constrained the sensor value to lie outside the range of the map.

I've never tried using analogWrite() for servos because Servo.h is so easy to use and many servos simply will not work with a 490Hz/980Hz signal when they are designed for 50Hz.

If you want to stick with your way then maybe do a controlled test to see what value in the analogWrite() positions the servo where you want it. The value is not very likely to be 90 because that relates to how the Servo library write() works (0-180) not analogWrite() which is 0-255.