Letzte switch case bedingung wird nie ausgeführt

@michael80 nachgeschaut. [edit - User vertauscht]

#include <Adafruit_NeoPixel.h>
#include <Servo.h>

//..............Servo..............
Servo servoLaden;
Servo servoStart;
#define pinServoLaden      9
#define pinServoStart     10
//..............Schrittmotor..............
#define pinStepperDir     11
#define pinStepperStep    12
#define pinStepperEnable  13
//..............Taster..............
#define pinTasterStop      6
#define pinTasterStart     7
//..............NeoPixel LED..............
#define pinNeoPixel        1
enum LEDFARBE {GRUEN, ROT, ORANGE, BLAU, LEDAUS};
Adafruit_NeoPixel pixels(1, pinNeoPixel, NEO_GRB + NEO_KHZ800);
//..............endschalter..............
#define pinEndschalter     2
//..............IR sensor..............
#define pinIRLed           8
#define pinPhotoDiode     A0
//..............vibrator..............
#define pinVibrator        3

uint8_t checkTaster(bool delayEin = true, int delayTime = 500);


void setup()
{
  Serial.begin(9600);
  pinMode(pinTasterStart, INPUT_PULLUP);
  pinMode(pinTasterStop, INPUT_PULLUP);
  pinMode(pinStepperDir, OUTPUT);
  pinMode(pinStepperStep, OUTPUT);
  pinMode(pinStepperEnable, OUTPUT);
  pinMode(pinEndschalter, INPUT);
  pinMode(pinIRLed, OUTPUT);
  digitalWrite(pinIRLed, HIGH);
  pinMode(pinVibrator, OUTPUT);
  pixels.begin();
  pixels.setBrightness(20);    //0 bis 255
  pixels.show();
  initialMotorServo();
}

void loop()
{
  static enum ezustand {AUS, EIN, AUSRICHTEN, VIBRIEREN, STEPPER, LADEN, STOPFEN, FEHLER} zustand = AUS;
  static bool OneCycle = false;
  uint8_t tmpTasterPressed = 0;
  uint8_t returnStopfMaschine = 0;
  //tvibrieren();
  switch (zustand)
  {
    case AUS:
      tmpTasterPressed = checkTaster();
      if (tmpTasterPressed == pinTasterStop)
      {
        zustand = AUSRICHTEN;
      }
      else if (tmpTasterPressed == pinTasterStart)
      {
        zustand = EIN;
      }
      else
      {
        setNeoPixel(ROT);
      }
      break;
    case EIN:
      if (checkTaster() == pinTasterStop)
      {
        if (OneCycle)
        {
          zustand = FEHLER;
        }
        else
        {
          OneCycle = true;
          setNeoPixel(ORANGE);
        }
      }
      else
      {
        OneCycle = false;
        setNeoPixel(GRUEN);
      }
      if (zustand != FEHLER)
      {
        zustand = VIBRIEREN;
      }
      break;
    case AUSRICHTEN:
      if (checkTaster(false) == pinTasterStop)
      {
        BlinkLED(250, BLAU);  //Ausrichten
        runStepper(true);
      }
      else
      {
        zustand = AUS;
      }
      break;
    case VIBRIEREN:
      vibrierenEin(4000);
      zustand = STEPPER;
      break;
    case STEPPER:
      tmpTasterPressed = checkTaster();
      if (tmpTasterPressed == pinTasterStop)
      {
        if (OneCycle)
        {
          zustand = FEHLER;
        }
        else
        {
          OneCycle = true;
          setNeoPixel(ORANGE);
        }
      }
      else if (tmpTasterPressed == pinTasterStart)
      {
        OneCycle = false;
        setNeoPixel(GRUEN);
      }
      if (zustand != FEHLER)
      {
        if (runStepper(false))
        {
          zustand = LADEN;  //Schrittmotor 45Grad drehen
        }
      }
      break;
    case LADEN:
      tmpTasterPressed = checkTaster();
      if (tmpTasterPressed == pinTasterStop)
      {
        if (OneCycle)
        {
          zustand = FEHLER;
        }
        else
        {
          OneCycle = true;
          setNeoPixel(ORANGE);
        }
      }
      else if (tmpTasterPressed == pinTasterStart)
      {
        OneCycle = false;
        setNeoPixel(GRUEN);
      }
      if (zustand != FEHLER)
      {
        if (runServo(2, 154, servoLaden))   //Zigihülse reinstecken
        {
          if (checkIR())  //Prüfen ob die Hülse richtig drauf ist
          {
            zustand = STOPFEN;
          }
          else
          {
            zustand = FEHLER;
          }
        }
      }
      break;
    case STOPFEN:
      tmpTasterPressed = checkTaster();
      if (tmpTasterPressed == pinTasterStop)
      {
        if (OneCycle)
        {
          zustand = FEHLER;
        }
        else
        {
          OneCycle = true;
          setNeoPixel(ORANGE);
        }
      }
      else if (tmpTasterPressed == pinTasterStart)
      {
        OneCycle = false;
        setNeoPixel(GRUEN);
      }
      //rückgabe wert wird in eine var. geschrieben damit die funktion nur einmal pro durchlauf ausgeführt wird
      returnStopfMaschine = StopfMaschine();
      if (zustand != FEHLER)
      {
        if (returnStopfMaschine == 0)
        {
          zustand = FEHLER;
        }
        else if (returnStopfMaschine == 2)
        {
          if (OneCycle)
          {
            zustand = AUS;
          }
          else
          {
            zustand = STEPPER;
          }
        }
      }
      break;
    case FEHLER:
      Serial.println("Fehler");
      BlinkLED(250, ROT);
      if (checkTaster() == pinTasterStop)
      {
        zustand = AUS;
      }
      break;
  }
  /*if(zustand == FEHLER)
    Serial.println("Fehler");
    {
      BlinkLED(250, ROT);
      if(checkTaster() == pinTasterStop)
      {zustand = AUS;}
    }*/
  Serial.println(zustand);
}


