Tag Archives: scetch

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 <<

LCD mit I2C

Der Vorteil eines I2C-Moduls liegt klar auf der Hand… 2 statt 5 Kabel, somit sind mehr Pins frei.
Will man mehr als ein LCD ansteuern, kein Problem….. es bleiben 2 Kabel 🙂

Board I2C an pin:
Uno, Nano, Ethernet A4 (SDA), A5 (SCL)
Mega2560 20 (SDA), 21 (SCL)
Leonardo 2 (SDA), 3 (SCL)
Due 20 (SDA), 21 (SCL), SDA1, SCL1

#include 
#include 

LiquidCrystal_I2C lcd1(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified backpack
LiquidCrystal_I2C lcd2(0x20,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified backpack

void setup()
{
  // activate LCD 1 module
  lcd1.begin (16,2); // for 16 x 2 LCD module
  lcd1.setBacklightPin(3,POSITIVE);
  lcd1.setBacklight(HIGH);

  // activate LCD 2 module
  lcd2.begin (16,2); // for 16 x 2 LCD module
  lcd2.setBacklightPin(3,POSITIVE);
  lcd2.setBacklight(HIGH);
}

void loop()
{
  lcd1.home (); // set cursor to 0,0
  lcd2.home (); // set cursor to 0,0

  lcd1.print("LCD 1"); 
  lcd2.print("LCD 2");

  lcd1.setCursor (0,1);        // go to start of 2nd line
  lcd1.print(millis());
  lcd2.setCursor (0,1);
  lcd2.print(millis());
  delay(1000);
}
1
WP2Social Auto Publish Powered By : XYZScripts.com