Making Joystick/Potentiometer Work Like a Counting Button


newbie here playing around, learning through experimentation, etc.

i've connected 6 leds pwm digital pins (uno) , ps2 style single joystick.

comments @ beginning of code describes setup. questions follow.

code: [select]
/*
   joystick experimenting
   ps2 joystick 2 potentiometers , button (press down on joystick)
   6 leds in column connected w/ current-limiting resistors 6 pwm capable 'digital' pins
   y-axis input changes led lit
   x-axis input changes brightness of led lit
   button press toggles leds on or off
*/

int ledarray[] = {3, 5, 6, 9, 10, 11}; //the 6 pins used leds
boolean running = false; //button press starting value write 0

void setup() {
  for (int index = 0; index < 6; index++) //could replace 6 pincount auto sizeof array
  {
    pinmode(ledarray[index], output); //sets , pwm pins output using array index sequence
//not defining pinmode inputs, pins default inputs unless made outputs
    digitalwrite(a2, high); //turn on pullup resistor b button, constant 1/high reading when not pressed
  }
  serial.begin(9600); //for sensor monitoring on serial monitor
}

void loop() {

  int sensorx = analogread(a0); //analog reads return values 0 1023. joystick in neutral position reads 524
  int sensory = analogread(a1); //joystick in neutral position reads 505
  int sensorb = digitalread(a2); //not pressed 1, pressed 0
  serial.print(sensorx);
  serial.println(sensory); //shows both x , y axis reading values on serial monitor

  sensorx = map(sensorx, 0, 1023, 5, 255); //left right brightness, low end of 5 have @ least light
  sensory = map(sensory, 0, 1023, 5, 0); //up down position if spring-pot-type-auto-return-to-home

  analogwrite(ledarray[sensory], sensorx);
  delay(10);
  digitalwrite(ledarray[sensory], low);

  if (digitalread(a2) == low) //while button pressed, reading of constant 1/high changes 0/low
  {
    delay(150); //slow down samples taken, "debounces". holding button cause blink @ rate.
    running = !running; //started false/0, turns true/1
    for (int index = 0; index < 6; index++)
      digitalwrite(ledarray[index], running); //first time writes 1, second time writes 0, etc
  }
}




as is, joystick's y-axis picks led lit up. constantly. letting joystick return neutral moves led 'starting' one. first goal, goal accomplished, ready move on.

now, want make joystick work in 'simpler' way of selecting lit led.
push joystick up, led position changes on 1 led in 1 direction.
push joystick down, led position changes on 1 led in other direction.
i assume delay needed doesn't scroll @ sampling rate, i'll try fancy , long pause after first position change. pushing joystick cause switch, continued holding scroll faster

but first things first. need taking joystick input , causing change in position, remembers change ignores joystick returning neutral await next input of or down. shifted led or down one, didn't remember.

i can't make sense of samples i've seen referring "last position" stuff.

edit: took out confusion causing code, wasn't in entirety anywho.

in commented out code - how see ever possible?

if (sensory >= 525 && sensory <= 500)
    {change = 0;
    }



but commented out code today has no chance of working because @ beginning of loop map values between (y) 0 , 5 or (x) 5 , 255 , compare larger values such 500.

suggest remember when experimenting again code


so need test got states , wait 'going neutral' before changing again @ next pass in loop. need memorize led selected , @ pwm not rely on read potentiometers



Arduino Forum > Using Arduino > Programming Questions > Making Joystick/Potentiometer Work Like a Counting Button


arduino

Comments

Popular posts from this blog

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode - Raspberry Pi Forums

class MPU6050 has no member named begin

missing filename after '-o'