I thought you had CANBUS output that was going to give you digital info?
If you're sticking with analog voltages, then you can make a 'map' of RPM to PWM on 3 pins for the color you want.
Divide the RPMs by 100, so you have 10 to 70. Call it 0 to 70 even.
Make a table with 70 entries
0 RedValue GreenValue BlueValue
1 RedValue GreenValue BlueValue
2 RedValue GreenValue BlueValue
:
:
60 RedValue GreenValue BlueValue
70 RedValue GreenValue BlueValue
Fill in the values.
Now when you read an RPM in, you read out the PWM values and send them out.
analogWrite (redPin, rpmRedMap[value]);
analogWrite (bluePin, rpmBlueMap[value]);
analogWrite (greenPin, rpmGreenMap[value]);
You can do the same mapping for analog values, multiplying the number so its a whole number and using that for the table
52 RedValue GreenValue BlueValue
51 RedValue GreenValue BlueValue
50 RedValue GreenValue BlueValue
:
:
1 RedValue GreenValue BlueValue
0 RedValue GreenValue BlueValue
value = analogRead(rpmSensor); // yields 0 to 1023
value = value/20; // yields 0 to 51, corresponding to full RPM (0) to slowest (52)
analogWrite (redPin, voltageRedMap[value]);
analogWrite (bluePin, voltageBlueMap[value]);
analogWrite (greenPin, voltageGreenMap[value]);
So your code might look like:
// set up pin definitions
byte redPin = 3; // PWM pin
byte greenPin = 5; // PWM pin
byte bluePin = 6; // PWM pin
byte voltageRedMap [] = {
255,254,252, ...4,2,0,}; // these are the 51 or 52 value for red
byte voltageBlueMap [] = {
255,254,252, ...4,2,0,}; // these are the 51 or 52 value for blue
byte voltageGreenMap [] = {
255,254,252, ...4,2,0,}; // these are the 51 or 52 value for green
int value;
void setup(){
pinMode (redPin, OUTPUT);
pinMode (bluePin, OUTPUT);
pinMode (greenPin, OUTPUT);
// nothing needed for analog input
}
void loop(){
value = analogRead(A0); // yields 0 to 1023
value = value/20; // yields 0 to 51, corresponding to full RPM (0) to slowest (52)
analogWrite (redPin, voltageRedMap[value]);
analogWrite (bluePin, voltageBlueMap[value]);
analogWrite (greenPin, voltageGreenMap[value]);
delay(50); // pause so colors are only updated ~20 times a second
}
How you fill out the 3 arrays will determine the colors you get. You don't say where the voltage is coming from, hopefully you have protection in place to prevent it from exceeding 5.5V so it doesn't damage the analog input (assuming a 5V power level for the Arduino chip)