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;
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);
  Serial.begin(115200);
  pinMode(pin, INPUT);
}

void loop() {
  //unsigned long T;  // Periodendauer in us
  //double f;         // Frequenz in MHz
  //vmax = 0;         //

  for (int i = 0; i < 5; i++) {
    ontime = pulseIn(pin, HIGH);
    Serial.print("ontime : ");
    Serial.println(ontime);

    speedarray[i] = ontime;
    delay(250);
  }

  sortArray(speedarray, 5);
  mitte = speedarray[2];
  Serial.println(mitte);

  if(mitte > 0 && mitte <= 25000){  
  v = (mitte / T) * 100;
  }
  else {
  v = 0.0;
  }
  
  Serial.print("Sie fahren :");
  Serial.println(round(v));

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

void switchCase() {
  int LINKS = 0;
  int RECHTS = 0;
  int Rot = 150;
  int Gruen = 0;

  LINKS = round(v) / 10;
  RECHTS = round(v) % 10;

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

  if (LINKS <= 3 && RECHTS <= 9) {
    Rot = 0;
    Gruen = 150;
  }
  if (LINKS >= 3 && RECHTS >= 0) {
    Rot = 150;
    Gruen = 0;
  }

  switch (LINKS) {
    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) {
    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