Hallo zusammen.
mein Problem ist es , dass immer nur die erste If-Abfrage unter my.serial.available() abgerufen wird.
Also nur wenn der Empfänger eine 1 empfängt funktioniert alles. Aber wenn eine 2 empfangen wird, passiert nichts. Wenn ich aber die if Abfrage mit der Bedingung für den Wert 2 nach oben schiebe funtkioniert es.
Woran liegt das? Habe ich zu viele If Abfragen?
#include <MobaTools.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(12, 13);
const byte LED_LINEAR_HOCH = 8;
const byte LED_LINEAR_RUNTER = 9;
const byte LED_ROTATION_AUSROLLEN = 10;
const byte LED_ROTATION_EINROLLEN = 11;
boolean STOPWERT = false;
boolean ROTATIONSRICHTUNG = false;
enum : byte { WARTEN, STARTEN, LINHOCH, ROTATION, LINRUNTER, STOPPED };
byte motorState = WARTEN;
byte motorStopState = WARTEN;
byte motorSelectState = LED_LINEAR_HOCH;
MoToTimer laufZeit;
long stopZeit = 0;
long linearDauer = 8000;
long rotationDauer = 8000;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
mySerial.begin(9600);
while ( !mySerial);
pinMode(LED_LINEAR_HOCH, OUTPUT);
pinMode(LED_LINEAR_RUNTER, OUTPUT);
pinMode(LED_ROTATION_AUSROLLEN, OUTPUT);
pinMode(LED_ROTATION_EINROLLEN, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (mySerial.available()) {
if ( mySerial.read() == '1' && STOPWERT == false ) // Ganz normaler Ablauf der Schrittkette beim Ausrollen
{
ROTATIONSRICHTUNG = false; // Um zu entscheiden welcher Motor auf 'HIGH' gesetzt wird. Hier MOTOR 'AUSROLLEN'
motorState = STARTEN;
}
else if ( mySerial.read() == '1' && STOPWERT == true ) // Schrittkette wurde unterbrochen und muss auf den letzten Zustand zurückgesetzt werden
{
STOPWERT = false;
ROTATIONSRICHTUNG = false; // Um zu entscheiden welcher Motor auf 'HIGH' gesetzt wird. Hier MOTOR 'AUSROLLEN'
motorState = motorStopState;
digitalWrite(motorSelectState, HIGH ); //Letzter Motor wird wieder gestartet
laufZeit.setTime(stopZeit); // Neue Laufzeit wird gestartet
}
if ( mySerial.read() == '2' && STOPWERT == false ) // Ganz normaler Ablauf der Schrittkette beim Einrollen
{
ROTATIONSRICHTUNG = true; // Um zu entscheiden welcher Motor auf 'HIGH' gesetzt wird. Hier MOTOR 'EINROLLEN'
motorState = STARTEN;
}
else if ( mySerial.read() == '2' && STOPWERT == true ) // Schrittkette wurde unterbrochen und muss auf den letzten Zustand zurückgesetzt werden
{ // Um zu entscheiden welcher Motor auf 'HIGH' gesetzt wird. Hier MOTOR 'EINROLLEN'
STOPWERT = false;
ROTATIONSRICHTUNG = true;
motorState = motorStopState;
digitalWrite(motorSelectState, HIGH ); //Letzter Motor wird wieder gestartet
laufZeit.setTime(stopZeit); // Neue Laufzeit wird gestartet
}
if ( mySerial.read() == '3' ) // Stop-Taste wurde gedrückt
{
motorStopState = motorState; // merken des aktuellen Zustands
motorState = STOPPED; // Zustand STOPPED wird aufgerufen
STOPWERT = true; // Es wurde gestoppt
stopZeit = laufZeit.getTime(); // Restlaufzeit für den Motor
}
}
switch ( motorState ) {
case WARTEN:
// auf Kommando warten
break;
case STARTEN:
laufZeit.setTime( linearDauer );
digitalWrite( LED_LINEAR_HOCH , HIGH ); // Linearmotor starten
motorSelectState = LED_LINEAR_HOCH;
motorState = LINHOCH;
break;
case LINHOCH:
if ( laufZeit.expired() ) {
// Laufzeit Linearmotor abgelaufen
digitalWrite( LED_LINEAR_HOCH , LOW ); // Linearmotor stoppen
if ( ROTATIONSRICHTUNG == false )
{
digitalWrite( LED_ROTATION_AUSROLLEN , HIGH ); // Rotationsmotor_AUSROLLEN starten
motorSelectState = LED_ROTATION_AUSROLLEN;
}
if ( ROTATIONSRICHTUNG == true)
{
digitalWrite( LED_ROTATION_EINROLLEN , HIGH ); // Rotationsmotor_EINROLLEN starten
motorSelectState = LED_ROTATION_EINROLLEN;
}
laufZeit.setTime( rotationDauer );
motorState = ROTATION;
}
break;
case ROTATION:
if ( laufZeit.expired() ) {
// Laufzeit Rotationsrmotor abgelaufen
digitalWrite( LED_ROTATION_AUSROLLEN , LOW ); // Rotationsmotor_AUSROLLEN stoppen
digitalWrite( LED_ROTATION_EINROLLEN , LOW ); // Rotationsmotor_EINROLLEN stoppen
digitalWrite( LED_LINEAR_RUNTER , HIGH ); // Linearmotor abwärtsstarten
laufZeit.setTime( linearDauer );
motorSelectState = LED_LINEAR_RUNTER;
motorState = LINRUNTER;
}
break;
case LINRUNTER:
if ( laufZeit.expired() ) {
// Laufzeit Linearmotor abgelaufen
digitalWrite( LED_LINEAR_RUNTER , LOW ); // Linearmotor stoppen
motorState = WARTEN;
}
break;
case STOPPED: // Stop Tase wurde gedrückt
digitalWrite(LED_LINEAR_HOCH, LOW); // Aussschalten aller Antriebe
digitalWrite(LED_LINEAR_RUNTER, LOW);
digitalWrite(LED_ROTATION_AUSROLLEN, LOW);
digitalWrite(LED_ROTATION_EINROLLEN, LOW);
motorState = WARTEN;
}
}