🔍

TWO JOYSTICKS AND ONE MATRIX

// esp32 with analog joystick in >>> variables from 0-4095


#include <Arduino.h>

// analog joysticks need to be connected with 3.3V!
 
#define J1XPIN 34
#define J1YPIN 35

#define J2XPIN 32
#define J2YPIN 33

int j1_x_raw;
int j1_y_raw;

int j2_x_raw;
int j2_y_raw;
 
void setup()
{  
  Serial.begin(9600);
}
void loop()
{
  j1_x_raw = analogRead(J1XPIN);
  j1_y_raw = analogRead(J1YPIN);

   j2_x_raw = analogRead(J2XPIN);
   j2_y_raw = analogRead(J2YPIN);
  
  float j1x = float(j1_x_raw) * 0.00024420024;
  float j1y = float(j1_y_raw)* 0.00024420024;
 
  float j2x = float(j2_x_raw)* 0.00024420024;
  float j2y = float(j2_y_raw)* 0.00024420024;

  
  Serial.print (int(j1x*100));
 Serial.print("  = ");
   Serial.print(int(j1y*100));
Serial.print(" // ");
    Serial.print (int(j2x*100));
 Serial.print(" = ");
   Serial.println(int(j2y*100));


  
  delay(100);
}


#include <Arduino.h>

#include "LedControl.h"

int joy1X = 0;
int joy1Y = 1;
bool joy1BTN = false;
int p1BTN_PIN = 14;


int joy2X = 2;
int joy2Y = 3;
bool joy2BTN = false;
int p2BTN_PIN = 10;

/*
 Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn 
 pin 11 is connected to the CLK 
 pin 10 is connected to LOAD 
 We have only a single MAX72XX.
 */
LedControl lc=LedControl(16,9,8,1);


void drawGATE(int _x, int _y){

  lc.setLed(0,_x-1,_y,true);
  lc.setLed(0,_x+1,_y,true);
  lc.setLed(0,_x,_y-1,true);
  lc.setLed(0,_x,_y+1,true);


}

int getArrayIndex(int x, int y) {
  if (x < 0 || x >= 7 || y < 0 || y >= 7) {
    // Invalid position
    // You may choose to handle the error in a specific way, like printing an error message or taking appropriate action
    // For simplicity, let's just return -1 to indicate an error
    return -1;
  }

  int index = y * 7 + x;
  return index;
}
 
bool buffer[64];

 //-------------

void setup() {
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,2);
  /* and clear the display */
  lc.clearDisplay(0);

  pinMode(p1BTN_PIN,INPUT_PULLUP);
  pinMode(p2BTN_PIN,INPUT_PULLUP);

  lc.setLed(0,3,3,1);

  for(int i=0;i<64;i++){
    buffer[i]=false;

    if(random(100)>80){
      buffer[i]=true;
    }
  }
}

float px2 = 2;
float py2 = 2;

float ax2 = -.02;
float ay2 = .03;


float px = 2;
float py = 2;

float ax = .2;
float ay = .4;

int p1_direct_x = 0;
int p1_direct_y = 0;

int p1_in_x = 0;
int p1_in_y = 0;

int p2_in_x = 0;
int p2_in_y = 0;

int p2_mapped_x = 0;
int p2_mapped_y = 0;

int p1_mapped_x = 0;
int p1_mapped_y = 0;

int animtick = 0;

bool a_phase = false;

void drawBuffer(){

  int tick=0;

  for(int x=0;x<8;x++){

    for(int y=0;y<8;y++){


        if( buffer[tick] == true){
          lc.setLed(0,x,y,true);
        }else{
          lc.setLed(0,x,y,false);
        }

      tick++;
    }


  }


}


void loop() {



  p1_in_x = analogRead(joy1X);
  p1_in_y = analogRead(joy1Y);

  p2_in_x = analogRead(joy2X);
  p2_in_y = analogRead(joy2Y);

  joy1BTN = digitalRead(p1BTN_PIN);

  joy2BTN = digitalRead(p2BTN_PIN);


  p1_mapped_x = int(map(p1_in_x, 0,1023, -100, 100));
  p1_mapped_y = int(map(p1_in_y, 0,1023, -100, 100));

  p1_direct_x = int(map(p1_in_x, 0,1023, 0, 7));
  p1_direct_y = int(map(p1_in_y,0,1023,0,7) );

  p2_mapped_x = int(map(p2_in_x, 0,1023, -100, 100));
  p2_mapped_y = int(map(p2_in_y, 0,1023, -100, 100));
   

//lc.clearDisplay(0);

drawBuffer();

px += ax;
py += ay;

px2 += p2_mapped_x*.002;
py2 += p2_mapped_y*.002;


if(px>7 || px <=0){ ax*=-1;}
if(py>7 || py<=0){ ay*=-1;}

if(px2>7  ){ px2=7;}
if(py2>7  ){ py2=7;}

if(px2<0  ){ px2=0;}
if(py2<0  ){ py2=0;}
 

  if(joy1BTN != 0){
   // lc.setLed(0,p1_direct_x,p1_direct_y,true);
  }else{
   // drawGATE(p1_direct_x,p1_direct_y);
  }



  if(joy2BTN != 0){

    int bid = getArrayIndex(int(px2),int(py2));

    buffer[bid] = !buffer[bid];

  }

  lc.setLed(0,px2,py2,a_phase);
  

  if(animtick>10){
       animtick=0;
       a_phase=true;
  }else{
      animtick++;
      if(animtick>8){
         a_phase=false;
      }
  }
  
  

    delay(10);
  // put your main code here, to run repeatedly:
}