Pages: [1]   Go Down
Author Topic: Serial data transfer  (Read 460 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Someone is helping me get the data out of a driving simulator game to drive real gauges and lights on a real dashboard,

He's written me a .dll file that starts with the game (the game is designed to allow all vehicle data to be read with a plugin)

We are starting with simple things, to turn a led on or off depending on if the vehicles ignition is on or off,

the dll reads the game data, and sends a value of 0 or 1 depending on the ignition status, preceded by the letter E so let the arduino know it's data relating to the electricity state,

I click the connect button on the dll's form, the arduino starts recieving data, and turns on the led set up to show if the connection is on or off, but the led for the electricity status also comes on, even tho it is off in the game, and toggeling the electricity status in the game, the form shows it changing, but the arduino isnt changing the led state.

Can someone look at the code and let me know if it's something in the arduino code, or i need to look at the dll part please.

Code:
int elec;
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 == 'E' ) Read_Elec();
    if (kind_of_data == 'I' ) digitalWrite(23,HIGH);
    if (kind_of_data == 'O' ) digitalWrite(23,LOW);
  }

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

void Read_Elec(){
  delay(1);
  int Elec = Serial.read();
  if (elec=1) digitalWrite(22,HIGH);
  if (elec=0) digitalWrite(22,LOW);
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if (elec=1) digitalWrite(22,HIGH);
  if (elec=0) digitalWrite(22,LOW);

Comparisons are == not just a single =.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Brilliant, thanks for that,

The guy who's writing the DLL for me did the arduino code too, he's never worked with an arduino before, so was guessing most things based on some code i was using before,

i spotted that we needed a '- (0) after the Elec = Serial.read()  bit, and we have the first part working, an led turning on and off in response to the ignition state of the vehicle in the game.

Next step is to work the rpm gauge, using the tone function as that's the kind of signal the real gauge needs, (variable frequency, 50% duty cycle)
need to figure out how to take the varying text values from the game (i.e. it shows the numbers, 550, for ideling rpm, 2450 for max rpa, and any number in between depending on engine speed) and make them alter the frequency output to the real gauge.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Got some bits added the the sketch:
Code:
int elec;
char kind_of_data;
void setup(){
  Serial.begin(115200);
  TCCR4B = (TCCR4B & 0xF8) | 0x01 ;
  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 == 'E' ) Read_Elec();
        if (kind_of_data == 'R' ) Read_Rpm();
        if (kind_of_data == 'I' ) digitalWrite(23,HIGH);
        if (kind_of_data == 'O' ) digitalWrite(23,LOW);
  }
  //****************************** READ DATA FROM SERIAL END ******************************
  Serial.println();
}
void Read_Elec(){
  delay(1);
  int Elec = Serial.read() - '0';
  if (Elec == 1)
  {
   digitalWrite(22,HIGH);
  }
  else
  {
digitalWrite(22,LOW);
  }
}
void Read_Rpm(){
  delay(1);
  int rpm = Serial.read()- '0';
  tone(9, map(rpm,127,255,45,433));
}

last bits arent working right... arduino is being sent the rpm data from the game, but it is numbers from 0 to 2350, i tried adjusting the '127,255' part to the new numbers, and the rpm gauge's needle moves, but only for about 300rpm then it goes crazy,

the 45,433 part is the frequency range the gauge needs, 45Hz for zero rpm, 433Hz for off the scale.

Any pointers on how to recieve the rpm data as numbers that directly relate to where the needs should be pointing at, rather than a value in 8 bit form that it is trying now.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  int rpm = Serial.read()- '0';
This limits the rpm to a value from 0 to 9 (presuming that what was sent was actually a digit). Is that right?

Code:
  tone(9, map(rpm,127,255,45,433));
Then, your from range is not even close to matching the range rpm is actually in. Therefore, the output value will not be in the to range. So, the input to the tone function is most likely not what you think it is.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The question about passing multiple digits via the serial port comes up practically every day (not that you are to know that).

PaulS describes ways of dealing with it in various posts of his. I describe a couple of methods here:

http://gammon.com.au/serial
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

that site has some very intersting stuff on it... ecpecialy a way to process the data relating to each gauge instiantly using switch states,

i am still very new to this programing stuff, basicaly i found out that with the electricity led, i needed the -'0' bit otherwise it didnt work,

and i guess that's been copied for the rpm part of the code,

so can i put something like -'0000'  to tell it to expect a 4 digit number, i imagine not, but as i said, i know very little.

the code "tone(9, map(rpm,127,255,45,433));" is from testing the gauges, using a test tablet that gave moving sliders for outputs, in 8 bit format, hence the 127, 255  bit, the 45,433 part is the frequency my particular rev counter responds to, 45Hz being zero, 433Hz being at the end stop,

so i need to map the incoming data that is 0 to 2350, relating directly to the rpm's of the engine, and map it to 45 to 433Hz, (my rev counter red lines at 5k, so i'm guessing something like 200Hz will be 2500rpm,

i'll have a good read of the suggestions and see if i can figure this out, but thanks for the help so far.
Logged

Pages: [1]   Go Up
Jump to: