The code is the following:(please keep in mind I did NOT write this code, I found it somewhere on the internet.)
- Code: Select all
int Rpm;
int RpmLight;
char kind_of_data;
int latchpin = 8; // connect to pin 8 on the '595
int clockpin = 4; // connect to pin 4 on the '595
int datapin = 7; // connect to pin 7 on the '595
void setup(){
pinMode(latchpin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(datapin, OUTPUT);
Serial.begin(9600);
}
void loop(){
//****************************** READ DATA FROM SERIAL ******************************
while (Serial.available() > 0)
{
kind_of_data = Serial.read();
if (kind_of_data == 'R' ) Read_Rpm();
digitalWrite(latchpin, LOW);
shiftOut(datapin, clockpin, MSBFIRST, RpmLight);
digitalWrite(latchpin, HIGH);
}
}
//****************************** READ DATA FROM SERIAL END ******************************
void Read_Rpm(){
int Rpm = 0;
delay(2);
int Rpm100 = Serial.read();
delay(2);
int Rpm10 = Serial.read();
delay(2);
int Rpm1= Serial.read();
Rpm100 = ((Rpm100)-48)*100;
Rpm10 = ((Rpm10)-48)*10;
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;}
Rpm = Rpm100+Rpm10+Rpm1;
if (Rpm > 108) { RpmLight = 0;}
if (Rpm > 92 && Rpm <= 108) { RpmLight = 1;}
if (Rpm > 76 && Rpm <= 92) { RpmLight = 3;}
if (Rpm > 60 && Rpm <= 76) { RpmLight = 7;}
if (Rpm > 54 && Rpm <= 60){ RpmLight = 15;}
if (Rpm > 48 && Rpm <= 54){ RpmLight = 31;}
if (Rpm > 32 && Rpm <= 48){ RpmLight = 63;}
if (Rpm > 16 && Rpm <= 32){ RpmLight = 127;}
if (Rpm > 0 && Rpm <= 10){ RpmLight = 255;}
}
And I have my x-sim set up like so:



If you know where I failed, please take the time and correct me!
Thanks for your help!
-Valentin