Joystick--LEDs on and off

For my digital electronics class, we started learning about arduino and micro-controllers. We got an assignment where we have to program a joystick to turn a green led on when the the up/down potentiometer reads between 300 & 400 (leaving it on even when the range is left), and turn the LED off when it reads between 700 & 800. We also have to do the same thing for the left/right potentiometer, except with a red LED. Then we need to use a push button to reset both LED’s to off when it is pressed.

I am first trying in circuits123d as I don’t have an arduino at home. I have the LEDs working, but when i try to add an extra if statement for the reset button, the LED’s stop responding.

Heres my code:

int xpot = A0;
int ypot = A1;
int pir = 12;
int xled = 7;
int yled = 8;

void setup() 
{
  pinMode(xpot, INPUT);				//
  pinMode(ypot, INPUT);
  pinMode(pir, INPUT);
  pinMode(xled, OUTPUT);
  pinMode(yled, OUTPUT);
  Serial.begin(9600);  
  digitalWrite(xled, LOW);
  digitalWrite(yled, LOW);
}

// the loop routine runs over and over again forever:
void loop() 
{
  int xpot = analogRead(A0);
  int ypot = analogRead(A1);
  Serial.println(xpot);
  Serial.println(ypot);
  
if(pir == LOW)
{
  if(300 < xpot && xpot < 400)
  {
    digitalWrite(xled, HIGH);
  }
  if(300 < ypot && ypot <400)
  {
    digitalWrite(yled, HIGH);
  } 
  if(700 < xpot && xpot < 800)
  {
    digitalWrite(xled, LOW);
  }
  if(700 < ypot && ypot < 800)
  {
    digitalWrite(yled, LOW);
  }
}
else
{
  digitalWrite(xled, LOW);
  digitalWrite(yled, LOW);
}
}

Does anyone have any suggestions as to how I could get it working? I have included a picture of my circuit as well

I apologize in advance for any simple mistakes I may have looked over, as I am rather new to this.

Change the first check in each if from < to >

The ifs are fine; they worked before.

You have to digitalRead the pir pin. You are not reading the state of the button, just the pin number you assigned. It's always not LOW.

alto777

alto777:
The ifs are fine; they worked before.

You have to digitalRead the pir pin. You are not reading the state of the button, just the pin number you assigned. It’s always not LOW.

alto777

I did that and the LED’s started responding to the potentiometers again, but now only one can be on at a time. If I have one on then turn the other potentiometer up to where it would turn on, the first LED turns off. The button does work now, however.

int xpot = A0;
int ypot = A1;
int pir = 12; // can be byte
int xled = 7;// can be byte
int yled = 8;// can be byte
 void setup()
{
  pinMode(xpot, INPUT);                                                              //
  pinMode(ypot, INPUT);
  pinMode(pir, INPUT_PULLUP); // turn on internal pullup resistor to avoid floating pin
  pinMode(xled, OUTPUT);
  pinMode(yled, OUTPUT);
  Serial.begin(9600); 
  digitalWrite(xled, LOW);
  digitalWrite(yled, LOW);
}
 // the loop routine runs over and over again forever:
void loop()
{
 xpot = analogRead(A0); // don't need to re-declare as int
 ypot = analogRead(A1); // don't need to re-declare as int
  Serial.println(xpot);
  Serial.println(ypot);
if(digitalRead(pir) == LOW) // need to read the pir pin
{
  if(300 < xpot && xpot < 400)  // looks odd,  I would use xpot >=300 && xpot <=400 to make it more clear
  {
    digitalWrite(xled, HIGH);
  }
  if(300 < ypot && ypot <400)  // >=300 && <=400
  {
    digitalWrite(yled, HIGH);
  }
  if(700 < xpot && xpot < 800)  // >=700 & <=800
  {
    digitalWrite(xled, LOW);
  }

  if(700 < ypot && ypot < 800) // >=700 & <=800
  {
    digitalWrite(yled, LOW);
  }

// Deleted 'else' section, contradicted "leaving it on even when the range is left", was also tied to the if above, and not have xled independent
 }
}

Thanks! It's working great now :slight_smile:

// turn on internal pullup resistor to avoid floating pin

not the problem - there was a pullup resistor on the bread board.

// Deleted ‘else’ section, contradicted “leaving it on even when the range is left”, was also tied to the if above, and not have led independent

Not really, it wasn’t. Now how would the button turn off the LEDs? The structure was correct syntax and logic.

if (the button isn’t pressed) do the pot thing
else (the button is pressed) turn off the LEDs

If you click the cursor to the right of a open brace, the IDE will highlight the matching closing brace. I know you all know that, but it is easy to forget.

Either LED would pop right back on if its pot was in range when the button was released. There was no rule about that.

Since no one likes

…(300 < y && y < 400)

, I’ll just say it is not a mistake, but does lead to thinking that

…(300 < x < 400)

like from regular math might work. Not so much! Frankly my probe;em with it is that I have neither intimate familiarity nor total confidence in operator precedence rules, so I’ll always want to see a few extra (free!) parenthesis in there, viz:

if ((x > 300) && (x < 500)) blah blah blah;

However, sometimes writing something oddly can help. I think the classic example is

if (300 == x) whatever;

something I don’t think ever caught on, but it certainly would flag miswriting “=” where “==” was called for.

alto777