Go Down

Topic: Where do i put the code to alter timers? (Read 651 times) previous topic - next topic


Mar 02, 2012, 10:05 pm Last Edit: Mar 02, 2012, 10:08 pm by gazz Reason: 1
I've found a sketch posted on the old forum that does basically all i want.... i.e. reads data from x-sim, and sends it via the arduino to my real gauges.

Only problem i have is that it is using pwm to vary the voltage to earth to make the needles move in the gauges, and this is causing the coils in the air-core gauges to 'sing' due to the low frequency of the pwm.

i need to run the pwm to those gauges at 31Khz to make them silent (to our ears), i'm using a mega 1280, so think timer 4 is best to alter???

i think i need to add "TCCR4B = TCCR4B 0b11111000 | 0x01", but where do i add it in the sketch? and do i need any other code to make timer 4 run at 31Khz?

The sketch i am using for my gauges is below, originally written my 'herctrap'

Code: [Select]
int rpm;
int i;
int leds;
int Speed;
int fuel;
int oil;
int previous_potion = 0;
int rotate;
int water;

char kind_of_data;

void setup(){


 pinMode(22, OUTPUT);
 pinMode(23, OUTPUT);

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 == 'F' ) Read_Fuel();
   if (kind_of_data == 'W' ) Read_Water();
   if (kind_of_data == 'O' ) Read_Oil();


 //****************************** READ DATA FROM SERIAL END ******************************

void  Read_Rpm(){

 int Rpm100 = Serial.read()- '0';
 int Rpm10 = Serial.read()- '0';
 int Rpm1 = Serial.read()- '0';

 int rpm = 100*Rpm100 + 10*Rpm10 + Rpm1;
 tone(9, map(rpm,127,255,45,433));


void Read_Fuel(){
 int Fuel100 = Serial.read()- '0';
 int Fuel10 = Serial.read()- '0';
 int Fuel1 = Serial.read()- '0';

 fuel = 100*Fuel100 + 10*Fuel10 + Fuel1;

 if (fuel<155) digitalWrite(22,HIGH);
 if (fuel>155) digitalWrite(22,LOW);


void Read_Water(){
 int Water100 = Serial.read()- '0';
 int Water10 = Serial.read()- '0';
 int Water1 = Serial.read()- '0';

 water = 100*Water100 + 10*Water10 + Water1;

  if (water>200) digitalWrite(23,HIGH);
 if (water<200) digitalWrite(23,LOW);


void Read_Oil(){
 int Oil100 = Serial.read()- '0';
 int Oil10 = Serial.read()- '0';
 int Oil1 = Serial.read()- '0';

 oil = 100*Oil100 + 10*Oil10 + Oil1;


Would someone be kind enough to indicate where i need to insert the code in the above sketch to set the timer that runs pins 6, 7 & 8 @ 31Khz please (or any other set of 3 analog pins, as long as the timer being changed dosent throw off other things like the servo lib. as i will be using that nest for some gauges that were mechanical and having servos shoved in them.


Found it, a post somewhere on the forum eventually led me to figure it out,

i just added 'TCCR4B = (TCCR4B & 0xF8) | 0x01 ;' in the setup, and my gauges are silent.

i can see it's going to be very very hard for me to learn this programming stuff, i'm almost wishing my hands werent hurting so much and i could go back to striping engines into little pieces, replacing all worn parts and re-building it... and not having any 'spare' buts or bolts left over afterwards, as that's what i grew up with, so i know the mechanical stuff and to me it's easy,

Go Up