Button and Analog Code for Micro Pro

Arduino/Teensy software/code questions and discussion
Post Reply
Tok_Tok
Posts: 12
Joined: Mon Sep 05, 2016 7:01 am
Has thanked: 2 times
Been thanked: 10 times

Button and Analog Code for Micro Pro

Post by Tok_Tok » Tue Nov 01, 2016 2:52 am

Hello peeps,

I'm building my own custom gameboy mod (not using the old classic housing) and I want to use an analog stick with it. I have found 2 parts of code for the Arduino Micro Pro and they both work (one is from a topic here) but when I combine them the analog stick stops working. I'm not a coder but I know some basics and tried to adjust the code but I only got it so far that I can upload it to the Micro pro.

Can somebody lend me a hand with making this code work? This is the sketch:

Code: Select all

/*
  JoystickMouseControl

 Controls the mouse from a joystick on an Arduino Leonardo, Micro or Due.
 Uses a pushbutton to turn on and off mouse control, and
 a second pushbutton to click the left mouse button

 Hardware:
 * 2-axis joystick connected to pins A0 and A1
 * pushbuttons connected to pin D2 and D3

 The mouse movement is always relative. This sketch reads
 two analog inputs that range from 0 to 1023 (or less on either end)
 and translates them into ranges of -6 to 6.
 The sketch assumes that the joystick resting values are around the
 middle of the range, but that they vary within a threshold.

 WARNING:  When you use the Mouse.move() command, the Arduino takes
 over your mouse!  Make sure you have control before you use the command.
 This sketch includes a pushbutton to toggle the mouse control state, so
 you can turn on and off mouse control.

 created 15 Sept 2011
 updated 28 Mar 2012
 by Tom Igoe

 this code is in the public domain

 */

#include <Keyboard.h>
#include "Mouse.h"

#define BUTTONS 16

// set pin numbers for switch, joystick axes, and LED:
const int switchPin = 2;      // switch to turn on and off mouse control
const int mouseButton = 3;    // input pin for the mouse pushButton
const int xAxis = A0;         // joystick X axis
const int yAxis = A1;         // joystick Y axis
const int ledPin = 5;         // Mouse control LED

// parameters for reading the joystick:
int range = 12;               // output range of X or Y movement
int responseDelay = 5;        // response delay of the mouse, in ms
int threshold = range / 4;    // resting threshold
int center = range / 2;       // resting position value

boolean mouseIsActive = false;    // whether or not to control the mouse
int lastSwitchState = LOW;        // previous switch state


int keycode_arr[] =
{
  129,  // PIN2   LEFT SHIFT
  32,   // PIN3   Space bar (Button 3)
  130,  // PIN4   LEFT-ALT (Button 2)
  128,  // PIN5   LEFT-CTRL (Button 1)
  215,  // PIN6   RIGHT
  217,  // PIN7   DOWN
  216,  // PIN8   LEFT
  218,  // PIN9   UP
  122,  // PIN10  Z
  176,  // PIN16  ENTER
  108,  // PIN14  (left shoulder - l)
  114,  // PIN15  (right shoulder - r)

  205,  // A2     F12
  17    // A3     CTRL
};

int key_state[BUTTONS];

void setup(){
  
  PRR1 = PRR1 |
         (
           (1 << PRTIM2) |
           (1 << PRTIM3) |
           (1 << PRUSART1)
         );
  PRR0 = PRR0 |
         (
           (1 << PRTWI) |
           (1 << PRSPI) |
           (1 << PRTIM1) |
           (1 << PRADC)
         );

  DDRB = DDRB & ~
         (
           (1 << DDB1) |
           (1 << DDB2) |
           (1 << DDB3) |
           (1 << DDB4) |
           (1 << DDB5) |
           (1 << DDB6)
         );

  PORTB = PORTB |
          (
            (1 << PB1) |
            (1 << PB2) |
            (1 << PB3) |
            (1 << PB4) |
            (1 << PB5) |
            (1 << PB6)
          );

  DDRC = DDRC & ~ (1 << DDC6);
  PORTC = PORTC | (1 << PC6);

  DDRD = DDRD & ~
         (
           (1 << DDD0) |
           (1 << DDD1) |
           (1 << DDD4) |
           (1 << DDD7)
         );
  PORTD = PORTD |
          (
            (1 << PD0) |
            (1 << PD1) |
            (1 << PD4) |
            (1 << PD7)
          );

  DDRE = DDRE & ~ (1 << DDE6);
  PORTE = PORTE | (1 << PE6);

  DDRF = DDRF & ~
         (
           (1 << DDF4) |
           (1 << DDF5) |
           (1 << DDF6) |
           (1 << DDF7)
         );
  PORTF = PORTF |
          (
            (1 << PF4) |
            (1 << PF5) |
            (1 << PF6) |
            (1 << PF7)
          );

  pinMode(ledPin, OUTPUT);         // the LED pin

  Keyboard.begin();
  // take control of the mouse:
  Mouse.begin();
  
}

