Tag Archives: tacho

BMW e36 Tacho sketch

Arduino Code für GameDash (XSimulator Motion Simulator)

unten stehender Code ist aus bestehenden Sketche adaptiert und verändert
(Technikprojekte, XSIMULATOR)

Alle funktionen des Tacho,  Ganganzeige, eine 8 stellige konfigurierbare LED-Anzeige für Rpm

funktioniert mit Mega2560, Uno und Nano
Die Pins müssen den entsprechenden Mikrocontroller angepasst werden.
#include 
#include 
char kind_of_data;
// shiftpoint #############
int ShiftPoint  = 0;   
int RpmSimTools = 6500;  // Schaltpunkt vorbelegen
int LimitRange  = 0;
int UnderShiftPoint;
int OverShiftPoint;
int OverShift;
int UnderShift;
int ButtonState;
int LastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay    = 5;
//LED rpm
int rpmLED = 0;
// 7segment Common Cathode 
int Gear;
int GearLight = 0;
int N1[9] = {63,6,91,79,102,109,124,7,80};  // 0 1 2 3 4 5 6 7 r
//arduino pins
int prpm = 2;      
int pkmh = 3;          
int ful  = 5;   // pwm 
int tmp  = 6;   // pwm 
int capture_button = 4;  //shift point capture button
int analog_adj_pot = 0;  // adjusting poti
//arduino spi pins nach 74HC595 (Shiftregister)
int latchpin = 10;   // blaues kabel >> RCLK   (Register clock)
int clockpin = 13;   // grünes kabel >> SRCLK  (Shift register clock)
int datapin  = 11;   // gelbes kabel >> SER    (Serial data input)
// Tone
Tone kmh;
Tone rpm;
 
void setup(){
  pinMode(latchpin, OUTPUT);  
  pinMode(ful, OUTPUT);  
  pinMode(tmp, OUTPUT); 
  pinMode(capture_button,INPUT);
   
  digitalWrite(latchpin, LOW);
 
  //Setup SPI
  SPI.setBitOrder(MSBFIRST);
  SPI.begin(); 
  
  //Setup TONE 
  kmh.begin(pkmh);
  rpm.begin(prpm);
 
  Serial.begin(115200);
  // Vorbelegung auf 50°c
  analogWrite(tmp,85); 
  // Vorbelegung auf 30 Ltr.
  analogWrite(ful,95); 
}
 
void CheckButtonPress() {   // shiftlight 
  int reading = digitalRead(capture_button);
  if (reading != LastButtonState){
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != ButtonState){
      ButtonState = reading;
      if (ButtonState == HIGH){
        ShiftPoint = RpmSimTools;
      }
    }
  }
  LastButtonState = reading;
}
 
void CheckShiftLightAdj() {  // shiftlight nachjustieren
  LimitRange = analogRead(analog_adj_pot);
  int RpmAdjust = map(LimitRange, 1, 1023, 0, 2000);
  UnderShift = RpmAdjust;
  OverShift = RpmAdjust;
}
 
void SetLimits() {  // shiftlight
  UnderShiftPoint = ShiftPoint - UnderShift;
  OverShiftPoint = ShiftPoint + OverShift;
}
  
void ReadData(){
if(Serial.available() > 0) {
    kind_of_data = Serial.read();
    delay(1);
    int Data100 = Serial.read() - '0';
    delay(1);
    int Data10 = Serial.read()- '0';
    delay(1);
    int Data1 = Serial.read()- '0';
 
    //Daten anpassen: aus 1 wird 001, aus 10 wird 010 etc.
    while (Data1 < 0) {
      Data1 = Data10;
      Data10 = Data100;
      Data100 = 0;
    }
 
    int Data = 100*Data100 + 10*Data10 + Data1;
      //  Speed
      if (kind_of_data == 'S') {
      int Speed = map(Data,0,255,0,318);
      //  Gauge begrenzen
       if(Speed > 318) {
        Speed = 318;
        kmh.play(Speed);
       }
       else if((Speed <= 318) && (Speed >= 25)) {
        kmh.play(Speed);
       }
       else if(Speed < 25) {
        kmh.stop();
       }
      } 
 
      // Drehzahl      
      else if (kind_of_data == 'R') {
       RpmSimTools = Data*10;
       int Drehzahl = map(Data,0,700,0,350);
    
       if(Drehzahl > 350) {
        Drehzahl = 350;
       }
       if(Drehzahl < 025) {
        Drehzahl = 000;
       }
       rpm.play(Drehzahl);
     }
 
      // Gang 
      else if (kind_of_data == 'G') {
       Gear = Data;
 
       if (Gear == 9) {   // rückwärts
        GearLight = N1[8];
       } 
       if (Gear == 0) {   // neutral
        GearLight = N1[0];
       } 
       if (Gear == 1) {   // 1 gang
        GearLight = N1[1];  
       } 
       if (Gear == 2) {   // 2 gang
        GearLight = N1[2];
       }  
       if (Gear == 3) {   // 3 gang
        GearLight = N1[3];
       } 
       if (Gear == 4) {   // 4 gang
        GearLight = N1[4];
       } 
       if (Gear == 5) {   // 5 gang
        GearLight = N1[5];
       }
       if (Gear == 6) {   // 6 gang
        GearLight = N1[6];
       }
       if (Gear == 7) {   // 7 gang
        GearLight = N1[7];
       }
      }
 
      //Tankinhalt 
      else if (kind_of_data == 'F') {
       int Tankinhalt = map(Data,0,65,13,155);
 
       if(Tankinhalt > 155) {
        Tankinhalt = 155;
       }
       if(Tankinhalt < 13) {
        Tankinhalt = 13;
       }
       analogWrite(ful,Tankinhalt);      
     }
 
      //Temperatur 
      else if (kind_of_data == 'T') {
       int Temperatur = map(Data,0,140,140,0);
       analogWrite(tmp,Temperatur);             
      }
 
    DisplayLeds();    
   }
}
 
void DisplayLeds(){  
       
   if (RpmSimTools < UnderShiftPoint *0.25){     
    rpmLED=0;    
   }       
   
   if (RpmSimTools >= UnderShiftPoint *0.25){
    rpmLED=1; 
   }
   
   if (RpmSimTools >= UnderShiftPoint *0.5){
    rpmLED=3;  
   }
 
   if (RpmSimTools >= UnderShiftPoint *0.75){
    rpmLED=7;  
   }
   
   if (RpmSimTools >= UnderShiftPoint){
    rpmLED=15;  
   }
    
   if (RpmSimTools >= ShiftPoint -500){
    rpmLED=31;   
   }

   if (RpmSimTools >= ShiftPoint){
    rpmLED=63;   
   }
  
   if (RpmSimTools >= OverShiftPoint -500){
    rpmLED=127;
   } 
  
   if (RpmSimTools >= OverShiftPoint){
    rpmLED=255;
   }
 
  digitalWrite(latchpin, HIGH);                       
  SPI.transfer(GearLight);    // Ganganzeige
  SPI.transfer(rpmLED);       // LED's Drehzahl
  digitalWrite(latchpin, LOW); 
}   
 
void loop(){ 
  CheckButtonPress();
  CheckShiftLightAdj();
  SetLimits();
  ReadData(); 
}

Die passende GameDash konfiguration findet ihr >> hier <<

12
WP2Social Auto Publish Powered By : XYZScripts.com