Pls Any help would be apreciated

I dont know how to make my setup work properly, this is my code. What im trying to get is to be able to use A0 and A1 separately, whe i plug something in A0 it is detected as if it was on both A0 and A1, please any suggestion.

#include <Joystick.h>

void setup()

{pinMode(A0, INPUT);
Joystick.begin();

const int pinToButtonMap = A0;

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

const int pinToButtonMap = A1;

void loop()

{int pot = analogRead(A0);
int mapped = map(pot,0,1023,0,255);
{Joystick.setThrottle(mapped);}

{int pot = analogRead(A1);
int mapped = map(pot,0,1023,0,255);
}{Joystick.setRudder(mapped);}}

You can’t.

Just read A0 and use that value to set both rudder and throttle like this:

#include <Joystick.h>

void setup()
{
  Joystick.begin();
}

void loop()
{
  int pot = analogRead(0);
  //  int mapped = map(pot, 0, 1023, 0, 255);
  int mapped = pot / 4; //this is much simpler here
  Joystick.setThrottle(mapped);
  Joystick.setRudder(mapped);
}

EDIT: I haven’t tried compiling this because I don’t have the Joystick library.

You [OP] have lots of "{" and "}" in odd places. Where does the function setup() end ?
Before posting code here you should use the built in formatter in the IDE and ensure that it can compile.

What’s connected to A1?

If nothing, the pin is floating. Floating pins usually read about the same thing as the previous thing you read because of the sampling cap.

lugo:

void loop()

{int pot = analogRead(A0);
int mapped = map(pot,0,1023,0,255);
{Joystick.setThrottle(mapped);}

{int pot = analogRead(A1);
int mapped = map(pot,0,1023,0,255);
}{Joystick.setRudder(mapped);}}

You definitely need to learn when to use braces, and more importantly when not to. You also need to get into the habit of formatting your code properly. Feeding the above through a formatter results in the following:

void loop()
{
    int pot = analogRead(A0);
    int mapped = map(pot, 0, 1023, 0, 255);
    {
        Joystick.setThrottle(mapped);
    }


    {
        int pot = analogRead(A1);
        int mapped = map(pot, 0, 1023, 0, 255);
    }
    {
        Joystick.setRudder(mapped);
    }
}

And now, I hope you can see your error.

The pot / mapped values for A1 are thrown away as soon as you've evaluated them due to the scope rules of C / C++. So when you call Joystick.setRudder(mapped); you're using the mapped value that was computed from A0. Hence the results you're experiencing.

I'm guessing now, but I suspect the error is cause by a failed attempt to fix the compiler errors you'd get from trying to compile this:

void loop()
{
    int pot = analogRead(A0);
    int mapped = map(pot, 0, 1023, 0, 255);
    Joystick.setThrottle(mapped);

    int pot = analogRead(A1);
    int mapped = map(pot, 0, 1023, 0, 255);
    Joystick.setRudder(mapped);
}

The following line:

    int pot = analogRead(A0);

does two things.

  1. It declares an int variable pot, and
  2. it calls analogRead(A0) and saves the value in the newly created line.

When you try and do the following:

    int pot = analogRead(A1);

a few lines later, pot already exists, so the compiler complains since it can't do step 1 again.

You can, however simply say:

    pot = analogRead(A1);

to call analogRead(A1) and save the value in pot, overwriting the original value which you don't need any more anyway.

So if you trim this down to:

void loop()
{
    int pot = analogRead(A0);
    int mapped = map(pot, 0, 1023, 0, 255);
    Joystick.setThrottle(mapped);

    pot = analogRead(A1);
    mapped = map(pot, 0, 1023, 0, 255);
    Joystick.setRudder(mapped);
}

it'll (A) work right, and (B) be vastly more readable.

I will add that doing things as you have with intermediate values is usually a very good idea, since it shows the individual steps you're taking.

You could compress this down to:

void loop()
{
    Joystick.setThrottle(map(analogRead(A0), 0, 1023, 0, 255));
    Joystick.setRudder(map(analogRead(A1), 0, 1023, 0, 255));
}

but this is not as readable, and with a modern day compiler it won't get you any performance gain at all.

Trust me when I tell you, your optimizer is a damn sight smarter than a lot of people give it credit for, so don't be afraid to take several steps so each is clear. The use of intermediate values helps document what's going on. "pot" is a value from a potentiometer, and mapped is a value that is computed from pot through a mapping function.

When you come back and look at this two years from now, you'll be glad you did this.

When I use A0 it activates both, so as if im using A1, it activates them both.
I wanna be able to separate the so that when I use A0, it only activates A0 and not both.
BTW I´m just a highschool student with almost no knowledge about programing, pls I really need help :confused:

lugo:
When I use A0 it activates both, so as if im using A1, it activates them both.
I wanna be able to separate the so that when I use A0, it only activates A0 and not both.
BTW I´m just a highschool student with almost no knowledge about programing, pls I really need help :confused:

What about the help you have already received ?
If you have made changes to your sketch on the basis of the suggestions you have received and are still having problems, post your new code in this thread. Use code tags.

I suspect you need to do something like this

void loop()
{
    int pot = analogRead(A0);
    pot = analogRead(A0);
    int mapped = map(pot, 0, 1023, 0, 255);
    Joystick.setThrottle(mapped);

    pot = analogRead(A1);
    pot = analogRead(A1);
    mapped = map(pot, 0, 1023, 0, 255);
    Joystick.setRudder(mapped);
}

by duplicating the analogRead() the first reading is thrown away. The Arduino has only one ADC and when you change it from one analog pin to another the ADC may contain remnants of the voltage from the first pin - hence the need to throw away the first reading.

...R