Sitter och labbar ihop ett system som ska vrida 2 plattor i utsuget i verkstan hemma. Ventiler helt enkelt...
Jag har inte hållit på så mycket med Arduino men man har väl kopierat en och annan färdig kod och editerat lite..
Stötte på ett oväntat resultat precis när jag monterat servo nummer 2.
Funktionen
Tanken är att när man trycker på en knapp så ska servon den är ansluten till rotera ca 90°.
När man trycker på samma knapp igen så roterar den tillbaka lika mycket.
Det fungerar fint.. Dock inte optimalt efter att jag kopplat in den andra motorn med dess knapp.
Felet
När ja kör ena motorn gör den sin rörelse. När jag kör den andra motorn gör den en kortare rörelse och går direkt tillbaka men bara en gång.
Nästa gång jag trycker på samma knapp gör den det den ska. När man sedan kör den första servon igen har den detta beteendet.
Med den förklaringen ser man symptomen klart och tydligti klippet nedan. (Under klippet är hela koden)
Koppling (Arduino UNO)
Jag använder ~3 och ~5 till servo signal samt 2 och 4 till knapparna.
Extern försörjning på breadboard.
GND sammanlänkad mellan UNO's POWER rad och breadboard.
På knapparnas ena sida sitter jord, på andra sidan går tråd till 2 eller 4 samt till 5v. Ett 10K motstånd sitter mellan 5v och signalen.
Min tanke är att10K motstånden inte räcker för att hålla tillbaka en spik när man skiftar knapp och att man skulle behöva en diod.
Men.. Eftersom jag läst att man brukar ha en resistor tänker jag att det finns en anledning så.. Jag frågar här innan jag hittar på nåt onödigt.
Vad kan felet vara?
Koden
b = button
s = servo
Kod: Markera allt
/*
Program made by Emanuel Teljemo in 2020.
The purpose of the program is to control 2 servo motors that are installed in a ventilation system.
The servos turn airvalves on and off to focus the air flow from the chosen path.
TO DO LIST:
* Shut down servos after 5 sec as a saftey measure against overheating
* Add a function to control a relay based on both servos angle state
* Add LED's to indicate open or closed valve
*WARNING!
*When this code was made I was experimenting so USE AT OWN RISK!
*/
#include <Servo.h> // Includes a servo file that is already installed with Arduino IDE
Servo s1, s2; // Creates a servo object
#define s1Pin 3 // Signal to s1
#define s2Pin 5 // Signal to s2
#define b1Pin 2 // Signal from pushbutton1
#define b2Pin 4 // Signal from pushbutton2
int angle = 7; // Creates and defines initial angle of servomotor on startup
int speed = 10; // The speed ov the servo movement
const int minAngle = 7; // The angle of the motor arm when vent is closed
const int maxAngle = 85; // The angle of the motor arm when vent is open
int b1Pushed = 0; // pushbutton variable set to 0 before setup
int b2Pushed = 0; // pushbutton variable set to 0 before setup
void setup() {
Serial.begin(9600); // setup serial must match buad rate. See buad rate in serial monitor
s1.attach(s1Pin); // attaches the servo on pin 3 to the servo object
s2.attach(s2Pin); // attaches the servo on pin 5 to the servo object
Serial.println("*Teljemo's servo controller initiated*"); // Adds (prints) text in serial monitor
s1.write(angle); // Set the initial motor position
s1.write(angle); // Set the initial motor position
Serial.print("Starting angle is: "); // Prints text
Serial.print(angle); // Prints angle after write
Serial.println(" degrees"); // Prints text
Serial.println("***********************************"); // Prints text
}
void loop() {
if(digitalRead(b1Pin) == LOW){ // Checks if button 1 is pushed down
b1Pushed = 1; // Set value to 1 if above is true
}
if( b1Pushed){ // Asks if button 1 is true
angle = angle + speed; // Changes the value of the angle variable
if (angle <= minAngle || angle >= maxAngle) { // Checks where the servomotor 1 angle is to drive it the opposite direction
speed = -speed;
b1Pushed = 0; // Tells the program that the 1st button os no longer pushed
}
s1.write(angle); // Moving the 1st servo to the defined value of angle
Serial.print("Moved servo 1: "); // Printing text
Serial.print(angle); // Shows the angle value in print
Serial.println(" degrees"); // Printing text
delay(50); // Creates a delay to wait for servo 1 to get in place before looping on
}
if(digitalRead(b2Pin) == LOW){ // Checks if button 2 is pushed down
b2Pushed = 1; // Set value to 1 if above is true
}
if( b2Pushed){ // Asks if button 2 is true
angle = angle + speed; // Changes the value of the angle variable
if (angle <= minAngle || angle >= maxAngle) { // Checks where the servomotor 2 angle is to drive it the opposite direction
speed = -speed;
b2Pushed = 0; // Tells the program that the 2nd button os no longer pushed
}
s2.write(angle); // Moving the 2nd servo to the defined value of angle
Serial.print("Moved servo 2: "); // Printing text
Serial.print(angle); // Shows the angle value in print
Serial.println(" degrees"); // Printing text
delay(50); // Creates a delay to wait for servo 2 to get in place before looping on
}
}