Pages: [1]   Go Down
Author Topic: Leonardo, combining scripts.  (Read 488 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

I am wondering about something,

At the moment, i use a mega to handle outputs from a computer simulation game to run bus gauges and lights on a real dashboard, the script starts serial comunication, and recieves the data from the game... just a string of numbers, then it sends them out as PWM, I/O, and tone signals on the relevent pins, which turn the lights on and off, or move the needles on the gauges to the relevent positions.

i handled the inputs to the game with a leo bodnar input board, untill i miswired the gear selector buttons ilumination, and sent 24 volts up an input pin,

So i am thinking of getting a leornardo, and combining the 2 boards into one,
is it possible to set the board up to handle both inputs and outputs at once? or will it cause lag or other problems,

It will only be on or off inputs, but the on inputs will be held on for the duration the control item is on, rather than just sending a pulse for on/off,

Basically i guess i want to combine 2 scripts to run together, but am not sure if it will work.
Logged

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

Quote
Basically i guess i want to combine 2 scripts to run together, but am not sure if it will work.
Which two scripts?

Quote
i handled the inputs to the game with a leo bodnar input board
Oh, sure I have a dozen of them sitting around. Not!
Logged

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

Quote
Basically i guess i want to combine 2 scripts to run together, but am not sure if it will work.
Which two scripts?

Sorry, was sort of wondering if it's possible to easily combine an ouput and an input script,

the output script i run on my mega right now is:

Code:
// Last updated: March 15th, 2012 - 8:40PM GMT
// Using state machine pattern - based on Nick Gammon code

typedef enum {  NONE, GOT_I, GOT_O, GOT_R, GOT_o, GOT_T, GOT_S, GOT_B, GOT_H, GOT_b, GOT_h, GOT_P, GOT_F, GOT_A, GOT_W, GOT_V, GOT_w, GOT_r, GOT_k, GOT_a } states;
states state = NONE;
unsigned int currentValue;

void setup ()
{
  Serial.begin (115200);
  TCCR4B = (TCCR4B & 0xF8) | 0x01 ;
  state = NONE;
  pinMode(22, OUTPUT);
  pinMode(23, OUTPUT);
  pinMode(24, OUTPUT);
  pinMode(25, OUTPUT);
  pinMode(26, OUTPUT);
  pinMode(27, OUTPUT);
  pinMode(28, OUTPUT);
  pinMode(29, OUTPUT);
  pinMode(30, OUTPUT);
  pinMode(31, OUTPUT);
  pinMode(32, OUTPUT);
  pinMode(33, OUTPUT);
  pinMode(34, OUTPUT);
}

// Lights

void Read_BusStopLight(const unsigned int value)
{
  if (value == 1)
  {
        digitalWrite(22,HIGH);
  }
  else
  {
        digitalWrite(22,LOW);
  }
}

void Read_Blinker(const unsigned int value)
{
  if (value == 1)
  {
        digitalWrite(24,HIGH);
  }
  else
  {
        digitalWrite(24,LOW);
  }
}

void Read_Highbeam(const unsigned int value)
{
  if (value == 1)
  {
        digitalWrite(25,HIGH);
  }
  else
  {
        digitalWrite(25,LOW);
  }
}

void Read_Battery(const unsigned int value)
{
  if (value == 1)
  {
        digitalWrite(26,HIGH);
  }
  else
  {
        digitalWrite(26,LOW);
  }
}

void Read_Handbrake(const unsigned int value)
{
  if (value == 1)
  {
        digitalWrite(27,HIGH);
  }
  else
  {
        digitalWrite(27,LOW);
  }
}

void Read_LowPressure(const unsigned int value)
{
  if (value == 1)
  {
        digitalWrite(28,HIGH);
  }
  else
  {
        digitalWrite(28,LOW);
  }
}

void Read_ABS(const unsigned int value)
{
  if (value == 1)
  {
        digitalWrite(29,HIGH);
  }
  else
  {
        digitalWrite(29,LOW);
  }
}

void Read_RearDoor(const unsigned int value)
{
  if (value == 1)
  {
        digitalWrite(30,HIGH);
  }
  else
  {
        digitalWrite(30,LOW);
  }
}

void Read_RetarderDirect(const unsigned int value)
{
  if (value == 1)
  {
        digitalWrite(31,HIGH);
  }
  else
  {
        digitalWrite(31,LOW);
  }
}

void Read_FourFlasher(const unsigned int value)
{
  if (value == 1)
  {
        digitalWrite(32,HIGH);
  }
  else
  {
        digitalWrite(32,LOW);
  }
}

void Read_Backlight(const unsigned int value)
{
  if (value == 1)
  {
        digitalWrite(33,HIGH);
  }
  else
  {
        digitalWrite(33,LOW);
  }
}

void Read_ASR(const unsigned int value)
{
  if (value == 1)
  {
        digitalWrite(34,HIGH);
  }
  else
  {
        digitalWrite(34,LOW);
  }
}

// Gauges

void Read_Temperature(const unsigned int value)
{
  analogWrite(7,map(value,0,100,255,0));
}

void Read_Rpm(const unsigned int value)
{
  tone(9, map(value,0,5700,55,423));
}

void Read_Oil(const unsigned int value)
{
  analogWrite(8,map(value,0,50,0,255));
}

void Read_Fuel(const unsigned int value)
{
  analogWrite(6,map(value,0,100,255,0));
}

void Read_Velocity(const unsigned int value)
{
  tone(10, map(value,0,1250,55,423));
}


void handlePreviousState()
{
  switch (state)
  {
  case GOT_R:
    Read_Rpm(currentValue);
    break;
  case GOT_o:
    Read_Oil(currentValue);
    break;
  case GOT_T:
    Read_Temperature(currentValue);
    break;
  case GOT_S:
Read_BusStopLight(currentValue);
break;
  case GOT_B:
Read_Blinker(currentValue);
break;
  case GOT_H:
Read_Highbeam(currentValue);
break;
  case GOT_b:
Read_Battery(currentValue);
break;
  case GOT_h:
Read_Handbrake(currentValue);
break;
  case GOT_P:
Read_LowPressure(currentValue);
break;
  case GOT_F:
Read_Fuel(currentValue);
break;
  case GOT_A:
Read_ABS(currentValue);
break;
  case GOT_W:
Read_RearDoor(currentValue);
break;
  case GOT_V:
Read_Velocity(currentValue);
break;
  case GOT_w:
Read_FourFlasher(currentValue);
break;
  case GOT_r:
Read_RetarderDirect(currentValue);
break;
  case GOT_k:
Read_Backlight(currentValue);
break;
  case GOT_a:
Read_ASR(currentValue);
break;
  case GOT_I:
digitalWrite(23,HIGH);
break;
  case GOT_O:
digitalWrite(23,LOW);
break;
  }
  currentValue = 0;
}

void processIncomingByte (const byte c)
{
  if (isdigit (c))
  {
    currentValue *= 10;
    currentValue += c - '0';
  }
  else
  {

    handlePreviousState ();
    switch (c)
    {
    case 'I':
      state = GOT_I;
      break;
    case 'O':
      state = GOT_O;
      break;
    case 'R':
      state = GOT_R;
      break;
    case 'o':
      state = GOT_o;
      break;
    case 'T':
      state = GOT_T;
      break;
    case 'S':
      state = GOT_S;
      break;
    case 'B':
      state = GOT_B;
      break;
    case 'H':
      state = GOT_H;
      break;
    case 'b':
      state = GOT_b;
      break;
    case 'h':
      state = GOT_h;
      break;
case 'P':
  state = GOT_P;
  break;
case 'F':
  state = GOT_F;
  break;
case 'A':
  state = GOT_A;
  break;
case 'W':
  state = GOT_W;
  break;
case 'V':
  state = GOT_V;
  break;
case 'w':
  state = GOT_w;
  break;
case 'r':
  state = GOT_r;
  break;
case 'k':
  state = GOT_k;
  break;
case 'a':
  state = GOT_a;
  break;
    default:
      state = NONE;
      break;
    }
  } 
 
}

void loop()
{
if (Serial.available())
processIncomingByte (Serial.read());
}

The input code i do not have as i'm not using an arduino for input ATM, but it will be a usb gamepad type input code, i.e. button presses only seen by windows as a usb hid gamepad.

i also know i wont have enough pins on a leonardo, so will have to think about matrix's/shift registers or things like that.... unless there is a mega that can work like a leonardo... i.e. be seen by windows as a usb hid devise.

Quote
i handled the inputs to the game with a leo bodnar input board
Oh, sure I have a dozen of them sitting around. Not!

Sorry again, thought everyone knew of them.... i get wrapped up in my little world at times,

It's basically a usb input board that you use when building your own joysticks, as in flight simulator controls, or in my case, bus simulator controls,
is seen by windows as a usb hid joypad with 8 x 8bit analogue axis and 32 buttons plus 4 more for the D pad. buttons are via a 6x6 matrix,
Logged

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

I forgot to say, one of the reasons i want to use an arduino for input rather than another leo bodnar input board, is i'd like to be able to add to the gamepad script to send a gamepad button pulse on key down and key up... or rising edge and falling edge detection i think it's called....

this is because i am using a lot of toggle type switches, the type that stay in one position rather than push buttons that push and release,

A lot of driving games don't like continuously pressed button inputs, and will not detect the switch release (falling edge) so you have to double switch to get the game to recognise you are turning something off.

 
Logged

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

Quote
Oh, sure I have a dozen of them sitting around. Not!
That was a hint for you to post a link.

Code:
void Read_Blinker(const unsigned int value)
{
  if (value == 1)
  {
        digitalWrite(24,HIGH);
  }
  else
  {
        digitalWrite(24,LOW);
  }
}
You have a really funny definition of Read. I suppose the function to get input is called Write_something().
Logged

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

Quote
Oh, sure I have a dozen of them sitting around. Not!
That was a hint for you to post a link.

D'oh, here ya go,
http://www.leobodnar.com/products/BU0836/


Code:
void Read_Blinker(const unsigned int value)
{
  if (value == 1)
  {
        digitalWrite(24,HIGH);
  }
  else
  {
        digitalWrite(24,LOW);
  }
}
You have a really funny definition of Read. I suppose the function to get input is called Write_something().

I'll admit i know very little about programming, it's just so hard to get my head around it,
A friend helped me write the script as well as completely writing the DLL that starts the comunications from the game to the arduino,

All i know is it works.... the bit of code above is to flash the indicator led in the dashboard on and off in time with the one in the game,

Unfortunately my friend can no longer help me with this project, so i am stuck, i don't even have the source code for the DLL, so i can't add more functions... even if i knew how to.
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2299
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So i am thinking of getting a leornardo, and combining the 2 boards into one,
is it possible to set the board up to handle both inputs and outputs at once? or will it cause lag or other problems,

How many inputs?, How many outputs? Yes is the answer but unless you know how many pins you need you can't figure out which card to use.

As you like to get your wiring wrong I'd recommend a card such as the UNO which allows you to replace a burnt out chip at a fraction of the cost of a new board!.

Mark
Logged

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

at least 20 inputs, 8 PWM outputs, and around 15 on/off outputs,
and possibly later 5 analogue inputs.

If i could get my mega to work with inputs it'd be ideal, but i'm not sure i can do that, as it needs to show up on the computer as a HID gamepad/joystick.
Logged

Pages: [1]   Go Up
Jump to: