Programmierung

Aus HSHL Mechatronik
Zur Navigation springen Zur Suche springen

Programmcode

//----------------------------------------------//
//         Speedy-Tempomessgerät                //
//           Projektarbeit                      //
//       Justin Frommberger & Jonas Gerke       //
//          Stand: 29.11.2022                   //
//--------------------------------------------- //


#include <FastLED.h>
#include <ArduinoSort.h>

#define LED_PIN 7
#define NUM_LEDS 257

CRGB leds[NUM_LEDS];

int DELAYVAL = 400;

float v = 0;                  //Variablen Deklaration
float ontime, offtime;
int pin = 5;
float mitte = 0;
int T = 25000;
float speedarray[5];

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);   //LEDs Deklarieren
  Serial.begin(115200);
  pinMode(pin, INPUT);
}

void loop() {

  for (int i = 0; i < 5; i++) {       //Messung wie lange der Sensor ein High Signal sendet
    ontime = pulseIn(pin, HIGH);

    speedarray[i] = ontime;           // Messungen in Array Speichern
    delay(250);
  }

  sortArray(speedarray, 5);          // Messungen nach Größe sortieren
  mitte = speedarray[2];             // Median-Wert herausfinden

  if(mitte > 0 && mitte <= 25000){   // Sicherstellen das nur positive Zahlen verwendet werden ansonsten Geschwindigkeit = 0 KM/H
  v = (mitte / T) * 100;
  }
  else {
  v = 0.0;
  }

  // LED ausführen
  switchCase();        // switchCase Methode ausführen
  FastLED.show();      // LEDs Starten
  delay(DELAYVAL);
}

void switchCase() {
  int LINKS = 0;     
  int RECHTS = 0;
  int Rot = 150;     //Deklaration um LEDs Rot oder Gruen anzuzeigen
  int Gruen = 0;

  LINKS = round(v) / 10;   // linke Ziffer herausfinden
  RECHTS = round(v) % 10;  // rechte Ziffer herausfinden

  for (int i = 0; i <= 255; i++) {
    leds[i] = CRGB(0, 0, 0);
  }

  if (LINKS <= 3 && RECHTS <= 9) {   // Geschwindigkeiten unter 30 KM/H werden Gruen angezeigt
    Rot = 0;
    Gruen = 150;
  }
  if (LINKS >= 3 && RECHTS >= 0) {   // Geschwindigkeiten über 30 KM/H werden Rot angezeigt
    Rot = 150;
    Gruen = 0;
  }

  switch (LINKS) {                   // Linke Zahl
    case 0:
      for (int i = 0; i <= 55; i++) {
        leds[i] = CRGB(0, 0, 0);
      }
      for (int i = 72; i <= 125; i++) {
        leds[i] = CRGB(0, 0, 0);
      }
      break;
    case 1:
      for (int i = 88; i <= 125; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 2:
      for (int i = 10; i <= 29; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 40; i <= 86; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 97; i <= 115; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 3:
      for (int i = 40; i <= 86; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 87; i <= 125; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 4:
      for (int i = 0; i <= 9; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 30; i <= 39; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 56; i <= 71; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 88; i <= 125; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 5:
      for (int i = 0; i <= 9; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 30; i <= 87; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 88; i <= 96; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 116; i <= 125; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 6:
      for (int i = 0; i <= 96; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 116; i <= 125; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 7:
      for (int i = 40; i <= 55; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 88; i <= 125; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 8:
      for (int i = 0; i <= 125; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 9:
      for (int i = 0; i <= 9; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 30; i <= 39; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 40; i <= 125; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
  }

  switch (RECHTS) {                     // Rechte Zahl
    case 0:
      for (int i = 127; i <= 182; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 199; i <= 254; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 1:
      for (int i = 215; i <= 254; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 2:
      for (int i = 137; i <= 156; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 167; i <= 213; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 225; i <= 244; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 3:
      for (int i = 167; i <= 213; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 214; i <= 254; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 4:
      for (int i = 127; i <= 136; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 157; i <= 166; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 183; i <= 198; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 215; i <= 254; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 5:
      for (int i = 127; i <= 136; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 157; i <= 214; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 215; i <= 224; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 245; i <= 254; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 6:
      for (int i = 127; i <= 224; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 245; i <= 254; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 7:
      for (int i = 167; i <= 182; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 215; i <= 254; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 8:
      for (int i = 127; i <= 254; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
    case 9:
      for (int i = 127; i <= 136; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 157; i <= 166; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      for (int i = 167; i <= 254; i++) {
        leds[i] = CRGB(Rot, Gruen, 0);
      }
      break;
  }
  delay(250);
  mitte = 0;
}


→ zurück zum Hauptartikel: Projektarbeit Speedy Tempomessgerät - 2022