Dag Allemaal,
ik heb een sketch waarbij ik een DC-motortje link- en rechtsom kan laten draaien dmv een drukknop of een infrarood afstandsbediening.Tot zover werkt alles.
Nu wil ik hierbij een servomotor aan toevoegen. De code hiervan op zich werkt ook. Maar nu blijkt de DC-motor te sputteren, de potentiometer voor toerentalregeling werkt niet, en ik meet rare spanningen op de servo-output terwijl ik die niet uitstuur.
Ik heb gemerkt dat als ik de regel: "myServo.attach(3);" in comment zet, er geen probleem met dc-motor zijn.
Wij kan me verder helpen?
Alvast bedankt.
#include <Servo.h>
#include <IRremote.h>
long myTime = 0;
Servo myServo; // create a servo object
const int poolMotor = 5;
const int controlPin1 = 2; // connected to pin 7 on the H-bridge
const int controlPin2 = 4; // connected to pin 2 on the H-bridge
const int enablePin = 9; // connected to pin 1 on the H-bridge
const int btnFrontStage = 12; // connected to the switch for turning brushed motor on and off
const int btnDivingBoard = 8; // connected to the switch for turning diving board servo motor on
const int potPin = A0; // connected to the potentiometer's output
const int irReceivePin = 7; // connected to IRreceiver OUT
IRrecv irrecv(irReceivePin);
decode_results results;
// create some variables to hold values from your inputs
int onOffFrontstageState = 0; // current state of the on/off switch
int previousFrontStageState = 0; // previous position of the on/off switch
int directionSwitchState = 0; // current state of the direction switch
int previousDirectionSwitchState = 0; // previous state of the direction switch
int motorEnabled = 0; // Turns the motor on/off
int motorSpeed = 0; // speed of the motor
int motorDirection = 1; // current direction of the motor
int buttonPushCounter = 0; // counter for the number of button presses
int irResult = 0;
void setup() {
myServo.write(0);
myServo.attach(3); // <---- probleem als ik deze regel uncomment
// initialize the inputs and outputs
pinMode(btnDivingBoard, INPUT);
pinMode(btnFrontStage, INPUT);
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(poolMotor, OUTPUT);
pinMode(irReceivePin, INPUT);
irrecv.enableIRIn(); // Start the receiver
// pull the enable pin LOW to start
digitalWrite(enablePin, LOW);
myTime = millis();
Serial.begin(9600);
}
void loop() {
long key;
// read the value of the on/off switch
onOffFrontstageState = digitalRead(btnFrontStage);
delay(1);
// read the value of the pot and divide by 4 to get a value that can be
// used for PWM
motorSpeed = analogRead(potPin) / 4;
/* if (myTime + 2000 <= millis()) {
Serial.print("motorSpeed: ");
Serial.println(motorSpeed);
Serial.print("potPin: ");
Serial.println(analogRead(potPin));
Serial.print("seconds: ");
Serial.println(millis() / 1000);
myTime = millis();
}*/
// if the on/off button changed state since the last loop()
if (onOffFrontstageState != previousFrontStageState) {
// change the value of motorEnabled if pressed
if (onOffFrontstageState == HIGH) {
motorEnabled = !motorEnabled;
buttonPushCounter ++;
}
}
// if the direction button changed state since the last loop()
if (directionSwitchState != previousDirectionSwitchState) {
// change the value of motorDirection if pressed
if (directionSwitchState == HIGH) {
motorDirection = !motorDirection;
}
}
// change the direction the motor spins by talking to the control pins
// on the H-Bridge
if (motorDirection == 1) {
digitalWrite(controlPin1, HIGH);
digitalWrite(controlPin2, LOW);
} else {
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, HIGH);
}
if (irrecv.decode(&results))
{
irrecv.resume(); // Receive the next value
key = results.value;
Serial.println(key);
if (key == 16716015) //druk <
{
buttonPushCounter = 1;
}
else if (key == 16726215) //druk ok
{
buttonPushCounter = 2;
}
else if (key == 16734885) //druk >
{
buttonPushCounter = 3;
}
else if (key == 16736925) //druk 2
{
startDivingBoard();
}
}
switch (buttonPushCounter) {
case 1: // motor left
motorDirection = 1;
motorEnabled = 1;
break;
case 2:
motorEnabled = 0;
break;
case 3: // motor right
motorDirection = 0;
motorEnabled = 1;
break; // motor stop
default:
motorEnabled = 0;
buttonPushCounter = 0;
break;
}
// if the motor is supposed to be on
if (motorEnabled == 1) {
// PWM the enable pin to vary the speed
analogWrite(enablePin, motorSpeed);
} else { // if the motor is not supposed to be on
//turn the motor off
analogWrite(enablePin, 0);
}
// save the current on/off switch state as the previous
previousDirectionSwitchState = directionSwitchState;
// save the current switch state as the previous
previousFrontStageState = onOffFrontstageState;
}
void startDivingBoard() {
for (int i = 0; i <= 2; i++) {
myServo.write(45);
delay(400);
myServo.write(0);
delay(400);
}
delay(150);
digitalWrite(poolMotor, HIGH);
delay(200);
digitalWrite(poolMotor, LOW);
}
fleaCircus.ino (4.62 KB)