Using analog pins instead of digital pins for the Gy-521 acceleromter module

I am trying to use the digital pins A0 and A2 instead of the digital pins 2 and 3 for my Arduino pro micro and GY-521 accelerometer module. I believe I need to change the coding but am not sure what to do. If anyone could help that would be amazing. Here is the existing code i am wanting to edit.

#include <Wire.h>
#include <MPU6050.h>
#include <Mouse.h>

MPU6050 mpu;
int16_t ax, ay, az, gx, gy, gz;
int vx, vy;
int button1 = 6;
int button2 = 7;
int buttonState1 = 0;
int buttonState2 = 0;

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
Wire.begin();
Mouse.begin();
// For versions prior to Arduino 1.0.1
// pinMode(buttonPin, INPUT);
// digitalWrite(buttonPin, HIGH);
// put your setup code here, to run once:
pinMode(button1, INPUT);
digitalWrite(button1, HIGH);
pinMode(button2, INPUT);
digitalWrite(button2, HIGH);
mpu.initialize();
if (!mpu.testConnection()) { while (1); }
}
void loop() {
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

vx = -(gx+150)/150;
vy = (gy-150)/150;

Serial.print("gx = ");
Serial.print(gx);
Serial.print(" | gy = ");
Serial.print(gy);

Serial.print(" | X = ");
Serial.print(vx);
Serial.print(" | Y = ");
Serial.println(vy);

Mouse.move(vx, vy);

attachInterrupt(1, keyboard, CHANGE);
attachInterrupt(7,fire,HIGH);
if (digitalRead(button1) == HIGH) {
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
}
else {
if (Mouse.isPressed(MOUSE_LEFT)) {
Mouse.release(MOUSE_LEFT);
}
}
if (digitalRead(button2) == HIGH) {
if (!Mouse.isPressed(MOUSE_RIGHT)) {
Mouse.press(MOUSE_RIGHT);
}
}
else {
if (Mouse.isPressed(MOUSE_RIGHT)) {
Mouse.release(MOUSE_RIGHT);
}
}
delay(20);
vx = analogRead(vx);
vy = analogRead(vy);
buttonState1 = digitalRead(button1);
Serial.print("X: ");
Serial.print(vx);

Serial.print(" | Y: ");
Serial.print(vy);
Serial.print(" | Button: ");
Serial.println(buttonState1);

// delay(100); // add some delay between reads

}
void keyboard()
{
vx = analogRead(vx);
vy = analogRead(vy);
buttonState1 = digitalRead(button1);
Serial.print("X: ");
Serial.print(vx);

Serial.print(" | Y: ");
Serial.print(vy);
Serial.print(" | Button: ");
Serial.println(buttonState1);

//delay(100); // add some delay between reads

}
void fire()
{

}

you can start by reading the instructions for this forum and Post the code in code tags. I think you missed some definitions but not worth the time to clean up your code. More comments would help you and us.

An I2C module must should be connected to the hardware I2C pins.
There might be a software I2C solution, but that most likely won't be as good.

Question: Why?
Leo..

Perhaps you meant:

vx = analogRead(A0);
vy = analogRead(A2);

It makes no sense to use the same variable for both the analog value (0-1023) and the pin number (A0 or A2).

I am wanting to replace my Arduino pro micro with the Arduino pico and this board only has 3 digital pins. I would need four to complete my project or... I was wondering if i could use two digital and two analog.

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