//rückgabe wert: 0 = fehler,  1 = noch nicht fertig, 2 = fertig
uint8_t StopfMaschine()
{
  static enum {AUS, EIN, START, STOPFEN, FEHLER} zustand = AUS;
  switch (zustand)
  {
    case AUS:
      if (checkEndschalter())
      {
        zustand = EIN;
      }
      else
      {
        zustand = FEHLER;
      }
      break;
    case EIN:
      if (runServo(2, 40, servoStart))    //An der Stopfmaschine start drücken
      {
        zustand = START;
      }
      break;
    case START:
      if (!checkEndschalter())
      {
        zustand = STOPFEN;
      }
      break;
    case STOPFEN:
      if (checkEndschalter())
      {
        zustand = AUS;
        return 2;
      }
      break;
    case FEHLER:
      zustand = AUS;
      return 0;
  }
  return 1;
}

void initialMotorServo()
{
  servoStart.attach(pinServoStart);
  servoLaden.attach(pinServoLaden);
  runServo(2, 10, servoLaden);  //sicher stellen damit der servo in der richtigen pos. ist
  runServo(2, 10, servoStart);  //sicher stellen damit der servo in der richtigen pos. ist
  digitalWrite(pinStepperEnable, LOW);
  digitalWrite(pinStepperDir, LOW);  //gegen den Uhrzeigersinn
  delay(10);
}

void setNeoPixel(LEDFARBE val)
{
  static LEDFARBE lastFarbe = LEDAUS;
  if (lastFarbe != val)
  {
    lastFarbe = val;
    switch (val)
    {
      case GRUEN:
        pixels.setPixelColor(0, pixels.Color(0, 255, 0));
        break;
      case ROT:
        pixels.setPixelColor(0, pixels.Color(255, 0, 0));
        break;
      case ORANGE:
        pixels.setPixelColor(0, pixels.Color(255, 140, 0));
        break;
      case BLAU:
        pixels.setPixelColor(0, pixels.Color(0, 0, 205));
        break;
      case LEDAUS:
        pixels.setPixelColor(0, pixels.Color(0, 0, 0));
        break;
    }
    pixels.show();
  }
}

void BlinkLED(unsigned long interval, LEDFARBE farbe)
{
  static bool LEDon = false;
  static unsigned long prevMillis = 0;
  if (millis() - prevMillis >= interval)
  {
    prevMillis = millis();
    if (LEDon)
    {
      LEDon = false;
      setNeoPixel(farbe);
    }
    else
    {
      LEDon = true;
      setNeoPixel(LEDAUS);
    }
  }
}


bool runStepper(bool ausrichten)
{
  static uint8_t LastStep = 1;
  if (!ausrichten)
  {
    digitalWrite(pinStepperStep, HIGH);
    delayMicroseconds(6000);
    digitalWrite(pinStepperStep, LOW);
    delayMicroseconds(6000);
    if (LastStep >= 100)  //Schrittmotor immer um 45 Grad drehen
    {
      LastStep = 1;
      return true;
    }
    else
    {
      LastStep += 1;
      return false;
    }
  }
  else
  {
    digitalWrite(pinStepperStep, HIGH);
    delay(30);
    digitalWrite(pinStepperStep, LOW);
    delay(30);
    return true;
  }
}

bool runServo(uint8_t startWinkel, uint8_t endWinkel, Servo &sObjekt)
{
  static int LastWinkel = -1;
  static bool Reverse = false;
  if (LastWinkel == -1)
  {
    LastWinkel = startWinkel;
  }
  if (LastWinkel >= endWinkel || Reverse)
  {
    Reverse = true;
    //In ausgangs position zurück drehen
    LastWinkel -= 1;
    sObjekt.write(LastWinkel);
    delay(3);
    if (LastWinkel <= startWinkel)
    {
      LastWinkel = -1;
      Reverse = false;
      return true;
    }
    else
    {
      return false;
    }
  }
  else
  {
    LastWinkel += 1;
    sObjekt.write(LastWinkel);
    delay(3);
    return false;
  }
}


uint8_t checkTaster(bool delayEIN, int delayTime)
{
  static unsigned long prevMillis = 0;
  if (millis() - prevMillis >= uint32_t(delayTime) || !delayEIN)
  {
    if (digitalRead(pinTasterStart) == LOW)
    {
      prevMillis = millis();
      return pinTasterStart;
    }
    else if (digitalRead(pinTasterStop) == LOW)
    {
      prevMillis = millis();
      return pinTasterStop;
    }
    else
    {
      return 0;
    }
  }
  else
  {
    return 0;
  }
}

bool checkEndschalter()
{
  if (digitalRead(pinEndschalter) == LOW)
  {
    return true;
  }
  else
  {
    return false;
  }
}

bool checkIR()
{
  if (analogRead(pinPhotoDiode) <= 685)
  {
    return true;
  }
  else
  {
    return false;
  }
}


unsigned long vibDauer = 0;
void vibrierenEin(int dauer)
{
  vibDauer = dauer + millis();
}

void tvibrieren()
{
  if (millis() <= vibDauer)
  {
    digitalWrite(pinVibrator, HIGH);
  }
  else
  {
    digitalWrite(pinVibrator, LOW);
  }
}

Du hast noch eine Baustelle mit dem

uint8_t checkTaster(bool delayEIN, int delayTime)

Ich hab da ein cast auf uint32_t gemacht, aber das geht besser.

1 Like