Fixing cheap ebay sim handbrake hall sensor potentiometer

I am new to Arduino but have been programming for a number of years.

This script is meant to control an analogue input from my Hall Sensor and map it to an analogue Joystick axis. The Joystick.h library is working and I have tested it with other scripts however when I try to compile this new code I get an "expected unqualified-id before '{' token" error:

#include "Joystick.h"

void setup()
// Create Joystick
{Joystick_ Joystick;}

{pinMode(A2, INPUT);

Joystick.begin();

const int pinToButtonMap = A2;
}

void loop()

{int pot = analogRead(A2);
int mapped = map(pot,525,870,0,255);
{Joystick.setThrottle(mapped);}}

Use CTRL T in the IDE to format your code.

void setup()
// Create Joystick
{Joystick_ Joystick;}

That limits the scope of Joystick to setup.
You then have a code block that isn't in a function.
Please remember to use code tags when posting code

larryd:
Use CTRL T in the IDE to format your code.

Before you do that, Use a standard indentation to clearly show the code blocks. Never put more than one statement per line. Place any brackets by themselves on a separate line. Use blank lines sparingly, no more than one at a time.

The auto formatter will not fix everything.

Where does the setup() function end ?

Ok I have reformatted and had a bit of a think:

#include "Joystick.h"

void setup()
{  
Joystick_ Joystick;
}

{
 pinMode(A2, INPUT);

  Joystick.begin();

  const int pinToButtonMap = A2;
}

void loop()

{ int pot = analogRead(A2);
  int mapped = map(pot, 525, 870, 0, 255);
  {
    Joystick.setThrottle(mapped);
  }
}

When I don't include this segment:

{  
Joystick_ Joystick;
}

I get an unidentified reference error

aarg:
Before you do that, Use a standard indentation to clearly show the code blocks. Never put more than one statement per line. Place any brackets by themselves on a separate line. Use blank lines sparingly, no more than one at a time.

The auto formatter will not fix everything.

Playing with formatting:
https://forum.arduino.cc/index.php?topic=660909.0

{
 pinMode(A2, INPUT);

  Joystick.begin();

  const int pinToButtonMap = A2;
}

Which function is this code in ?
Which function should it be in ?

You still have a code block that is not in a function.
I suggest you merge it into setup.

Hi Guys,

Thanks for the help so far. I have been working on this and had to scrap the previous code as I couldn't get it working properly.

I now have some updated code :

const int ledPin = 13;//the led attach to pin13
int sensorPin = A2; // select the input pin for the potentiometer
int digitalPin = 7; //D0 attach to pin7

int sensorValue = 0;// variable to store the value coming from A2
boolean digitalValue = 0; // variable to store the value coming from pin7

#include "Joystick.h"

Joystick_ Joystick;

void testThrottleRudder(unsigned int value)
{
  Joystick.setThrottle(value);
  Joystick.setRudder(value);
}

void setup()
{
  Joystick.begin();
  pinMode(digitalPin, INPUT); //set the state of D0 as INPUT
  pinMode(sensorPin, INPUT_PULLUP); //set the state of D0 as INPUT
  pinMode(ledPin, OUTPUT); //set the state of pin13 as OUTPUT
  Serial.begin(9600); // initialize serial communications at 9600 bps
  Joystick.setThrottleRange(0, 255);

}

void loop()
{
  sensorValue = analogRead(sensorPin); //read the value of A2
  digitalValue = digitalRead(digitalPin); //read the value of D0
  Serial.print("Sensor Value "); // print label to serial monitor
  Serial.println(sensorValue); //print the value of A2
  Serial.print("Digital Value "); // print label to serial monitor
  Serial.println(digitalValue); //print the value of D0 in the serial
  if ( digitalValue == HIGH ) //if the value of D0 is HIGH
  {
    digitalWrite(ledPin, LOW); //turn off the led
  }
  if ( digitalValue == LOW) //else
  {
    digitalWrite(ledPin, HIGH); //turn on the led
  }
  delay(200);//delay 200ms
}

The problem I have now is that I can not take the values from the Hall Sensor and map them to the Throttle axis.

This feels like progress, at least now the serial viewer now picks up the Hall sensor inputs and also installs the Joystick drivers. All that's left is to MAP the Hall Sensor inputs to an axis.

Any help would really be appreciated about now as I am struggling to see whats missing

Sorry for all of the hacky code. I have found a much more elegant solution that is now working.

Saving it here for anyone that might have the same problem in future and needs to research a fix:

#include <Joystick.h>
Joystick_ Joystick;

void setup()

{ pinMode(A2, INPUT);
  Joystick.begin();
}

const int pinToButtonMap = A2;

void loop()

{ int pot = analogRead(A2);
  int mapped = map(pot, 498, 605, 0, 255);
  {
    Joystick.setThrottle(mapped);
  }
}

Can you point me in the best starting place to learn more about Arduino programming?