Go Down

Topic: Using UnoJoy to send data to VR game (Read 169 times) previous topic - next topic

therealweezy

Dear people willing to help me,

I am having a problem with a program I made, to give a little information: for a school assignment, I am making a VR bike simulator. With a reed sensor(magnetic) I measure the speed of the wheel and with a potentiometer, I measure the steering angle.
I also convert the speed to a value for the program. I monitored all the value's and they are correct.

for sending the information to the game I use UnoJoy, this is a program that converts your Arduino to a joystick.
The problem I have is that the values are not sent to the pc.

if somebody has an idea how I can fix it, I appreciate that a lot.

I think the problem is somewhere in this part of the code:
Code: [Select]

dataForController_t getControllerData(void)
{
  dataForController_t controllerData = getBlankDataForController();
  
  controllerData.leftStickX = stuur() >> 2;
  controllerData.leftStickY = speed_to_bit() >> 2;
  
  return controllerData;
}


I also noticed that people on this forum are quite precise on how to use this forum, this is my first time posting here so if there are any tips on how to make posts more efficient/clear i am open for feedback.

thanks.  

This is the full program:
Code: [Select]

#include <Wire.h>
#include "UnoJoy.h"

#define Program_versie


//#define DEBUG
//#define Sensor        0  
//#define POTENTIOMETER A1

// pinnen
int Sensor = 0;
int ledpin = 13;
int POTENTIOMETER = 15;

// counter
int state = 0;
int prevState = 0;
int counter = 1;

// status voor REED sensor
bool sensorStatus = 0;

// nodige int's
int interval = 20;
float tijd = 0;
float tijdInSec = 0;
int maxSpeed = 30;
float diameter = 0.66;
float Speed = 0.00;
float omtrek = 3.14 * diameter;
float SpeedKMH = 0.00;
int stuur();
int speed_to_bit();


// timers
unsigned long currentMillis = 1;
unsigned long newMillis = 1;
unsigned long previousMillis = 1;
unsigned long wheelTimer = 1;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);

setupPins();
}

void loop() {
  // put your main code here, to run repeatedly:
#ifdef DEBUG
dataForController_t controllerData = getControllerData();
setControllerData(controllerData);  
#else
snelheid ();
stuur ();
speed_to_bit ();
#endif
}

void setupPins(){
  pinMode(Sensor, INPUT);
  pinMode(ledpin, OUTPUT);  
  pinMode(POTENTIOMETER, INPUT);  
}

void snelheid () {
// Reed Sensor
currentMillis = millis();

sensorStatus = digitalRead(Sensor);
  if (sensorStatus == LOW) {
        //Serial.println(sensorStatus);
        wheelTimer = millis();
        digitalWrite(ledpin, HIGH);
       }
      
else {
      digitalWrite(ledpin, LOW);
     }
    
tijd = (currentMillis - wheelTimer);
tijdInSec = tijd/1000;
float tijdInUur = ((tijdInSec/60)/60);

//Serial.println(tijd);  
//Serial.println(tijdInSec);
Speed = omtrek / (tijdInSec);
SpeedKMH = (Speed * 3.6);

#ifdef DEBUG
//Serial.println(SpeedKMH);  
#endif    

return SpeedKMH;
}    


int stuur () {
  digitalWrite(7, HIGH);
// potmeter voor stuurhoek
  int potWaarde = analogRead(POTENTIOMETER);
  if(potWaarde < 335)
    potWaarde = 335;
  if(potWaarde > 720)
    potWaarde = 720;
int  stuurhoek = map(potWaarde, 335, 720, 0, 1023);

//Serial.println(analogRead(POTENTIOMETER));

#ifdef DEBUG
Serial.println(stuurhoek);
#endif

return stuur;
    
}

int speed_to_bit () {
  //Serial.println(SpeedKMH);
  int bitValue = 512 - (512 * (int)SpeedKMH) / maxSpeed;
  if(bitValue > 512)
  {
    bitValue = 512;
  }
  if(bitValue < 0)
  {
    bitValue = 0;    
  }

//Serial.println(bitValue);
#ifdef DEBUG
  Serial.println(bitValue);
  Serial.print("\t");
#endif
 
  
  return bitValue;  
}

dataForController_t getControllerData(void)
{
  dataForController_t controllerData = getBlankDataForController();
  
  controllerData.leftStickX = stuur() >> 2;
  controllerData.leftStickY = speed_to_bit() >> 2;
  
  return controllerData;
}

Go Up