void loop()
{
  // read the switch:
  int switchState = digitalRead(switchPin);
  // if it's changed and it's high, toggle the mouse state:
  if (switchState != lastSwitchState) {
    if (switchState == HIGH) {
      mouseIsActive = !mouseIsActive;
      // turn on LED to indicate mouse state:
      digitalWrite(ledPin, mouseIsActive);
    }
  }
  // save switch state for next comparison:
  lastSwitchState = switchState;

  // read and scale the two axes:
  int xReading = readAxis(A0);
  int yReading = readAxis(A1);

  // if the mouse control state is active, move the mouse:
  if (mouseIsActive) {
    Mouse.move(xReading, yReading, 0);
  }

  // read the mouse button and click or not click:
  // if the mouse button is pressed:
  if (digitalRead(mouseButton) == HIGH) {
    // if the mouse is not pressed, press it:
    if (!Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.press(MOUSE_LEFT);
    }
  }
  // else the mouse button is not pressed:
  else {
    // if the mouse is pressed, release it:
    if (Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.release(MOUSE_LEFT);
    }
  }

  delay(responseDelay);
  
  int port_b;
  int port_c;
  int port_d;
  int port_e;
  int port_f;

  port_b = PINB;
  port_c = PINC;
  port_d = PIND;
  port_e = PINE;
  port_f = PINF;

  key_state[0] = ((port_d & (1 << PD1)) == 0); // PIN2
  key_state[1] = ((port_d & (1 << PD0)) == 0); // PIN3
  key_state[2] = ((port_d & (1 << PD4)) == 0); // PIN4
  key_state[3] = ((port_c & (1 << PC6)) == 0); // PIN5
  key_state[4] = ((port_d & (1 << PD7)) == 0); // PIN6
  key_state[5] = ((port_e & (1 << PE6)) == 0); // PIN7
  key_state[6] = ((port_b & (1 << PB4)) == 0); // PIN8
  key_state[7] = ((port_b & (1 << PB5)) == 0); // PIN9
  key_state[8] = ((port_b & (1 << PB6)) == 0); // PIN10
  key_state[9] = ((port_b & (1 << PB2)) == 0); // PIN16
  key_state[10] = ((port_b & (1 << PB3)) == 0); // PIN14
  key_state[11] = ((port_b & (1 << PB1)) == 0); // PIN15
  //key_state[12] = ((port_f & (1 << PF7)) == 0); // A0
  //key_state[13] = ((port_f & (1 << PF6)) == 0); // A1
  key_state[14] = ((port_f & (1 << PF5)) == 0); // A2
  key_state[15] = ((port_f & (1 << PF4)) == 0); // A3


  for (int i = 0; i < BUTTONS; i++)
  {
    if (key_state[i] == 1)
    {
      Keyboard.press(keycode_arr[i]);
    }
    else
    {
      Keyboard.release(keycode_arr[i]);
    }
  }
}


/*
  reads an axis (0 or 1 for x or y) and scales the
 analog input range to a range from 0 to <range>
 */

int readAxis(int thisAxis) {
  // read the analog input:
  int reading = analogRead(thisAxis);

  // map the reading from the analog input range to the output range:
  reading = map(reading, 0, 1023, 0, range);

  // if the output reading is outside from the
  // rest position threshold,  use it:
  int distance = reading - center;

  if (abs(distance) < threshold) {
    distance = 0;
  }

  // return the distance for this axis:
  return distance;
}
Would be really great!! Thanks!

User avatar
vnman
Posts: 147
Joined: Tue Jun 21, 2016 5:21 am
Has thanked: 5 times
Been thanked: 34 times

Re: Button and Analog Code for Micro Pro

Post by vnman » Tue Nov 01, 2016 3:44 am

You might want to look at this guide -

http://www.sudomod.com/forum/viewtopic. ... 5&start=30

Tok_Tok
Posts: 12
Joined: Mon Sep 05, 2016 7:01 am
Has thanked: 2 times
Been thanked: 10 times

Re: Button and Analog Code for Micro Pro

Post by Tok_Tok » Tue Nov 01, 2016 3:56 am

Thank you! I'll look into this when I get home.

This is probably exactly what I need.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest