Attiny85 servo control

Hello, noob question for you guys.
I'm building this remote actuator that runs on batteries for a friend using an attiny85 a servo and a button.
I can get the servo to work just fine with the button but my issue is with the disconnect for the servo power. To save battery power I'm turning on/off an FET on pin 4 which cuts off power to the servo after the routine. Most of it works, except that the FET turns on when I push the button, the servo does it's thing but I cannot get the FET to turn off after the servo returns home.
My sketch is attached. Any help will be greatly appreciated

#include <avr/sleep.h>
#include <SoftwareServo.h> // include the software servo library

SoftwareServo servo; // create a software servo object

int switchPin = 1; // define the switch pin
int servoPos = 70; // starting servo position

void setup() {
servo.attach(0); // attach the servo to pin 0
pinMode(switchPin, INPUT); // set the switch pin as input
pinMode(4, OUTPUT); // For the MOSFET

}

void loop() {

if (digitalRead(switchPin) == HIGH) { // check if the switch is pressed
digitalWrite(4,HIGH);

servoPos = 150; // set servo position to 150 degrees

} else {
servoPos = 70; // set servo position to 10 degrees
}
servo.write(servoPos); // move the servo to the desired position

delay(100);
digitalWrite(4,LOW);

SoftwareServo::refresh();

{
} }

You managed to get your lead-up and question inside code tags, but not your sketch.
So close.

Have you seen about INPUT_PULLUP ?

Sorry, I couldn't figure out how to upload the sketch.
I thought I had it :slight_smile:
Not sure wht you mean by INPUT_PULLUP, total noob here.
I'm surprised I got this far

How to get the best out of this forum - Using Arduino / IDE 1.x - Arduino Forum

INPUT | INPUT_PULLUP | OUTPUT - Arduino Reference

I read the description. Not in front of my circuit now but are u suggesting to change " pinMode(switchPin, INPUT);" to " pinMode(switchPin, INPUT_PULLUP); ?

Yes - and the switch wiring will, most likely, have to be jiggered to accommodate.

The switch is a wireless module and stays hi (5v) and goes low when actuated.
I'm not sure I understand how is the switch affecting the on/off part on pin4 .
Pin4 goes on, servo moves to 150 and back, but pin4 doesn't turn back off.
Am I missing something?

Is the Gate pulled down?
Try getting that code tags deal straightened out.

It's getting late here now.
I will ltry your suggestion and look for a way to upload the sketch tomorrow
Thank you for all the help.

Did you copy/paste something out of your sketch?

No, I didn't uploaded the sketch correctly.
I'll try again tomorrow after reading the right way to do it in here.



include <avr/sleep.h>
#include <SoftwareServo.h> // include the software servo library

SoftwareServo servo; // create a software servo object

int switchPin = 1; // define the switch pin
int servoPos = 70; // starting servo position

void setup() {
servo.attach(0); // attach the servo to pin 0
pinMode(switchPin, INPUT); // set the switch pin as input
pinMode(4, OUTPUT); // For the MOSFET

}

void loop() {

if (digitalRead(switchPin) == HIGH) { // check if the switch is pressed
digitalWrite(4,HIGH);

servoPos = 150; // set servo position to 150 degrees


} else {
servoPos = 70; // set servo position to 10 degrees
}
servo.write(servoPos); // move the servo to the desired position

delay(100);
digitalWrite(4,LOW);

SoftwareServo::refresh();

{
} }

Immediately before this you have a "delay(100)"... but the library says this:

    static void refresh();    // must be called at least every 50ms or so to keep servo alive
                              // you can call more often, it won't happen more than once every 20ms

The library: arduino/libraries/SoftwareServo/SoftwareServo.h at master · nicolaskruchten/arduino · GitHub

Total noob here, not sure what the solution could be

Should I remove that line ?

Here is your uploaded sketch, formatted, and posted between < CODE > tags. You can make your own code tags by making three "back tick marks" (```), pasting your formatted code, then making three more back-ticks. The result will looks like this:

#include <avr/sleep.h>
#include <SoftwareServo.h> // include the software servo library

SoftwareServo servo; // create a software servo object

int switchPin = 1; // define the switch pin
int servoPos = 70; // starting servo position

void setup() {
  servo.attach(0); // attach the servo to pin 0
  pinMode(switchPin, INPUT); // set the switch pin as input
  pinMode(4, OUTPUT); // For the MOSFET

}

void loop() {

  if (digitalRead(switchPin) == HIGH) { // check if the switch is pressed
    digitalWrite(4, HIGH);

    servoPos = 150; // set servo position to 150 degrees

  } else {
    servoPos = 70; // set servo position to 10 degrees
  }
  servo.write(servoPos); // move the servo to the desired position

  delay(100);
  digitalWrite(4, LOW);

  SoftwareServo::refresh();

  {
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.