Joystick and led

Im using 9 LED in a shape of a cross, 1 in the middle 2 on each edge. I want to control the lights of the LED with a joystick. For example, if the joystick is tilted all the way to the left, the LED on the far left edge will light up. If the joy stick is tilted slightly to the left, the LED between the far left LED and the middle LED will light up. Here is my sketch could you all please help me?

int joystick_x = A0; int joystick_y= A1;

void setup()
{
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
Serial.begin(9600);
}

void loop() {
int val_0 = analogRead(A0);
int val_1 = analogRead(A1);
Serial.print("VRx : ");
Serial.print(val_0);
Serial.print(" ");
Serial.print("VRy : ");
Serial.println(val_1);
delay(1000);
if (val_0 <250 && 509 >val_1>505) {
digitalWrite(6,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
else if (val_0>250 && val_0 < 505 && 509 >val_1> 505) {
digitalWrite(5,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
else if (515 > val_0> 514 && val_1< 250){
digitalWrite(3,HIGH);
digitalWrite(6,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
}
else if (515 >val_0> 514 && val_1>250 && val_1 <505) {
digitalWrite(4,HIGH);
digitalWrite(3,LOW);
digitalWrite(6,LOW);
digitalWrite(5,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
else if (val_0 <800 && val_0> 515 && 507 >val_1> 505){
digitalWrite(9,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(6,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
else if (val_0 > 800 && 507 > val_1> 505) {
digitalWrite(10,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(6,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
}
else if (515 > val_0> 514 && val_1 >508 && val_1 < 800){
digitalWrite(7,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
else if (515 > val_0> 514 && val_1 >800) {
digitalWrite(8,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(7,LOW);
digitalWrite(6,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);

}
else if (515 > val_0 > 514 && 507 > val_1> 505) {
digitalWrite(11,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(6,LOW);
}
}

I moved your topic to an appropriate forum category @user1219.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

I have deleted your other cross-post @user1219.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Did you intend for this range to be impossible to reach?

Hi, @user1219

When you load your code, what happens?

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks... Tom... :grinning: :+1: :coffee: :australia:

If I got the setup right, all LEDs remain off. I extended the range of the joystick/conditional statements and some LEDs worked. I would guess that the "up, down, left, right" (of joystick and LEDs) is the goal.

int joystick_x = A0; int joystick_y = A1;

void setup()
{
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int val_0 = analogRead(A0);
  int val_1 = analogRead(A1);
  Serial.print("VRx : ");
  Serial.print(val_0);
  Serial.print(" ");
  Serial.print("VRy : ");
  Serial.println(val_1);
  delay(1000);
  // if (val_0 < 250 && 509 > val_1 > 505) {
  if (val_0 < 250 && val_1 > 512 && val_1 < (512 + 128)) {
    digitalWrite(6, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
  }
  // else if (val_0 > 250 && val_0 < 505 && 509 > val_1 > 505) {
  else if (val_0 > 250 && val_0 < 512 && val_1 > 512) {
    digitalWrite(5, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
  }
  // else if (515 > val_0 > 514 && val_1 < 250) {
  else if (val_0 < 512 && val_1 < 250) {
    digitalWrite(3, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
  }
  // else if (515 > val_0 > 514 && val_1 > 250 && val_1 < 505) {
  else if (val_0 > 512 && val_1 > 250 && val_1 < 505) {
    digitalWrite(4, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
  }
  // else if (val_0 < 800 && val_0 > 515 && 507 > val_1 > 505) {
  else if (val_0 < 800 && val_0 > 512 && val_1 > 512) {
    digitalWrite(9, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(6, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
  }
  // else if (val_0 > 800 && 507 > val_1 > 505) {
  else if (val_0 > 800 && val_1 > 505) {
    digitalWrite(10, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(6, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
  }
  // else if (515 > val_0 > 514 && val_1 > 508 && val_1 < 800) {
  else if (val_0 > 512 && val_1 > 508 && val_1 < 800) {
    digitalWrite(7, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
  }
  // else if (515 > val_0 > 514 && val_1 > 800) {
  else if (val_0 > 512 && val_1 > 800) {
    digitalWrite(8, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);

  }
  // else if (515 > val_0 > 514 && 507 > val_1 > 505) {
  else if (val_0 > 512 && val_1 > 512) {
    digitalWrite(11, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(6, LOW);
  }
}

The .json for the wokwi simulation:

{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": -4.8, "left": 95.5, "attrs": {} },
    { "type": "wokwi-led", "id": "led1", "top": -186, "left": 42.2, "attrs": { "color": "red" } },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": -147.6,
      "left": 42.2,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": -224.4,
      "left": -34.6,
      "attrs": { "color": "red" }
    },
    { "type": "wokwi-led", "id": "led4", "top": -224.4, "left": 119, "attrs": { "color": "red" } },
    { "type": "wokwi-led", "id": "led5", "top": -224.4, "left": 3.8, "attrs": { "color": "red" } },
    {
      "type": "wokwi-led",
      "id": "led6",
      "top": -224.4,
      "left": 80.6,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-led",
      "id": "led7",
      "top": -262.8,
      "left": 42.2,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-led",
      "id": "led8",
      "top": -224.4,
      "left": 42.2,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-led",
      "id": "led9",
      "top": -301.2,
      "left": 42.2,
      "attrs": { "color": "red" }
    },
    { "type": "wokwi-potentiometer", "id": "pot1", "top": -20.5, "left": 278.2, "attrs": {} },
    { "type": "wokwi-potentiometer", "id": "pot2", "top": -10.9, "left": 9.4, "attrs": {} }
  ],
  "connections": [
    [ "nano:3", "led2:A", "green", [ "v0" ] ],
    [ "nano:4", "led1:A", "green", [ "v0" ] ],
    [ "nano:5", "led8:A", "green", [ "v0" ] ],
    [ "nano:6", "led7:A", "green", [ "v0" ] ],
    [ "nano:7", "led9:A", "green", [ "v0" ] ],
    [ "nano:8", "led4:A", "green", [ "v0" ] ],
    [ "nano:9", "led6:A", "green", [ "v0" ] ],
    [ "nano:10", "led5:A", "green", [ "v0" ] ],
    [ "nano:11", "led3:A", "green", [ "v0" ] ],
    [ "nano:GND.2", "led2:C", "black", [ "v0" ] ],
    [ "led2:C", "led1:C", "green", [ "v0" ] ],
    [ "led1:C", "led8:C", "green", [ "v0" ] ],
    [ "led8:C", "led7:C", "green", [ "v0" ] ],
    [ "led7:C", "led9:C", "green", [ "v0" ] ],
    [ "led8:C", "led5:C", "green", [ "v0" ] ],
    [ "led5:C", "led3:C", "green", [ "v0" ] ],
    [ "led8:C", "led6:C", "green", [ "v0" ] ],
    [ "led6:C", "led4:C", "green", [ "v0" ] ],
    [ "pot1:GND", "nano:GND.1", "black", [ "v19.2", "h-67.2" ] ],
    [ "pot1:SIG", "nano:A1", "green", [ "v28.8", "h-154" ] ],
    [ "pot1:VCC", "nano:5V", "red", [ "v38.4", "h-116" ] ],
    [ "pot2:GND", "nano:GND.1", "black", [ "v9.6", "h201.6" ] ],
    [ "pot2:SIG", "nano:A0", "green", [ "v19.2", "h95.6" ] ],
    [ "pot2:VCC", "nano:5V", "red", [ "v28.8", "h152.8" ] ]
  ],
  "dependencies": {}
}

Hi @xfpd,

I'm also not sure about the intention of the TO but it's probably something like this?

/*
  Forum:https://forum.arduino.cc/t/joystick-and-led/1201691
  Wokwi:https://wokwi.com/projects/384488613015669761

*/

constexpr byte joystick_x = A0;
constexpr byte joystick_y = A1;
constexpr byte noOfLeds {5};
constexpr byte xAxisPins[noOfLeds] = {3, 4, 5,  6,  7};
constexpr byte yAxisPins[noOfLeds] = {8, 9, 5, 10, 11};

constexpr unsigned long INTERVAL {200};  // Every 200 ms (1/5 th of a second)
unsigned long lastUpdate = 0;

int valueX;
int valueY;
int ledX;
int ledY;

void setup()
{
  for (int i = 0; i < noOfLeds; i++ ) {
    pinMode(xAxisPins[i], OUTPUT);
    pinMode(yAxisPins[i], OUTPUT);
  }
  Serial.begin(9600);
}

void loop() {
  if (millis() - lastUpdate > INTERVAL) {
    lastUpdate = millis();
    valueX = analogRead(A0);
    valueY = analogRead(A1);
    ledX = map(valueX, 0, 1024, 0, 5);
    ledY = map(valueY, 0, 1024, 0, 5);
    setLeds(ledX, ledY);
    printData();
  }
}

void setLeds(byte lx, byte ly) {
  for (int i = 0; i < noOfLeds; i++ ) {
    digitalWrite(xAxisPins[i], i == lx ? HIGH : LOW);
    digitalWrite(yAxisPins[i], i == ly ? HIGH : LOW);
  }
  if (lx == 2 || ly == 2) {
    digitalWrite(xAxisPins[2], HIGH);
  }
}

void printData() {
  Serial.print("VRx : ");
  Serial.print(valueX);
  Serial.print(" ");
  Serial.print("VRy : ");
  Serial.println(valueY);
  Serial.print("Led X : ");
  Serial.print(ledX);
  Serial.print(" ");
  Serial.print("Led Y : ");
  Serial.println(ledY);
}

See here : https://wokwi.com/projects/384488613015669761

image

????
:wink:

Do you know if Wokwi has an "attrs" for the joystick for analog (all values from 0 to 1023) in place of the digital (0, 512, 1023)?

Just a hint: 507 >val_1> 505 is math but not C++.

You had to write

507 >val_1> 505

as

 507 > val_1 && val_1 > 505

which only will become true for val_1 equal 506. So the identical statement would be

 vale_1 == 506

which is much clearer.

But be aware that analogRead() may jitter and you will have some problems to really stay on a specific value ...

Great ... I also tried this component and had no success for the same reason :wink:

So the answer is unfortunately no at this time. If I find a solution I'll let you know, would be nice if you did also give me a hint ... :slight_smile:

(There is another missing feature: I created a resistor ladder but could not read the appropriate values via analogRead() ; seems as if daisy chained resistors are not evaluated by the simulation ?!?)

That's what the doc says:

https://docs.wokwi.com/parts/wokwi-analog-joystick

Partial movement and touch control are not currently supported. We'd love to see them supported though - so if you are up to the task, there's an open issue waiting for your love.

I tried this last year... same conclusion, voltage dividers are not supported. I have only started trying to use "build your own IC"... And sometimes I stumble into wokwi secrets, like a rotary dial (telephone) and INPUT_PULLDOWN (only in build-an-IC), and more.

1 Like

Nice. Now make it so when only the LED in the middle is on, it blinks.

Then add stuff so that if you yank the joystick (or proxy slide fader) from one extreme to the other, the LED illuminated steps through the LEDs in between where it was and where it's heading at some adjustable pace.

a7

Hello user1219

Consider:

//https://forum.arduino.cc/t/joystick-and-led/1201691
//https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
#define ProjectName "Joystick and led"
#define NotesOnRelease "Arduino MEGA tested"
// make names
enum JoySticks {JoyStickX,JoyStickY};
enum Numbers {One,Two,Three,Four,Five}; 
// make variables
constexpr uint8_t PotPins[]  {A0,A1};
constexpr uint8_t LedPinsX[] {3,4,5,6,7};
constexpr uint8_t LedPinsY[] {8,9,5,10,11};
// make structures
struct POT2LEDS
{
  const uint8_t PotPin;
  const uint8_t LedPins[sizeof(LedPinsX)];
  void run()
  {
    for (auto LedPin:LedPins) digitalWrite(LedPin,LOW);
    digitalWrite(LedPins[map(analogRead(PotPin),0,1023,0,5)],HIGH);
  }
}pot2Leds[] 
{
  {PotPins[JoyStickX],LedPinsX[One],LedPinsX[Two],LedPinsX[Three],LedPinsX[Four],LedPinsX[Five]},
  {PotPins[JoyStickY],LedPinsY[One],LedPinsY[Two],LedPinsY[Three],LedPinsY[Four],LedPinsY[Five]},
};
// make support
void heartBeat(const uint8_t LedPin, uint32_t currentMillis)
{
  static bool setUp = false;
  if (setUp == false) pinMode (LedPin, OUTPUT), setUp = true;
  digitalWrite(LedPin, (currentMillis / 500) % 2);
}
// make application
void setup()
{
  Serial.begin(115200);
  Serial.print("Source: "), Serial.println(__FILE__);
  Serial.print(ProjectName), Serial.print(" - "), Serial.println(NotesOnRelease);
  for (auto pot2Led:pot2Leds) for (auto LedPin:pot2Led.LedPins) pinMode(LedPin,OUTPUT);
  Serial.println(" =-> and off we go\n");
}
void loop()
{
  uint32_t currentMillis = millis();
  heartBeat(LED_BUILTIN, currentMillis);
  for (auto pot2Led:pot2Leds) pot2Led.run();
}

Have a nice day and enjoy coding in C++.

That's all possible ... here a version with

  • Blinking center led in case x- and y-axis are centered (use the center led)
  • Levels between two leds where either one or two leds are illuminated plus blinking center led if both axis use the center
  • Possibility to switch between both versions by pressing a button
/*
  Forum:https://forum.arduino.cc/t/joystick-and-led/1201691
  Wokwi:https://wokwi.com/projects/384628140447292417

*/

constexpr byte joystick_x = A0;
constexpr byte joystick_y = A1;
constexpr byte buttonPin  = 12;
constexpr byte noOfLeds {5};
constexpr byte xAxisPins[noOfLeds] = {3, 4, 5,  6,  7};
constexpr byte yAxisPins[noOfLeds] = {8, 9, 5, 10, 11};

;

constexpr unsigned long INTERVAL {200};  // Every 200 ms (1/5 th of a second) update of the led cross
unsigned long lastUpdate = 0;

int valueX;
int valueY;
int ledX;
int ledY;

boolean blinking = false;

enum crossTypes {STANDARD, INTERLEVEL} crossType = STANDARD;

void setup()
{
  pinMode(buttonPin, INPUT_PULLUP);
  for (int i = 0; i < noOfLeds; i++ ) {
    pinMode(xAxisPins[i], OUTPUT);
    pinMode(yAxisPins[i], OUTPUT);
  }
  Serial.begin(9600);
}

void loop() {
  if (millis() - lastUpdate > INTERVAL) {
    lastUpdate = millis();
    switch (crossType) {
      case STANDARD: standardCross();
        break;
      case INTERLEVEL: interLevelCross();
        break;
    }
  }
  handleBlinking();
  if (buttonPressed()){
    switch(crossType){
      case STANDARD: crossType = INTERLEVEL;
                     resetCross();
         break;
      case INTERLEVEL: crossType = STANDARD;
                     resetCross();
         break;
    }
  }
}

void readJoystickMappedTo(byte mapTo) {
  valueX = analogRead(A0);
  valueY = analogRead(A1);
  ledX = map(valueX, 0, 1024, 0, mapTo);
  ledY = map(valueY, 0, 1024, 0, mapTo);
}

// *********************************************************
//                      Standard Cross
// *********************************************************
void standardCross() {
  readJoystickMappedTo(noOfLeds);
  for (int i = 0; i < noOfLeds; i++ ) {
    if (i != 2) {
      digitalWrite(xAxisPins[i], i == ledX ? HIGH : LOW);
      digitalWrite(yAxisPins[i], i == ledY ? HIGH : LOW);
    }
  }
  blinking = false;
  byte mode = 0;
  if (ledX == 2) {
    mode++;
  }
  if (ledY == 2) {
    mode++;
  }
  switch (mode) {
    case 0 :
      digitalWrite(xAxisPins[2], LOW);
      break;
    case 1 :
      digitalWrite(xAxisPins[2], HIGH);
      break;
    case 2 :
      blinking = true;
      break;
  }
}

void handleBlinking() {
  static unsigned long lastBlink = 0;
  if (!blinking) {
    return;
  }
  if (millis() - lastBlink >= 500) {
    lastBlink = millis();
    byte state = digitalRead(xAxisPins[2]);
    digitalWrite(xAxisPins[2], !state);
  }
}

// *********************************************************
//                    interLevel Cross
// *********************************************************
void interLevelCross() {
  constexpr byte noOfLevels {9};
  readJoystickMappedTo(noOfLevels);
  resetCross();
  switchLeds();
}

void switchLeds() {
  int8_t x0 = ledX / 2;
  int8_t x1 = -1;
  if (ledX & 1) {
    x1 = x0 + 1;
  }
  int8_t y0 = ledY / 2;
  int8_t y1 = -1;
  if (ledY & 1) {
    y1 = y0 + 1;
  }
  blinking = false;
  byte mode = 0;
  if (x0 == 2 || x1 == 2) {
    mode++;
  }
  if (y0 == 2 || y1 == 2) {
    mode++;
  }

  if (x0 != 2) {
    digitalWrite(xAxisPins[x0], HIGH);
  }

  if (y0 != 2) {
    digitalWrite(yAxisPins[y0], HIGH);
  }
  if (x1 >= 0 && x1 != 2) {
    digitalWrite(xAxisPins[x1], HIGH);
  }
  if (y1 >= 0 && y1 != 2) {
    digitalWrite(yAxisPins[y1], HIGH);
  }
  switch (mode) {
    case 0:
      break;
    case 1 :
      digitalWrite(xAxisPins[2], HIGH);
      break;
    case 2 :
      blinking = true;
      break;
  }
}

void resetCross() {
  for (int i = 0; i < noOfLeds; i++ ) {
    digitalWrite(xAxisPins[i], LOW);
    digitalWrite(yAxisPins[i], LOW);
  }
}


boolean buttonPressed() {
  static unsigned long lastChange = 0;
  static byte state = HIGH;
  static byte lastState = HIGH;
  byte actState = digitalRead(buttonPin);
  if (actState != lastState) {
    lastChange = millis();
    lastState = actState;
  }
  if (actState != state && millis() - lastChange > 20) {
    state = actState;
    return !state;
  }
  return false;
}




void printData() {
  Serial.print("VRx : ");
  Serial.print(valueX);
  Serial.print(" ");
  Serial.print("VRy : ");
  Serial.println(valueY);
  Serial.print("Led X : ");
  Serial.print(ledX);
  Serial.print(" ");
  Serial.print("Led Y : ");
  Serial.println(ledY);
}

On Wokwi: https://wokwi.com/projects/384628140447292417

I'll leave that "Knight Rider" version to others ... :wink:

Features. Nice.

Without pressing the button (start up mode), I changed your readJoystickMappedTo() to apply a smoothing of the readings I hoped would just show the LED moving toward their goals:

void readJoystickMappedTo(byte mapTo) {
  const float alpha = 0.7;
  static int avgX;
  static int avgY;

  valueX = analogRead(A0);
  valueY = analogRead(A1);

  avgX = alpha * avgX + (1 - alpha) * valueX;
  avgY = alpha * avgY + (1 - alpha) * valueY;

  valueX = avgX;
  valueY = avgY;

  ledX = map(valueX, 0, 1024, 0, mapTo);
  ledY = map(valueY, 0, 1024, 0, mapTo);
}

It's closer to what I'm looking for, but not entirely satisfactory. But for a quick hack on top of your wokwi, not too bad.

I'm too lazy to try another idea that might not work any better. :expressionless:

a7

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.