Help with servo code and PWM

I have parts of this sorted but I'm missing something important...

This code works fine:

// Actuates migamotor NM70R-6P actuator with MAD-V5
//http://www.migamotors.com/

int pin         =  9;     // SMA actuator gate pin on MAD-V5 connected to PWM pin 9  
int pulsewidth  =  50;    // Any value between 0 and 255. Higher value = faster actuation  
  
void setup() {  
  // None required for analogWrite!  
}  
  
void loop() {  
  analogWrite(pin, pulsewidth); /// Actuator wire contracts
 delay(2000);
analogWrite(pin, 0);///Actuator wire relaxes
delay(6000);
}

It just nods the head on a litt;e bot I'm fooling with.

I'm trying to get it to work with the "sweep" servo example here:

// Sweep Scan
// This code runs a sweeping/scanning routine with a standard servo signal wire connected to pin 10 and a miga motors nano muscle with a MAD-V5 driver on pin 9


#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0; // variable to store the servo position

                             // Actuates migamotor NM70R-6P actuator with MAD-V5
                             //http://www.migamotors.com
int pin         =  9;     // SMA actuator gate pin on MAD-V5 connected to PWM pin 9  
int pulsewidth  =  50;    // Any value between 0 and 255. Higher value = faster actuation  

 
void setup() 
{ 
  myservo.attach(10);  // attaches the servo on pin 10 to the servo object 
} 
 
 
void loop()
{

  analogWrite(pin, pulsewidth); /// Actuator wire contracts
 delay(2000);
analogWrite(pin, 0);///Actuator wire relaxes
delay(6000);

  
   

  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  
 


  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
    
    
    
    
    
     
    


  }
  
  
 

}

I have my ints in the right spot (I think) and I've plugged the other part of the code in different parts of the sketch where I want it to operate.

I'm guessing I need a function and I'm currently reading up on it but any pointers would be great.

Thanks

Not sure what you are trying to do with the PWM, but below is some simple servo test code using the servo library.

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  } 
}

The PWM is to run this:

And the "sweep" sketch is to run the head rotation shown here:

I like the sweep code because it makes it easy to have speed control of servo rotation and I want him to be somewhat "alive".

I'm having trouble figuring out if my troubles are power related because I'm using 7805 voltage regulators and maybe having both servos on the same line is gorking things out a bit... I have some DC/DC converters ordered though.

Both sketches I posted work fine but as soon as I try to combine them, Say, sweep one direction 180-nod-sweep back 0 it goes to pot. I can even count the delay but no nod?

hoff70:
I have parts of this sorted but I'm missing something important...

This code works fine:

int pin         =  9;     // LED connected to PWM pin 11  

int pulsewidth  =  50;    // Any value between 0 and 255  
 
void setup() {  
 // None required for analogWrite!  
}  
 
void loop() {  
 analogWrite(pin, pulsewidth);
delay(2000);
analogWrite(pin, 0);
delay(6000);
}




What you are missing is the following fine print in the servo library documentation:

<mark>The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins. On the Mega, up to 12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12.</mark> 

So if you change the LED to be on another PWM pin than 9 or 10, it should work. There was an another post with the same problem three days ago in the displays forum (since the OP was using a 16x2 display with PWM instead of a led).

Wow...
Thanks Michael!
As soon as I think I have a TINY bit of comprehension about what's going on I get a curve ball :roll_eyes: I had a hunch it was something to do with analogWrite and servo.h... I figured they both use the clock so something was screwy.

Anyway, here's what I was trying to accomplish:

I want him to be "expressive" :stuck_out_tongue:

int pin         =  9;     // LED connected to PWM pin 11

The compiler doesn't read the comments. People do. When the comments don't match the code, I'm left to wonder what kind of idiot wrote this mess. Make the comments match, or make the variable names self-documenting.

const int ledPin = 9;

Now, I know what this pin is for, and which pin the LED should be attached to. No comment needed, and no comment needs to be maintained. Being const, you can't accidentally overwrite it, either.

  myservo.attach(10);  // attaches the servo on pin 9 to the servo object

Is the servo attached to pin 9 or 10? You should have a variable, with a decent name, that defines the servo pin.

PaulS:

const int ledPin = 9;

Now, I know what this pin is for, and which pin the LED should be attached to. No comment needed, and no comment needs to be maintained. Being const, you can't accidentally overwrite it, either.

Also being const allows the compiler to optimize this. Instead of having to read ledPin from a memory location, it can possibly load the immediate value directly into a register.

Sorry Paul, that was inconsiderate of me. I would have been confused as he!! about a month ago looking at that mess. Still am actually...

I made an attempt to tidy the comments up so they may make a little sense to anyone checking it out in the future. I tried to describe what the sketch is supposed to do and I left the pin errors so the rest of the comments would make sense.

Michael, could you elaborate more on using ints? There are just SOOOO many different ways of doing things and I'm trying to settle into actually writing some code myself but it's a steep curve. Truth is, it's just plain drudgery for me but necessary for making stuff happen...

Writing pinMode 1000 times is a PITA :fearful:

Thanks

Writing pinMode 1000 times is a PITA

Why do you need to? Do you have some special Arduino that has 1000 pins AND an aversion to arrays and for loops?

PaulS:

Writing pinMode 1000 times is a PITA

Why do you need to? Do you have some special Arduino that has 1000 pins AND an aversion to arrays and for loops?

I will use The Force...