Código:
/*
Programmed by PSArias
Thanks to tronicgr for the base code
http://x-sim.de
*/
#include <TM1638.h> //can be downloaded from http://code.google.com/p/tm1638-library/
#define RPMMAX 9500;
#define MAXFUEL 60;
// 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();
byte values[]={0,0,0,0,0,0,0,0};
byte value = 32;
for(int i=0; i<10; i++){
if(i<2) {
values[0]=value;
value/=2;
}
else if(i-2>0){
values[i-2]=value;
values[i-3]=0;
}
else
values[i-2]=value;
module.setDisplay(values);
delay(50);
}
value=4;
for(int i=9; i>=0; i--){
if(i>7) {
values[7]=value;
value/=2;
}
else if(i<7){
values[i]=value;
values[i+1]=0;
}
else
values[i]=value;
module.setDisplay(values);
delay(50);
}
value=32;
for(int i=0;i<2;i++){
values[0]=value;
value/=2;
module.setDisplay(values);
delay(50);
}
for(int i=128;i>=1;i/=2){
module.setDisplayToString(" ",i);
delay(50);
}
for(int i=0; i<3; i++){
module.setDisplayToString(" ",255);
module.setLEDs(0b11111111 | 0b111111111<< 8 );
delay(250);
module.setDisplayToString(" ",0);
module.setLEDs(0b00000000 | 0b00000000<< 8 );
delay(250);
}
module.setDisplayToString(" rEAdy ");
module.setLEDs(0b11111111 | 0b00000000<< 8 );
delay(1500);
module.clearDisplay();
}
byte option, button;
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 = RPMMAX; //retrieves from x-sim USO this value as parameter divided by 100
byte maxfuel = MAXFUEL;
unsigned int carspeed; //holds the speed data (0-65535 size)
byte gear; // holds gear value data
byte oilTemp;
byte waterTemp;
byte fuel;
byte intensity=7;
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
byte oilTempData = 0;
byte waterTempData = 0;
byte fuelData = 0;
void getData(){
if (Serial.available() >= 9) { //if 6 bytes available in the Serial buffer...
for (i=0; i<17; i++) { // for each byte
bufferArray[i] = 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<<8) + d2); // concatonate bytes (shift 8 bits)
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<<8) + 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 (bufferArray[9] == 'O' ){
d1 = bufferArray[10];
d2 = bufferArray[11];
oilTemp = ((d1<<8) + d2);
oilTempData = 1;
}
if (bufferArray[12] == 'W' ){
d1 = bufferArray[13];
d2 = bufferArray[14];
waterTemp = ((d1<<8) + d2);
waterTempData = 1;
}
if (bufferArray[15] == 'F' ){
fuel = bufferArray[16];
fuelData = 1;
}
}
void option1(byte rpmMode){
getData();
//Speed Data---------------------------------------------
if (speeddata == 1) {
for(int i=7; i>=5; i--){
if(carspeed!=0){
module.setDisplayDigit(carspeed%10, i, false);
carspeed/=10;
}
else
module.clearDisplayDigit(i, false);
}
//module.setDisplayToDecNumber(carspeed, 0, false); //displays numerical the speed
speeddata=0;
}
//Gear Data ----------------------------------------------
if (geardata == 1) {
gear = gear - 127; // offset the 0 value in 8-bit
if (gear >= 1 and gear <10 ){
module.setDisplayDigit(gear, 1, false); // displays numerical value of the current gear
}
if (gear == 0){
module.setDisplayToString("n", 0, 1); // displays the character for neutral
}
if (gear == 255){ // -1 that reprecents reverse rollover to 255 so...
module.setDisplayToString("r", 0, 1); // displays the character for reverse
}
geardata=0;
}
//RPM Data------------------------------------------------------
if (rpmdata == 1) {
rpmleds = map(rpm,0,rpmmax,0,9); // distributes the rpm level to the 8 leds + 1 for shift change
if(rpmMode==1) {
if (rpmleds==0){
module.setLEDs(0b00000000 | 0b00000000<< 8);
}
if (rpmleds==1){
module.setLEDs(0b00000001 | 0b00000000<< 8 );
}
if (rpmleds==2){
module.setLEDs(0b00000011 | 0b00000000<< 8 );
}
if (rpmleds==3){
module.setLEDs(0b00000111 | 0b00000000<< 8 );
}
if (rpmleds==4){
module.setLEDs(0b00001111 | 0b00000000<< 8);
}
if (rpmleds==5){
module.setLEDs(0b00011111 | 0b00000000<< 8);
}
if (rpmleds==6){
module.setLEDs(0b00011111 | 0b00100000<< 8 );
}
if (rpmleds==7){
module.setLEDs(0b00011111 | 0b01100000<< 8 );
}
if (rpmleds==8){
module.setLEDs(0b11111111 | 0b111111111<< 8 );
}
}
else if(rpmMode==2) {
if (rpmleds==0){
module.setLEDs(0b00000000 | 0b00000000<< 8);
}
if (rpmleds==1){
module.setLEDs(0b00000000 | 0b00000000<< 8 );
}
if (rpmleds==2){
module.setLEDs(0b10000001 | 0b00000000<< 8 );
}
if (rpmleds==3){
module.setLEDs(0b10000001 | 0b00000000<< 8 );
}
if (rpmleds==4){
module.setLEDs(0b11000011 | 0b00000000<< 8 );
}
if (rpmleds==5){
module.setLEDs(0b11000011 | 0b00000000<< 8 );
}
if (rpmleds==6){
module.setLEDs(0b11100111 | 0b00000000<< 8 );
}
if (rpmleds==7){
module.setLEDs(0b11100111 | 0b00011000<< 8 );
}
if (rpmleds==8){
module.setLEDs(0b11111111 | 0b111111111<< 8 );
}
}
rpmdata=0;
}
}
void option3(){ //Set RPM Max. vlaue
byte buttonPressed;
do {
module.setDisplayToDecNumber(rpmmax,0,false);
buttonPressed = module.getButtons();
if(buttonPressed==32)
rpmmax-=500;
else if(buttonPressed==64)
rpmmax+=500;
delay(100);
}while(buttonPressed!=128);
}
void option4(){
getData();
module.setDisplayToString("l",0,4);
if(oilTempData==1 && oilTemp<255 && oilTemp>0){
for(int i=7; i>=5; i--){
if(oilTemp!=0){
module.setDisplayDigit(oilTemp%10, i, false);
oilTemp/=10;
}
else
module.clearDisplayDigit(i, false);
}
oilTempData=0;
}
if(waterTempData==1 && waterTemp<255 && waterTemp>0){
for(int i=2; i>=0; i--){
if(waterTemp!=0){
module.setDisplayDigit(waterTemp%10, i, false);
waterTemp/=10;
}
else
module.clearDisplayDigit(i, false);
}
waterTempData=0;
}
}
void option5(){ //Set Fuel Max. vlaue
byte buttonPressed;
do {
fuel = maxfuel;
for(int i=3; i>=1; i--){
if(fuel!=0){
module.setDisplayDigit(fuel%10, i, false);
fuel/=10;
}
else if(i==3) //There is 0Lts, no fuel!
module.setDisplayDigit(0,i,false);
else //clean left zeros
module.clearDisplayDigit(i, false);
}
module.setDisplayToString("LtS",0,5);
buttonPressed = module.getButtons();
if(buttonPressed==32)
maxfuel-=1;
else if(buttonPressed==64)
maxfuel+=1;
delay(100);
}while(buttonPressed!=128);
clean();
}
void option6(){
getData();
if(fuelData==1 && fuel<228 && fuel>=127){
fuel-=127;
int maxLed = fuel*8/maxfuel;
for(int i=0; i<8; i++){
if(i>=maxLed){
if(maxLed==0 && fuel >= 0)
module.setLED(2,0);
else
module.setLED(0, i);
}
else
module.setLED(1, i);
}
for(int i=3; i>=1; i--){
if(fuel!=0){
module.setDisplayDigit(fuel%10, i, false);
fuel/=10;
}
else if(i==3) //There is 0Lts, no fuel!
module.setDisplayDigit(0,i,false);
else //clean left zeros
module.clearDisplayDigit(i, false);
}
module.setDisplayToString("LtS",0,5);
fuelData = 0;
}
}
void option7(){
byte buttonPressed;
do {
module.setupDisplay(1,intensity);
module.setDisplayToString("--tESt--",0);
buttonPressed = module.getButtons();
if(buttonPressed==32 && intensity>0)
intensity--;
else if(buttonPressed==64 && intensity<7)
intensity++;
delay(80);
}while(buttonPressed!=128);
}
void clean(){
module.clearDisplay();
module.setLEDs(0b00000000 | 0b00000000<< 8);
}
void loop() {
button=module.getButtons();
if(button!=0){
switch(button){
case 1:
clean();
module.setDisplayToString("GEAr SPd",0);
delay(1000);
module.clearDisplay();
module.setDisplayToString("LEdS 1",0,1);
delay(1000);
clean();
break;
case 2:
clean();
module.setDisplayToString("GEAr SPd",0);
delay(1000);
module.clearDisplay();
module.setDisplayToString("LEdS 2",0,1);
delay(1000);
clean();
break;
case 4:
clean();
break;
case 8:
clean();
module.setDisplayToString("tAG l AC",0b10000000);
delay(1000);
clean();
break;
case 16:
clean();
module.setDisplayToString("FUEL",0,2);
delay(1000);
clean();
break;
case 32:
clean();
module.setDisplayToString("SEt FUEL",0);
delay(1000);
clean();
break;
case 64:
clean();
module.setDisplayToString("briGht",0,1);
delay(1000);
clean();
break;
}
option = button;
}
switch(option){
case 1:
option1(1);
break;
case 2:
option1(2);
break;
case 4:
option3();
break;
case 8:
option4();
break;
case 16:
option6();
break;
case 32:
option5();
break;
case 64:
option7();
break;
case 128:
clean();
break;
default:
clean();
break;
}
}
Muy bien! Ya tenéis un gran paso hecho! Sigamos...
Marcadores