Re: Lustigt fel på Arduino-krets (Video och kod included)
Postat: 29 december 2020, 13:40:36
Det finns ingen if-tilldelning. Antingen har ni läst fel eller så är koden ändrad i efterhand.
Svenskt forum för elektroniksnack.
https://elektronikforumet.com/forum/
Kod: Markera allt
if (s1angle <= s1minAngle || s1angle >= s1maxAngle)
{ // Checks where the 1st servomotor's angle is to drive it the opposite direction
s1speed = -s1speed;
}
Kod: Markera allt
if (s1angle >= 80)
{
digitalWrite(s1LedClosed, LOW);
digitalWrite(s1LedOpen, HIGH);
}
if (s1angle <= 10)
{
digitalWrite(s1LedOpen, LOW);
digitalWrite(s1LedClosed, HIGH);
}
Kod: Markera allt
if (s1angle == s1minAngle || s1angle == s1maxAngle) { // Checks where the 1st servomotor's angle is to drive it the opposite direction
s1speed = -s1speed;
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 saftey control function to check servo position after movement
Shorten code (combine variables)
Remake button code so it only reads 1 signal and doesnt keeps running servos if button is hold
WARNING!
When this code was made I was experimenting so USE AT OWN RISK!
*/
// Setting up servo's
#include <Servo.h> // Includes a servo file that is already installed with Arduino IDE
Servo s1, s2; // Creates servo object's
// Defines pins on the arduino board
#define s1Pin 3 // Signal from servo 1
#define s2Pin 5 // Signal from servo 2
#define b1Pin 2 // Signal from pushbutton1
#define b2Pin 4 // Signal from pushbutton2
#define s1LedOpen 8
#define s1LedClosed 9
#define s2LedOpen 10
#define s2LedClosed 11
// Defines variables for servo 1
int s1angle = 7; // Initial angle for servo 1
int s1speed = 10; // Speed for servo 1 movement
const int s1minAngle = 7; // Angle of servo 1 arm when vent is closed
const int s1maxAngle = 87; // Angle of servo 1 arm when vent is open
// Defines variables for servo 2
int s2angle = 7; // Initial angle for servo 2
int s2speed = 10; // Speed for servo 2 movement
const int s2minAngle = 7; // Angle of servo 2 arm when vent is closed
const int s2maxAngle = 87; // Angle of servo 2 arm when vent is open
// Creates variables for buttons
int b1Pushed = 0; // Button variable set to 0 before setup
int b2Pushed = 0; // Button variable set to 0 before setup
// Setup
void setup() {
// Connections
Serial.begin(9600); // Serial need to match buad rate. See buad rate in serial monitor
//Attaching/activating servos
s1.attach(s1Pin); // Attaches servo 1 on pin 3
s2.attach(s2Pin); // Attaches servo 2 on pin 5
// Pinout for LED's
pinMode(s1LedOpen, OUTPUT);
pinMode(s1LedClosed, OUTPUT);
pinMode(s2LedOpen, OUTPUT);
pinMode(s2LedClosed, OUTPUT);
Serial.println("*Teljemo's servo controller initiated*"); // Print text in serial monitor
// Initial movement
s1.write(s1angle); // Set the initial position for servo 1 (homeing, when mounted it should be open)
s2.write(s2angle); // Set the initial position for servo 2 (homeing, when mounted it should be open)
// Print for serial monitor
Serial.println("Starting angles are: "); // Print text in serial monitor
Serial.print(s1angle); // Prints angle of servo 1
Serial.println(" degrees for servo 1"); // Print text in serial monitor
Serial.print(s2angle); // Prints angle of servo 2
Serial.println(" degrees for servo 2"); // Print text in serial monitor
Serial.println("***********************************"); // Print text in serial monitor
}
// End of setup
// Loop
void loop() {
// Button 1
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 pushed
s1angle = s1angle + s1speed; // Changes the value of the 1st angle variable
if (s1angle == s1minAngle || s1angle == s1maxAngle) { // Checks where the 1st servomotor's angle is to drive it the opposite direction
s1speed = -s1speed;
b1Pushed = 0; // Tells the program that the 1st button is no longer pushed
}
s1.write(s1angle); // Moving the 1st servo to the defined value of s1angle
Serial.print("Moved servo 1: "); // Print text in serial monitor
Serial.print(s1angle); // Prints angle of servo 1
Serial.println(" degrees"); // Print text in serial monitor
delay(1); // Creates a delay to wait for servo 1 to get in place before looping on
}
// Button 2
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
s2angle = s2angle + s2speed; // Changes the value of the angle variable
if (s2angle == s2minAngle || s2angle == s2maxAngle) { // Checks where the 2nd servomotor's angle is to drive it the opposite direction
s2speed = -s2speed;
b2Pushed = 0; // Tells the program that the 2nd button os no longer pushed
}
s2.write(s2angle); // Moving the 2nd servo to the defined value of angle
Serial.print("Moved servo 2: "); // Print text in serial monitor
Serial.print(s2angle); // Prints angle of servo 2
Serial.println(" degrees"); // Print text in serial monitor
delay(1); // Creates a delay to wait for servo 2 to get in place before looping on
}
// LED check
//Servo 1 LED's
if (s1angle == 87) {
digitalWrite(s1LedClosed, LOW);
digitalWrite(s1LedOpen, HIGH);
}
if (s1angle == 7) {
digitalWrite(s1LedOpen, LOW);
digitalWrite(s1LedClosed, HIGH);
}
// Servo 2 LED's
if (s2angle == 87) {
digitalWrite(s2LedClosed, LOW);
digitalWrite(s2LedOpen, HIGH);
}
if (s2angle == 7) {
digitalWrite(s2LedOpen, LOW);
digitalWrite(s2LedClosed, HIGH);
}
//End of loop
}
// End of program
Kod: Markera allt
int b1State = HIGH;
int b1PrevState = HIGH;
void loop() {
b1State = digitalRead(b1Pin);
if ((b1State != b1PrevState) && (b1State == LOW)) {
s1angle = s1angle + s1speed; // Changes the value of the 1st angle variable
if (s1angle == s1minAngle || s1angle == s1maxAngle) { // Checks where the 1st servomotor's angle is to drive it the opposite direction
s1speed = -s1speed;
}
s1.write(s1angle); // Moving the 1st servo to the defined value of s1angle
Serial.print("Moved servo 1: "); // Print text in serial monitor
Serial.print(s1angle); // Prints angle of servo 1
Serial.println(" degrees"); // Print text in serial monitor
delay(1); // Creates a delay to wait for servo 1 to get in place before looping on
}
b1PrevState = b1State;
Tack![b skrev:Zkronk[/b] post_id=1583708 time=1609960737 user_id=1387]
Du behöver hålla reda på föregående tillstånd på knapparna för att kunna få det beteendet.
Hur hanterar du kontaktavstudsning på knapparna? Gör du det i hårdvara eller ska du implementera det i mjukvaran?
Kod: Markera allt
if (s1angle == s1minAngle || s1angle == s1maxAngle)
Kod: Markera allt
s1speed = -s1speed;
eller t.ex.
b1Pushed = !b1Pushed;
Kod: Markera allt
s1speed = s1speed*(-1);
eller t.ex.
if (b1Pushed) b1Pushed = 0;
else b1Pushed = 1;
Kod: Markera allt
if (s1angle <= s1minAngle || s1angle >= s1maxAngle)
Kod: Markera allt
delay(10);
Kod: Markera allt
// Button 1
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 pushed
s1angle = s1angle + s1speed; // Changes the value of the 1st angle variable
if (s1angle <= s1minAngle || s1angle >= s1maxAngle) { // Checks where the 1st servomotor's angle is to drive it the opposite direction
s1speed = -s1speed;
b1Pushed = 0; // Tells the program that the 1st button is no longer pushed
}
s1.write(s1angle); // Moving the 1st servo to the defined value of s1angle
Serial.print("Moved servo 1: "); // Print text in serial monitor
Serial.print(s1angle); // Prints angle of servo 1
Serial.println(" degrees"); // Print text in serial monitor
delay(10); // Creates a delay to wait for servo 1 to get in place before looping on
}
Kod: Markera allt
// If the output buffer is full, there's nothing for it other than to
// wait for the interrupt handler to empty it a bit
Kod: Markera allt
int s1countUpDown = 0; // set to -1 when counting down, set to 1 when counting up;
// Button 1
if (digitalRead(b1Pin) == LOW) { // Checks if button 1 is pushed down
s1Moving = 1; // Set value to 1 if above is true
}
if (s1Moving ) { // Asks if servo1 need to move
s1angle = s1angle + s1speed * s1countUpDown ; // Changes the value of the 1st angle variable
// following checks makes it idiotproof
if (s1angle < s1minAngle)
s1angle = s1minAngle;
else if (s1angle > s1maxAngle)
s1angle = s1maxAngle;
if (s1angle == s1minAngle)
{
s1countUpDown = 1;
s1.write(s1maxAngle); // Moving the 1st servo to the end
Serial.print("Movin servo 1 to the max"); // Print text in serial monitor
s1Moving = 0;
}
else if (s1angle == s1maxAngle)
{
s1countUpDown = -1;
s1.write(s1minAngle); // Moving the 1st servo to the beginning
Serial.print("Movin servo 1 to the max"); // Print text in serial monitor
s1Moving = 0;
}
// using s1speed as a factor to this delay
delay(s1speed); // Creates a delay to wait for servo 1 to get in place before looping on
}