Hello! I'm making my own steering wheel and I needed to map a value but for some reason when I run the code it says "xvalue was not declared in scope". Thanks!
#include <Joystick.h>
#include <HID.h>
#include <Keyboard.h>
Joystick_ Joystick;
int button = 0;
int button1 = 0;
int delaytime = 0;
void setup() {
Joystick.begin();
Joystick.setXAxisRange(127, -127);
pinMode(A0, INPUT_PULLUP);
Keyboard.begin();
pinMode(7, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
Serial.begin(9600);
byte xvalue;
}
void loop() {
delaytime = 0;
button = digitalRead(7);
button1 = digitalRead(6);
int pot1 = analogRead(A0);
Serial.println(pot1);
xvalue = map(pot1, 14, 127, 1023, -127
if(button == 0)
Keyboard.print("r");
if(button == 0)
delaytime = 200;
if(button1 == 0)
Keyboard.print("f");
if(button1 == 0)
delaytime = 200;
Joystick.setXAxis(xvalue);
delay(delaytime);
}
As xvalue is declared in setup() you cannot use it outside of that function. Declare it as a global instead
The easier you make it to read and copy your code the more likely it is that you will get help
Please follow the advice given in the link below when posting code , use code tags and post the code here
See also FAQ - Arduino Forum for general rules on forum behaviour and etiquette.
Hello,
Welcome to the Arduino Forum.
This guide explains how to get the best out of this forum. Please read and follow the instructions below.
Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…
Named variables
They're wildly overrated.
You declare the variable xvalue at the end of setup, where it immediately goes out of scope. You try to use it in loop, where it doesn't exist. Make it global.
Why global?
It's not used used outside of loop
Good point.
If he makes it global it makes life simpler, and he doesn't need to worry about making it static.
The irony is that there is no real need for the variable in the first place
1 Like
I believe you know what static means.
I am anticipating further problems, in this case a complaint that xvalue keeps losing its, err, value. I just think it's easier to tell new folk to make variables global, saves other problems.
Looks like you anticipated the wrong problem.
system
Closed
October 19, 2021, 8:59pm
13
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.