Moin,
ich habe vor einiger Zeit hier mal eine Anfrage gestellt, für mein Kinder Kart.
Dort wurde mir von @agmue super weitergeholfen, bzw er hat fast alles umgesetzt.
Nun möchte ich gern noch was ändern, stehe aber leider etwas aufm Schlauch.
Ich habe dort noch Änderungen vorgenommen. ( Blinker, Licht, jetzt auch ne Bremse.)
Hier ist der geschlossene Ursprungsport Kinder Kart Motor Softstart - #49 by agmue
Hier der Aktuelle Code.
#define DEBUGGING // Einkommentieren für die Serielle Ausgabe
#ifdef DEBUGGING
#define DEBUG_B(...) Serial.begin(__VA_ARGS__)
#define DEBUG_P(...) Serial.print(__VA_ARGS__)
#define DEBUG_L(...) Serial.println(__VA_ARGS__)
#else
#define DEBUG_B(...)
#define DEBUG_P(...)
#define DEBUG_L(...)
#endif
#include <Noiasca_led.h> // ab Version 0.0.6 von https://werner.rothschopf.net/microcontroller/202202_tools_led_en.htm
#include <Servo.h>
const byte LICHT = 2; // Licht einschalten
const byte DIR = 4; // VOR / ZURÜCK
const byte EIN = 5; // Einschalten
const byte MOTOR_PWM = 6; // an PWM
const byte V_EN = 7; // vorwärts Freigabe; an RPWM oder LPWM
const byte R_EN = 0; // rückwärts Freigabe; an LPWM oder RPWM
const byte BEEP = 8; // Rückwärtsfahrsignal
const byte SPEED_PWM = 9; // Blinker takt
const byte LICHT_EIN = 10; // Licht ausgang
const byte BLINKER = 11; // Blinker takt
const byte SLOW = 12; // Langsamfahrt
const byte POTI = A0; // Gaspedal
const byte POTI_S = A1; // Begrenzer
int bremse = A2; // Analog Pin, an dem das Potentiometer angeschlossen ist
int val; // Variable um den Wert des Analogen Pin zwischenzuspeichern
BlinkPin beep {BEEP};
BlinkPin blinker {BLINKER};
BlinkPin speed_pwm {SPEED_PWM};
SmoothPin motor {MOTOR_PWM}; // UNO PWM pins 3, 5, 6, 9, 10, 11
Servo myservo; // erstellt ein Servo-Objekt, um einen Servomotor zu steuern
void setup() {
DEBUG_B(115200);
DEBUG_L("\nStart");
pinMode(DIR, INPUT_PULLUP);
pinMode(SLOW, INPUT_PULLUP);
pinMode(LICHT, INPUT_PULLUP);
pinMode(V_EN, OUTPUT);
pinMode(R_EN, OUTPUT);
pinMode(EIN, OUTPUT);
pinMode(LICHT_EIN, OUTPUT);
myservo.attach(3); // verknüpft den Servomotor an Pin 3 mit dem Servo-Objekt
motor.begin(); // you have to call the .begin() method for the LED pair
motor.setMaxBrightness(0); // you can limit the maximum brightness. Highest value on Arduino is 255. Default 255.
motor.off(); // you can switch the LED off
motor.setOnInterval(80); // you can modify the delay time for the smothing
motor.setOffInterval(70); // you can modify the delay time for the smothing
beep.begin(); // you have to call the .begin() method for this LED
beep.setOnInterval(200); // 200ms on
beep.setOffInterval(600); // 600ms off
beep.off(); // you can switch the LED off
blinker.begin(); // you have to call the .begin() method for this LED
blinker.setOnInterval(1000); // 200ms on
blinker.setOffInterval(1000); // 600ms off
blinker.on(); // you can switch the LED off
speed_pwm.begin(); // you have to call the .begin() method for this LED
speed_pwm.setOnInterval(0); // 200ms on
speed_pwm.setOffInterval(0); // 600ms off
speed_pwm.on(); // you can switch the LED off
}
void fahren(const uint8_t EN_L, const uint8_t EN_H, const uint8_t MAX)
{
const int32_t POTI_MIN = 200; // Analogwert bei Pedal losgelassen
const int32_t POTI_MAX = 840;
const int32_t POTI_SLOW = 19; // Analogwert bei Pedal durchgetreten
int32_t poti = analogRead(POTI);
int32_t poti_s = analogRead(POTI_S); // Typ ist für nachfolgende Rechnung wichtig
uint8_t geschw = constrain(((poti - POTI_MIN) * MAX / (POTI_MAX - POTI_MIN)) - (poti_s / POTI_SLOW) , 0, MAX);
DEBUG_P(poti_s);
DEBUG_P('\t');
digitalWrite(EN_L, LOW);
speed_pwm.setOnInterval(poti_s + 30);
speed_pwm.setOffInterval(poti_s + 30);
motor.setMaxBrightness(geschw);
if ((MAX > 0) && (analogRead(bremse)< 250)) {
digitalWrite(EN_H, HIGH);
motor.on();
} else {
digitalWrite(EN_H, LOW);
motor.off();
}
}
void loop()
{
val = analogRead(bremse); // liest das Potentiometer aus (Wert zwischen 0 und 1023)
val = map(val, 210, 850, 20, 110); // rechnet den Wert in den Wertebereich des Servomotors (zwischen 0 und180)
myservo.write(val); // überträgt die Zielposition an den Servomotors
delay(5); // lässt dem Servomotor Zeit, die Zielposition zu erreichen
blinker.update();
speed_pwm.update();
enum {VORWAERTS, RUECKWAERTS, LICHT};
const uint8_t MAX_VORWAERTS = 88; //128
const uint8_t MAX_RUECKWAERTS = 55;
const uint8_t MAX_VORWAERTS_SLOW = 50;
const uint8_t MAX_RUECKWAERTS_SLOW = 35;
static uint8_t schritt = 0;
{
if (!digitalRead (LICHT) ==HIGH) {
digitalWrite(LICHT_EIN, HIGH);
} else {
digitalWrite(LICHT_EIN, LOW);
}
}
switch (schritt)
{
case VORWAERTS:
if (digitalRead(DIR))
{
if (digitalRead(SLOW)) fahren(V_EN, R_EN, MAX_VORWAERTS_SLOW); else fahren(V_EN, R_EN, MAX_VORWAERTS);
if ( motor.getCurrentBrightness() > 0 ) digitalWrite(EIN, HIGH); else digitalWrite(EIN, LOW) ;
} else {
fahren(R_EN, V_EN, 0);
if ( motor.getCurrentBrightness() <= 1 ) schritt = 1;
}
break;
case RUECKWAERTS:
if (!digitalRead(DIR)) {
if (digitalRead(SLOW)) fahren(R_EN, V_EN, MAX_RUECKWAERTS_SLOW); else fahren(R_EN, V_EN, MAX_RUECKWAERTS);
if ( motor.getCurrentBrightness() > 0 ) beep.on(); else beep.off();
if ( motor.getCurrentBrightness() > 0 ) digitalWrite(EIN, HIGH); else digitalWrite(EIN, LOW);
} else {
fahren(R_EN, V_EN, 0);
beep.off();
if ( motor.getCurrentBrightness() <= 1 ) schritt = 0;
}
break;
}
DEBUG_L(motor.getCurrentBrightness());
motor.update(); // you have to call update() for the LED
beep.update();
}
Nun möchte ich gern, das wenn die Bremse betätigt wird, der Motor sofort eine Drehzahl von "0" hat. Jetzt läuft der Motor ja noch nach, das soll bei Betätigung der Bremse verhindert werden.
Ich hoffe mein Wusch/frage ist soweit verständlich.
Und ja, der Code ist sicherlich nicht sauber geschrieben, bzw verändert, aber funktioniert soweit.
Gruß Christian


