need help with single servo control

I do industrial controls. I can't believe how powerful these little microcontrollers are. As far as interfacing these things I have that pretty much down but I'm a total noob when it comes to programing.

I found this project-

http://www.arduino.cc/en/Tutorial/knob

It does exactly what I need but i need one change- I need an additional input hooked to a switch. When the input is activated, the output of the Arduino should make a servo pulse from 1.2 ms to 2 ms according to the position of the pot. When the input is denergized, the servo signal needs to go to 1 ms and stay there regardless of the position of the pot. What would I need to do this code to add the extra input. I'm sorry if I'm asking to be spoon fed. This is for a cool project, controlling a miroturbine.

If you search user "krugtech" on youtube you can see my turbine powered van. This will be the next thing that gets a microcontroller.

The Servo library uses a value from 0 to 180 to represent an angle of turn. The standard servo library has a minimum pulse width of 1 milliseconds and a maximum of 2. The output pulse width will be about 1+(value/180) milliseconds. I think a 1.2 millisecond pulse would be a control value of 36.

if (digitalRead(buttonPin) == HIGH)
    {
    myservo.write(map(analogRead(knobPin), 0, 1023, 36, 180));
    }
else
    {
    myservo.write(0);
    }

Or you could use the "writeMilliseconds" method.

Your project sounds cool enough so that I'll throw in a sample how that thing may work:

#include <Servo.h>

const int buttonPin = 2;        // the number of the pushbutton pin
const int potPin =  0; // the number of the potentiometer pin (Analog 0)
const int servoPin = 12; // Servo control line

const int ServoIdle = 1000;      // Servo position when idling in ms.
const int ServoMin = 1200;      // Minimum pulse width for servos in ms.
const int ServoMax = 2000;      // Maximum pulse width for servos in ms.
const int DebounceDelay = 100; 

Servo myServo; 

void setup() {
        // initialize the button pin as an input:
        pinMode(buttonPin, INPUT);
        // Activate internal pull up resistor for button
        digitalWrite(buttonPin, HIGH);

        // Initialise Servos. First set it to idle position, then attach to prevent movement on startup
        myservo.writeMicroseconds (ServoIdle);
        myServo.attach (servoPin);
}

void loop() {
    static unsigned long debounce = 0;         // variable for reading the pushbutton status
    static int buttonstate = HIGH;         // variable for reading the pushbutton status

    // Read button and debounce.
    int buttonnow = digitalRead(buttonPin);
    if (millis() - debounce >= DebounceDelay
        && buttonnow != buttonstate) {
        buttonstate = buttonnow; 
        debounce = millis();

        if (buttonstate == HIGH) {
             // The button has just been released - move back to idle
             myservo.writeMicroseconds (ServoIdle);
        }
    }

    // If button is pressed, track potentiometer
    if (buttonstate == LOW) {
        int potnow = analogRead (potPin);      // Read potentiometer
        myservo.writeMicroseconds (map (potnow, 0, 1023, ServoMin, ServoMax));    // Set Servo
        delay (50);    // Wait a little to give servo time to move
    }
}

As usual, the code is untested.

Korman

Oops, writeMicroseconds.
Brain-fart.

Thanks for all your help. Here's the van-

I'd really like to automate some functions on the van. I ordered an Arduino, it will be here tomorrow, time to learn something new. There's just too much capability here.

Got it working. Thank you Korman. This was the first time I've ever done anything like this. I had all kinds of problems at first but I stayed at it and got it done.

botton was misspelled, I used potPin 0 instead of 16 and I had all kinds of servo errors until i added #include <Servo.h>

Don't get me wrong, I'm not complaining. You guys did the heavy lifting, I just had to test it a little and I learned a lot.

Video of the scooter up shortly

#include <Servo.h> 


const int buttonPin = 2;        // the number of the pushbutton pin
const int potPin =  0; // the number of the potentiometer pin (Analog 0)
const int servoPin = 12; // Servo control line

const int ServoIdle = 1000;      // Servo position when idling in ms.
const int ServoMin = 1200;      // Minimum pulse width for servos in ms.
const int ServoMax = 2000;      // Maximum pulse width for servos in ms.
const int DebounceDelay = 100; 

