Joystick controlling LED

I'm trying to use a Joystick to control an LED. I have 2 sets of code I am trying to combine so that I get the correct output on my serial monitor (from Code 1), while my Max 2719 provides a visual representation (which works with Code 2). I've tried combining the 2 codes, which compiles correctly, but I get no LED output. Is there something I'm overlooking in my attempted combination?

Code 1:

int X = A1;
int Y = A0;
int SwitchButton = 2;
int X_axis_location = 0;
int Y_axis_location = 0;
int SW_status = 0;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
  
  pinMode(X, INPUT);
  pinMode(Y, INPUT);


  pinMode(SwitchButton, INPUT_PULLUP); // it is "input" and "with pull-up resistor
  // since we are using the build-in pull-up resistor
  // when the switch is open, the pin "SwitchButton" read high voltage
  // when the switch is closed, the pin "SwitchButton" read low voltage
  

  
}

void loop() 
{
  delay(3000); 
  X_axis_location = analogRead(X);
  Y_axis_location = analogRead(Y);
  SW_status = digitalRead(SwitchButton);
  
  Serial.print("X axis location of the JoyStick ");
  Serial.print('\n');
  Serial.print(X_axis_location);
  Serial.print('\n');
  Serial.print("Y axis location of the JoyStick ");
  Serial.print('\n');
  Serial.print(Y_axis_location);
  Serial.print('\n');
  Serial.print("Voltage of the SW read by Arduino ");
  Serial.print('\n');
  Serial.println(SW_status);
  
  
  
  
  Serial.print('\n');Serial.print('\n');Serial.print('\n');

  
}

Code 2:

int UD = 0;

int LR = 0; //Setting up controller//

#include "LedControl.h" // need the library

LedControl lc=LedControl(8,10,9,1); //10 is to CLOCK, 9 = CS, 8=DIN//

void setup() {

Serial.begin(9600);

lc.shutdown(0,false); // turn off power saving, enables display

lc.setIntensity(0,8); // sets brightness (0~15 possible values)

lc.clearDisplay(0); // clear screen

}

void loop() {

UD = analogRead(A0);

LR = analogRead(A1);

char x_translate = map(LR, 1023, 0, 7, 0); //This maps the values//

char y_translate = map(UD, 1023, 0, 0, 7);

Serial.print("UD = ");

Serial.print(UD, DEC);

Serial.print(", LR = ");

Serial.print(LR, DEC);

Serial.print(", x = ");

Serial.print(x_translate, DEC);

Serial.print(", y = ");

Serial.println(y_translate, DEC);

// not in shutdown mode

lc.clearDisplay(0);

lc.setLed(0,x_translate,y_translate,true);

delay(150); //adjust delay to get your joystick correct//

}

Attempted Combination:

int X = A1;
int Y = A0;
int SwitchButton = 2;
int X_axis_location = 0;
int Y_axis_location = 0;
int SW_status = 0;

#include "LedControl.h"

LedControl lc=LedControl(8,10,9,1);

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);

  pinMode(X, INPUT);
  pinMode(Y, INPUT);

  pinMode(SwitchButton, INPUT_PULLUP);

  lc = LedControl(8,10,9,1);
}

void loop()
{
  delay(3000);
  X_axis_location = analogRead(X);
  Y_axis_location = analogRead(Y);
  SW_status = digitalRead(SwitchButton);

  Serial.print("X axis location of the JoyStick ");
  Serial.print('\n');
  Serial.print(X_axis_location);
  Serial.print('\n');
  Serial.print("Y axis location of the JoyStick ");
  Serial.print('\n');
  Serial.print(Y_axis_location);
  Serial.print('\n');
  Serial.print("Voltage of the SW read by Arduino ");
  Serial.print('\n');
  Serial.println(SW_status);

  char x_translate = map(X, 1023, 0, 7, 0);
  char y_translate = map(Y, 1023, 0, 0, 7);

  lc.clearDisplay(0);
  lc.setLed(0,x_translate,y_translate,true);

  delay(150);
}

Welcome

int X = A1;
int Y = A0;
...
X_axis_location = analogRead(X);
Y_axis_location = analogRead(Y);
...
char x_translate = map(X, 1023, 0, 7, 0);
char y_translate = map(Y, 1023, 0, 0, 7);

Your error is somewhere in the two last lines

Hi, @grishmaksh
Welcome to the forum.
Thanks for using code tags. :+1:

As @guix has pointed out, just look at the variable you have got from the analog inputs and look at the variable you are using in the map functions.

Easily done when beginning to get used to C++ and Arduino.

Tom.. :smiley: :+1: :coffee: :australia:

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