PaulS:
In any case, POST YOUR CODE!
Here's the full code. Also see the video afterwards of this code running.
I forgot to get some slow acceleration/deceleration to show the resolution problem.
Info is jumbled together just for testing, I'm waiting on more parts in the mail to expand my display.
Speed is on left (3 digits) then gear (1 digit) then RPM (4 digits)
//X-Sim details
RPM = ~a01~
8 bit resolution
dezimal output
MPH = ~a02~
8 bit resolution
dezimal output
Gear = ~a03~
8 bit resolution
dezimal output
comport speed 115200
Pause of 20ms
R~a01~S~a02~G~a03~
//ARDUINO CODE
/*
rFactor Interface
Kelvyn Panici - 2011
Current X-Sim Limits:
Tachometer 0-8012RPM
Speed 0-255MPH
Gear 1-5, N, R
Current Hardcoded Values:
RPM = 8012 (See line 59)
Speed = 255MPH (See line 132)
Green Shiftlights = 7650
Red Shiftlights = 7725
White Shiftlights = 7825
*/
#include <TM1638.h>
#define RED TM1638_COLOR_RED
#define GREEN TM1638_COLOR_GREEN
TM1638 display1(22,23,24); //for Arduino Mega
//Clock -> Pin 23 (White)
//DIO -> Pin 22 (Yellow)
//STB0 -> Pin 24 (Green)
//TM1638 display1(2,3,4); //for Arduino Nano
//Clock -> Pin 3 (White)
//DIO -> Pin 2 (Yellow)
//STB0 -> Pin 4 (Green)
char kind_of_data;
boolean slStatusG=0; //Used to trigger shiftlights on and off. They only update once instead of repeatly.
boolean slStatusR=0; //Used to trigger shiftlights on and off.
long previousSLtime = 0;
void setup(){
pinMode(48, OUTPUT);
pinMode(49, OUTPUT);
pinMode(50, OUTPUT);
pinMode(51, OUTPUT);
digitalWrite(48, HIGH);
digitalWrite(49, HIGH);
digitalWrite(50, HIGH);
digitalWrite(51, HIGH);
Serial.begin(115200);
display1.setupDisplay(true,1);
}
void loop(){
//****************************** READ DATA FROM SERIAL **********************************
while (Serial.available() > 0){
kind_of_data = Serial.read();
if (kind_of_data == 'R' ) Read_Rpm();
if (kind_of_data == 'S' ) Read_Speed();
if (kind_of_data == 'G' ) Read_Gear();
}
}
//****************************** READ DATA FROM SERIAL END ******************************
//****************************** RPM DISPLAY CONTROL ************************************
void Read_Rpm(){
int RPM = 0;
delay(1);
int Rpm100 = Serial.read();
delay(1);
int Rpm10 = Serial.read(); //These three lines read in each digit of the 0-255 RPM value.
delay(1);
int Rpm1= Serial.read();
Rpm100 = ((Rpm100)-48)*100;
Rpm10 = ((Rpm10)-48)*10; //AlexOki's conversions.
Rpm1 = ((Rpm1)-48)*1;
if (Rpm10 < 0 && Rpm1 < 0){Rpm100 = Rpm100/100;Rpm10 = 0;Rpm1 = 0;}
if (Rpm1 < 0){Rpm100 = Rpm100/10;Rpm10 = Rpm10/10;Rpm1 = 0;} //AlexOki's conversions.
int hisRPM = Rpm100+Rpm10+Rpm1; //Combines three digits back into a single integer.
RPM=((hisRPM-127)*62.5); //Converts 0-255 values into 0-8012RPM values.
int digit1 = (RPM/1000);
int digit2 = ((RPM/100)-(digit1*10)); //Splits RPM integer back into individual digits.
int digit3 = ((RPM/10)-(digit1*100)-(digit2*10));
int digit4 = (RPM-(digit1*1000)-(digit2*100)-(digit3*10));
display1.setDisplayDigit(digit1,4,false);
display1.setDisplayDigit(digit2,5,false); //Displays digits individually on TM1638 module.
display1.setDisplayDigit(digit3,6,false);
display1.setDisplayDigit(digit4,7,false);
//****************************** Shiftlight control ************************************
if (RPM>7650){ //Turns on Green LEDs for revs over 7650.
if (slStatusG==0){
display1.setLED(GREEN,0);
display1.setLED(GREEN,1);
display1.setLED(GREEN,2);
display1.setLED(GREEN,3);
}
slStatusG = 1;
}
else{
if (slStatusG==1){ //Turns them back off.
display1.setLED(0,0);
display1.setLED(0,1);
display1.setLED(0,2);
display1.setLED(0,3);
}
slStatusG = 0 ;
}
if (RPM>7725){ //Turns on Red LEDs for revs over 7825.
if (slStatusR==0)
display1.setLED(RED,4);
display1.setLED(RED,5);
display1.setLED(RED,6);
display1.setLED(RED,7);
slStatusR = 1;
}
else{
if (slStatusR==1) //Turns them back off.
display1.setLED(0,4);
display1.setLED(0,5);
display1.setLED(0,6);
display1.setLED(0,7);
slStatusR = 0 ;
}
unsigned long currentSLtime = millis();
if ((RPM>7825)&&(currentSLtime - previousSLtime > 100)){ //Turns on WHITE LEDs for revs over 7825. They also flash.
digitalWrite(48, LOW);
digitalWrite(49, LOW);
digitalWrite(50, LOW);
digitalWrite(51, LOW);
previousSLtime = currentSLtime;
}
else{ //Turns them back off.
digitalWrite(48, HIGH);
digitalWrite(49, HIGH);
digitalWrite(50, HIGH);
digitalWrite(51, HIGH);
}
}
//****************************** SPEED DISPLAY CONTROL ************************************
void Read_Speed(){
delay(1);
int Speed100 = Serial.read();
delay(1);
int Speed10 = Serial.read(); //These three lines read in each digit of the 0-255 value.
delay(1);
int Speed1= Serial.read();
Speed100 = ((Speed100)-48)*100;
Speed10 = ((Speed10)-48)*10; //AlexOki's conversions.
Speed1 = ((Speed1)-48)*1;
if (Speed10 < 0 && Speed1 < 0){Speed100 = Speed100/100;Speed10 = 0;Speed1 = 0;}
if (Speed1 < 0){Speed100 = Speed100/10;Speed10 = Speed10/10;Speed1 = 0;} //AlexOki's conversions.
int hisSpeed = Speed100+Speed10+Speed1; //Combines three digits back into a single integer.
int Speed=((hisSpeed-127)*1.992/1.609344); //Converts 0-255 values into MPH values.
int sDigit1 = Speed/100;
int sDigit2 = Speed/10 - sDigit1*10; //Splits Speed integer back into individual digits
int sDigit3 = Speed - sDigit1*100 - sDigit2*10;
if (sDigit1==0) //First-digit-zero handling for speedmeter.
display1.clearDisplayDigit(0, false);// Prevents first digit from sticking.
else
display1.setDisplayDigit(sDigit1,0,false);
if ((sDigit1==0)&&(sDigit2==0)) //Second-digit-zero handling for speedmeter.
display1.clearDisplayDigit(1, false); // Prevents second digit from sticking.
else
display1.setDisplayDigit(sDigit2,1,false);
display1.setDisplayDigit(sDigit3,2,false); //Displays 3rd digit individually on TM1638 module.
}
//****************************** GEAR DISPLAY CONTROL ************************************
void Read_Gear(){
int Gear = 0;
delay(1);
int Gear100 = Serial.read();
delay(1);
int Gear10 = Serial.read(); //These three lines read in each digit of the 0-255 Gear value.
delay(1);
int Gear1= Serial.read();
Gear100 = ((Gear100)-48)*100;
Gear10 = ((Gear10)-48)*10; //AlexOki's conversions.
Gear1 = ((Gear1)-48)*1;
if (Gear10 < 0 && Gear1 < 0){Gear100 = Gear100/100;Gear10 = 0;Gear1 = 0;} //AlexOki's conversions.
if (Gear < 0){Gear100 = Gear100/10;Gear10 = Gear10/10;Gear1 = 0;}
Gear = Gear100+Gear10+Gear1; //Combines three digits back into a single integer.
switch (Gear) { //Switch that displays correct gear on TM1638
case 102:{
//byte values[] = { 0, 0, 0, 80, 0, 0, 0, 0 }; //Reverse Gear. Working with TM1638 developer to sort out display of this.
//display1.setDisplay(values);
//display1.setDisplayDigit(0b01010000,3,false,FONT_DEFAULT);
}
break;
case 127:{ //Neutral Gear. Working with TM1638 developer to sort out display of this.
//byte values[] = { 0, 0, 0, 84, 0, 0, 0, 0 };
//display1.setDisplay(values);
//display1.setDisplayDigit(0b01010100,3,false,FONT_DEFAULT);
}
break;
case 153: //First gear
display1.setDisplayDigit(1,3,false);
break;
case 179: //Second gear
display1.setDisplayDigit(2,3,false);
break;
case 204: //Third gear
display1.setDisplayDigit(3,3,false);
break;
case 230: //Fourth gear
display1.setDisplayDigit(4,3,false);
break;
case 255: //Fifth gear
display1.setDisplayDigit(5,3,false);
break;
}
}