// X-sim3dashboard v1 (using TM1638 display and Arduino Nano v3)
// Made by TronicGr (Thanos) 4-26-2012 for X-sim3
// Shared as Public Domain
// Serial parser example: R~a01~~95~S~a02~G~a03~
// Where:
// ~a01~ is 16bit value for rpm
// ~95~ is data value parameter for RPM_MAX divided by 100 to fit into a byte so actual value is 9500
// ~a02~ is 16bit value for speed
// ~a03~ is 8bit value for gear / neutral / reverse
// You can set the USO pause safely to 10ms for nice fast refresh rates!
#include <TM1638.h> //can be downloaded from
code.google.com/p/tm1638-library/
// define a module on data pin 5, clock pin 4 and strobe pin 3
TM1638 module(5, 4, 3);
void setup() {
//Create Serial Object
Serial.begin(115200);
// initialize the screen:
module.clearDisplay(); //clears the display from garbage if any
String name = "Manuel"; //sets a custom logo start up banner
module.setDisplayToString(name); //prints the banner
delay(1500); //small delay 1.5 sec
module.clearDisplay(); //clears the display
}
void loop() {
int i;
char bufferArray[20]; // holds all serial data into a array
unsigned int rpm; //holds the rpm data (0-65535 size)
unsigned int rpmleds; //holds the 8 leds values
unsigned int rpmmax; //retrieves from x-sim USO this value as parameter divided by 100
unsigned int carspeed; //holds the speed data (0-65535 size)
byte gear; // holds gear value data
byte d1; // high byte temp variable
byte d2; // low byte temp variable
byte rpmdata = 0; // marker that new data are available
byte speeddata = 0; // marker that new data are available
byte geardata = 0; // marker that new data are available
if (Serial.available() >= 9) { //if 6 bytes available in the Serial buffer...
for (i=0; i<9; i++) { // for each byte
bufferArray
= Serial.read(); // put into array
}
}
if (bufferArray[0] == 'R' ){ // if new bytes have been recieved
d1 = bufferArray[1]; // store high byte of rpm
d2 = bufferArray[2]; // store low byte of rpm
rpm = ((d1<< + d2); // concatonate bytes (shift 8 bits)
rpmmax = bufferArray[3]; // retrieves the maxrpm value
rpmmax = (rpmmax * 100)+500; // multiplies the rpm data into thousants
rpmdata=1; // we got new data!
}
if (bufferArray[4] == 'S' ){
d1 = bufferArray[5]; // store high byte of speed
d2 = bufferArray[6]; // store low byte of speed
carspeed = ((d1<< + d2); // concatonate bytes (shift 8 bits)
speeddata=1; // we got new data!
}
if (bufferArray[7] == 'G' ){
gear = bufferArray[8]; // retrieves the single byte of gear (0-255 value)
geardata=1; // we got new data!
}
if (speeddata == 1) {
module.setDisplayToDecNumber(carspeed, 0, false); //displays numerical the speed
speeddata=0;
}
if (geardata == 1) {
char* neutral = "n"; // sets the character for neutral
char* reverse = "r"; // sets the character for reverse
gear = gear - 127; // offset the 0 value in 8-bit
if (gear >= 1 and gear <10 ){
module.setDisplayDigit(gear, 0, false); // displays numerical value of the current gear
}
if (gear == 0){
module.setDisplayToString(neutral, 0, 0); // displays the character for neutral
}
if (gear == 255){ // -1 that reprecents reverse rollover to 255 so...
module.setDisplayToString(reverse, 0, 0); // displays the character for reverse
}
geardata=0;
}
if (rpmdata == 1) {
rpmleds = map(rpm,0,rpmmax,0,9); // distributes the rpm level to the 8 leds + 1 for shift change
if (rpmleds==0){
module.setLEDs(0b00000000 | 0b00000000 << ;
}
if (rpmleds==1){
module.setLEDs(0b00000000 | 0b00000001<< 8 );
}
if (rpmleds==2){
module.setLEDs(0b00000000 | 0b00000011<< 8 );
}
if (rpmleds==3){
module.setLEDs(0b00000000 | 0b00000111<< 8 );
}
if (rpmleds==4){
module.setLEDs(0b00000000 | 0b00001111<< ;
}
if (rpmleds==5){
module.setLEDs(0b00000000 | 0b00011111 << ;
}
if (rpmleds==6){
module.setLEDs(0b00100000 | 0b00011111<< 8 );
}
if (rpmleds==7){
module.setLEDs(0b01100000 | 0b00011111<<8 );
}
if (rpmleds=={
//module.setLEDs(0b11100000 | 0b000011111<<8 );}
module.setLEDs(0b11111111 | 0b111111111<<8 );
}
rpmdata=0;
}
}