Pages: [1]   Go Down
Author Topic: analogWrite problem with X-Sim  (Read 685 times)
0 Members and 1 Guest are viewing this topic.
Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Oi mate!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Earlier on I posted in the "Motors, Mechanics, and Power" part of the forum because I was having a spot of bother with my instrument cluster that I'm trying to make for LFS.
My problem was that only one of the instruments would work at a time and it all depended on which part of information the Arduino received first.

I discovered after a quick google search that the Arduino can only output one "Tone" at a time, and I was trying to output two. That's why it didn't work, and so, I'm using tone for the speedo (because I know how I can make the Tone "command" work accurately) and "analogWrite" for the rev counter, however the rev counter doesn't really work, when I activate the X-Sim it just makes the needle go to its maximum and stay there, unless i set the value 100% correct, then it stays at below zero when the engine is off, however when I turn the engine back on it sends it flying to its maximum again.

If I then proceed to do full throttle ingame and just let it drop from max to zero (ingame), the rev counter (on the cluster) will drop by just a few RPM at a few points.

Kind of difficult to explain, tried my best. If anyone want's some screenshots and/or the code to see for yourself let me know smiley-grin
Logged

Things I love: Arduino, my computer and Nicki

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8663
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is a third-party Tone library that will allow multiple simultaneous tones on different pins.

http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Oi mate!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks!
Logged

Things I love: Arduino, my computer and Nicki

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Oi mate!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, I didn't get around to try until today, but I am kind of wondering, how one would go about to add this to the code, as I'm using Herctrap's code, I don't really know how I'd implement that tone library into the code.

If anyone knows, here's the code, if you would know how to, any help is very much appreiciated! smiley-grin


Code:
int rpm;
int i;
int leds;
int Speed;
int fuel;
int ebrake;
int brake;
int fuelled;
int Turbo;
int gear;
int previous_potion = 0;
int rotate;

char kind_of_data;


void setup(){

  Serial.begin(115200);

  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, 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 == 'S' ) Read_Speed();
    if (kind_of_data == 'F' ) Read_Fuel();
    if (kind_of_data == 'E' ) Read_EBrake();
    if (kind_of_data == 'B' ) Read_Brake();

  }


  //****************************** 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;
  analogWrite(5,map(rpm,8,128,2,128));


}

void Read_Speed(){

  delay(1);
  int Speed100 = Serial.read()- '0';
  delay(1);
  int Speed10 = Serial.read()- '0';
  delay(1);
  int Speed1 = Serial.read()- '0';

  Speed = 100*Speed100 + 10*Speed10 + Speed1;

  tone(9, map(Speed,128,255,19,259));

}

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(2,map(fuel,127,255,53,183));
  if (fuel<135) digitalWrite(10,HIGH);
  if (fuel>135) digitalWrite(10,LOW);

}


void Read_EBrake(){
  delay(1);
  int EBrake100 = Serial.read()- '0';
  delay(1);
  int EBrake10 = Serial.read()- '0';
  delay(1);
  int EBrake1 = Serial.read()- '0';

  ebrake = 100*EBrake100 + 10*EBrake10 + EBrake1;

  if (ebrake>200) digitalWrite(3,HIGH);
  if (ebrake<200) digitalWrite(3,LOW);

}
void Read_Brake(){
  delay(1);
  int Brake100 = Serial.read()- '0';
  delay(1);
  int Brake10 = Serial.read()- '0';
  delay(1);
  int Brake1 = Serial.read()- '0';

  brake = 100*Brake100 + 10*Brake10 + Brake1;

  if (brake>200) digitalWrite(5,HIGH);
  if (brake<200) digitalWrite(5,LOW);

}
Logged

Things I love: Arduino, my computer and Nicki

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Oi mate!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Anyone?
Logged

Things I love: Arduino, my computer and Nicki

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8663
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Anyone?
You don't describe what you want the code to do.  Kind of hard to make recommendations without knowing the goal.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Oi mate!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Anyone?
You don't describe what you want the code to do.  Kind of hard to make recommendations without knowing the goal.


I want to add the library you linked me to the code, making the speed and RPM both run off tones, if it's possible smiley
Couldn't figure how to add an instance in such a way that it would work :l
Logged

Things I love: Arduino, my computer and Nicki

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Oi mate!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

bumping
Logged

Things I love: Arduino, my computer and Nicki

Pages: [1]   Go Up
Jump to: