How to get this to work 1 analog to 360 emulator multi buttons

#include "UnoJoy.h"

void setup(){
  setupPins();
  setupUnoJoy();
}

void loop(){
  // Always be getting fresh data
  dataForController_t controllerData = getControllerData();
  setControllerData(controllerData);
}

void setupPins(void){
  // Set all the digital pins as inputs
  // with the pull-up enabled, except for the 
  // two serial line pins
  for (int i = 2; i <= 12; i++){
    pinMode(i, INPUT);
    digitalWrite(i, HIGH);
  }
  pinMode(A4, INPUT);
  digitalWrite(A4, HIGH);
  pinMode(A5, INPUT);
  digitalWrite(A5, HIGH);
}

dataForController_t getControllerData(void){
  
  // Set up a place for our controller data
  //  Use the getBlankDataForController() function, since
  //  just declaring a fresh dataForController_t tends
  //  to get you one filled with junk from other, random
  //  values that were in those memory locations before
  dataForController_t controllerData = getBlankDataForController();
  // Since our buttons are all held high and
  //  pulled low when pressed, we use the "!"
  //  operator to invert the readings from the pins
  controllerData.triangleOn = !digitalRead(2);
  controllerData.circleOn = !digitalRead(3);
  controllerData.squareOn = !digitalRead(4);
  controllerData.crossOn = !digitalRead(5);
  controllerData.dpadUpOn = !digitalRead(6);
  controllerData.dpadDownOn = !digitalRead(7);
  controllerData.dpadLeftOn = !digitalRead(8);
  controllerData.dpadRightOn = !digitalRead(9);
  controllerData.l1On = !digitalRead(10);
  controllerData.r1On = !digitalRead(11);
  controllerData.selectOn = !digitalRead(12);
  controllerData.startOn = !digitalRead(A4);
  controllerData.homeOn = !digitalRead(A5);

  int analogPin = A0;     //analog input 0
int analogPin1 = A1;     // analog input 1

int val0 = 0;           // variable to store the value read of a0
int val1 = 0;           // variable to store the value read of a1
int ledPin = 13;        // LED connected to digital pin 13
  
  // Set the analog sticks
  //  Since analogRead(pin) returns a 10 bit value,
  //  we need to perform a bit shift operation to
  //  lose the 2 least significant bits and get an
  //  8 bit number that we can use  
  controllerData.leftStickX = analogRead(A0) >> 2;
  controllerData.leftStickY = analogRead(A1) >> 2;
  controllerData.rightStickX = analogRead(A2) >> 2;
val0 = analogRead(analogPin);    // read the input a0 pin
if (val0  > 200)
{
controllerData.triangleOn = !digitalRead(2);
}
  // And return the data!
  return controllerData;
}

i want a0 to digital button in 360 emulator

What does the code do and how is that different than what you want?

What is a 360 emulator?

1 Like

Difficult to help you if you do not post all the code and tell us about your project, its objective, ...

All I can see is that you pulled some code from https://forum.arduino.cc/t/reading-two-analog-inputs/463549

![x360ce_4_General|517x500](upload://w9IhDGfUjit 1ZEJUHGCCAWg205L.png)
This is 360 emulator

In the code almost at the bottom you see
Val0 analogread so that kinda works but not the way I would like to

So A0 analog I want it to be 3 buttons from 0 to 100 from 200 to 300 another button from 400 to 500 another button

I want it to make a h shifter with 2 potentiometers

Maybe this video will help you.

Yeah the way this is working I have it I followed his steps
But I want 1 analog to be 3 buttons

The code I posted is the same he is using I just made a few changes

One or two potentiometers?

Something like this might help:

int joystickState = (analogRead(analogPin)/103)*10 
   + analogRead(analogPin1)/103);
switch(joystickState){
  case 20: // x,y = 206-308,0-102
     controllerData.triangleOn = !digitalRead(2);
     break;
  case 31 ... 37 : // x,y in 309-412, 103-823
  case 21 ... 27 : // x,y = 206-308,103-823
     controllerData.???? = !digitalRead(2);
     break;
  ...
 }

I divided by 103 instead of 100 to make sure 0-1023 will distribute across the digits 0-9.

... and here's a small game using this scheme:

I want to use 2

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