Go Down

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

gazz

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(){

 Serial.begin(115200);

 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(){

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

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


}

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

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

 analogWrite(6,map(fuel,127,255,0,255));
 if (fuel<155) digitalWrite(22,HIGH);
 if (fuel>155) digitalWrite(22,LOW);

}


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

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

 analogWrite(7,map(water,127,255,0,255));
  if (water>200) digitalWrite(23,HIGH);
 if (water<200) digitalWrite(23,LOW);

}


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

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

 analogWrite(8,map(oil,127,255,0,255));
}


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.

gazz

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
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy