Can I switch two programs in one code? I am making airmouse now. I want to make it a joystick mode and a accelerometer mode. And I want to switch it in a button. What should I do?
This is my code.
#include <Esplora.h>
void setup()
{
}
void loop()
{
int xAxis = Esplora.readAccelerometer(X_AXIS); // read the X axis
int yAxis = Esplora.readAccelerometer(Y_AXIS); // read the Y axis
int mouseX = map(xAxis, -512, 512, 8, -8);
int slider = Esplora.readSlider()/2;
int left_button = Esplora.readButton(1);
int right_button = Esplora.readButton(3);
int up_button = Esplora.readButton(2);
int down_button = Esplora.readButton(4);
/*int speed_button = Esplora.readJoystickButton();
int Speed = */
Serial.print("\nx : "); // print the label for X
Serial.print(xAxis); // print the value for the X axis
Serial.print("\ty : "); // print a tab character, then the label for Y
Serial.print(yAxis);
Serial.print("\tslider/2 :");
Serial.print(slider);
if(yAxis<-60)
{
int mouseX = map(-yAxis+slider+30, -512, 512, -10, 10);
Mouse.move(mouseX, 0, 0);
}
if(yAxis>90)
{
int mouseX = map(-yAxis-slider, -512, 512, -10, 10);
Mouse.move(mouseX, 0, 0);
}
if(xAxis<-60)
{
int mouseY = map(-xAxis+slider, -512, 512, 10, -10);
Mouse.move(0, mouseY, 0);
}
if(xAxis>40)
{
int mouseY = map(-xAxis-slider, -512, 512, 10, -10);
Mouse.move(0, mouseY, 0);
}
if (left_button == 0) {
Mouse.press();
}
else
{
Mouse.release();
}
if (right_button == 0) {
Mouse.press(MOUSE_RIGHT);
} else {
Mouse.release(MOUSE_RIGHT);
}
if (up_button == 0) {
Mouse.move(0, 0, 1);
delay(100);
}
if (down_button == 0) {
Mouse.move(0, 0, -1);
delay(100);
}
/*int xValue = Esplora.readJoystickX();
int yValue = Esplora.readJoystickY();
int button = Esplora.readButton(2);
int right_button = Esplora.readButton(4);
int middle_button = Esplora.readJoystickSwitch();
int up = Esplora.readButton(3);
int down = Esplora.readButton(1);
Serial.print("Joystick X: ");
Serial.print(xValue);
Serial.print("\tY: ");
Serial.print(yValue);
Serial.print("\tButton: ");
Serial.print(button);
Serial.print("\n");
int mouseX = map(xValue, -512, 512, 8, -8);
int mouseY = map(yValue, -512, 512, -8, 8);
Mouse.move(mouseX, mouseY, 0);
if (button == 0) {
Mouse.press();
}
else
{
Mouse.release();
}
if (right_button == 0) {
Mouse.press(MOUSE_RIGHT);
} else {
Mouse.release(MOUSE_RIGHT);
}
if (middle_button == 0) {
Mouse.press(MOUSE_MIDDLE);
} else {
Mouse.release(MOUSE_MIDDLE);
}
if (up == 0) {
Keyboard.press(KEY_UP_ARROW);
} else {
Keyboard.release(KEY_UP_ARROW);
}
if (down == 0) {
Keyboard.press(KEY_DOWN_ARROW);
} else {
Keyboard.release(KEY_DOWN_ARROW);
}
delay(10);*/
delay(10); // wait half a second (500 milliseconds)
}
Annotations are the joystick mode.