Servo myservo; 

void setup() {
        // initialize the pot and button pin as an input:
        pinMode(potPin, INPUT);
        pinMode(buttonPin, INPUT);
        // Activate internal pull up resistor for button
        digitalWrite(buttonPin, HIGH);

        // Initialise Servos. First set it to idle position, then attach to prevent movement on startup
        myservo.writeMicroseconds (ServoIdle);
        myservo.attach (servoPin);
}

void loop() {
    static unsigned long debounce = 0;         // variable for reading the pushbutton status
    static int buttonstate = HIGH;         // variable for reading the pushbutton status

    // Read button and debounce.
    int buttonnow = digitalRead(buttonPin);
    if (millis() - debounce >= DebounceDelay
        && buttonnow != buttonstate) {
        buttonstate = buttonnow; 
        debounce = millis();

        if (buttonstate == HIGH) {
             // The button has just been released - move back to idle
             myservo.writeMicroseconds (ServoIdle);
        }
    }

    // If button is pressed, track potentiometer
    if (buttonstate == LOW) {
        int potnow = analogRead (potPin);      // Read potentiometer
        myservo.writeMicroseconds (map (potnow, 0, 1023, ServoMin, ServoMax));    // Set Servo
        delay (50);    // Wait a little to give servo time to move
    }
}

Chris,

when I wrote the code is untested, I was serious. I wrote it on the airport while waiting to board. But I'm glad it helped you. Now I'm going to fix the errors you mentioned in my posting above and nobody will know they ever where there...

Korman

What you did was perfect. You gave me a rough idea of where I needed to go and with your info, I got the job done and learned. if the code worked perfect, I would not have learned anything. I have people who work for me, when they need answers, I don't give them the answer, I give them something that will make them figure it out themselves and that's a great thing.

I've done a lot of cool projects over the years. I've been on TV a few times and I'll be on NatGeo in October on a show about amateur inventors. People like you Korman are the ones who helped me get to this point and I will never forget i could never do this alone.

You whipped up some code while waiting for a plane. it was rough and untested but a total noob like me took that code and in no time at all had a working project. That's talent.

don't give them the answer, I give them something that will make them figure it out themselves and that's a great thing.

Oh please, say it again - it makes me feel good!

I have to admit, the errors in there weren't added on purpose, they were just inattention. I tried to produce something correct even without the compiler at hand.

Korman

Here's the scooter-

I was driving around the neighborhood looking for my helmet and fire suit.

Thanks again!

Cool!

Korman

Hey guys, that scooter was extremely unsafe! It almost killed me and then tried to take out my son. We had to put it down, it was evil. We got a racing bike and coupled the turbine directly to the rear wheels via a special gearbox-

I could use all your help again. I'd like to monitor the speed of the turbine output shaft (about 17,000 rpm) and set it up so when I approach a target rpm the servo output signal that controls the throttle on the turbine will start to power down.

There's 2 shafts in this turbine, the main shaft spins 160,000 and is monitored by the turbines controller. the second shaft is driven by a second dedicated turbine wheel, this shaft can be used to power whatever you want with the proper gearing. The thing is, they relied heavily on a huge airplane propeller to keep the power turbine of this thing from overspeeding and it worked well. The prop would go supersonic at the tips and cause all kinds of drag before it got close to the turbines burst rpm.

the actual rpm of the power turbine is 60,000, it goes through a 3.43:1 reduction then there's the chain drive with about a 10:1 reduction. I figure picking up the rpm off the intermediate shaft would be best as it spins slower than 60,000. I could also pick up the power turbine speed by monitoring rear wheel speed but I would have to correct the program whenever i changed gear ratios.

can anyone give me a clue as to where the best place to put a speed sensor would be? writing programs isn't easy for me but when it comes to sensors and transducers and signal exchange I can get it done.

I got a sensor configured-

I know how to make good signals, i just need help with the code!

Chris

update- the scooter aka WOAT (waste of a turbine) almost killed me and my son so I ripped everything out and used the parts to build this little bike. I made a new gearbox for the turbine with gears from an angle grinder. This thing is fast and much safer.

Chris