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
Sorry, I couldn't figure out how to upload the sketch.
I thought I had it
Not sure wht you mean by INPUT_PULLUP, total noob here.
I'm surprised I got this far
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); ?
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?
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
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();
{
}
}