`
Hi guys.
I'm built a gaming console using two featherwing joysticks an esp32 feather and a 2.4 inch tft. The joys are connected via I2C to the tft then the esp32 fits in the tft wing. Any way I have it working using the seesaw example but when I try to separate the x and y readings into different functions every thing goes hay wire and the serial print runs a constant string of variables showing that its reading joystick movements even though they aren't being moved.
This works fine with the x's and y's separated....
int x = ss1.analogRead(2);
int y = ss1.analogRead(3);
if ( (abs(x - last_x) > 3) || (abs(x + last_x) < 3) || (abs(y - last_y) > 3) || (abs(y + last_y) < 3)) {
Serial.print(x); Serial.print(", "); Serial.println(y);
last_x = x;
last_y = y;
}
but having them do separate operations like so gives me the constant variables.....
int x = ss1.analogRead(2);
int y = ss1.analogRead(3);
if ( (abs(x - last_x) > 3)){
Serial.print("left1");
last_x = x;
}
else if ( (abs(x + last_x) < 3)){
Serial.print("right1");
last_x = x;
}
else if ( (abs(y - last_y) < 3)){
Serial.print("down1");
last_y = y;
}
else if ( (abs(y + last_y) > 3)){
Serial.print("up1");
last_y = y;
}
what could I be doing wrong
#include "Adafruit_seesaw.h"
Adafruit_seesaw ss1;
Adafruit_seesaw ss2;
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
#define BUTTON_RIGHT 6 /////////////what are these integers
#define BUTTON_DOWN 7 /////////////" "
#define BUTTON_LEFT 9 /////////////" "
#define BUTTON_UP 10 /////////////" "
#define BUTTON_SEL 14 /////////////" "
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
#define BUTTON_A 6 /////////////what are these integers
#define BUTTON_B 7 /////////////" "
#define BUTTON_Y 9 /////////////" "
#define BUTTON_X 10 /////////////" "
#define BUTTON_START 14 /////////////" "
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
uint32_t button_mask = (1 << BUTTON_RIGHT) | (1 << BUTTON_DOWN) |
(1 << BUTTON_LEFT) | (1 << BUTTON_UP) | (1 << BUTTON_SEL);
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
uint32_t button_mask2 = (1 << BUTTON_A) | (1 << BUTTON_B) |
(1 << BUTTON_Y) | (1 << BUTTON_X) | (1 << BUTTON_START);
#if defined(ESP8266)
#define IRQ_PIN 2
#elif defined(ESP32)
#define IRQ_PIN1 13
#define IRQ_PIN2 27
#elif defined(NRF52)
#define IRQ_PIN 27
#elif defined(TEENSYDUINO)
#define IRQ_PIN 8
#elif defined(ARDUINO_ARCH_WICED)
#define IRQ_PIN PC5
#else
#define IRQ_PIN 5
#endif
void setup() {
Serial.begin(115200);
while(!Serial);
if(!ss1.begin(0x49)){
Serial.println("ERROR!");
while(1);
}
if(!ss2.begin(0x4a)){
Serial.println("ERROR!");
while(1);
}
else{
Serial.println("seesaw started");
Serial.print("version: ");
Serial.println(ss1.getVersion(), HEX);
}
ss1.pinModeBulk(button_mask, INPUT_PULLUP);
ss1.setGPIOInterrupts(button_mask, 1);
pinMode(IRQ_PIN1, INPUT);
ss2.pinModeBulk(button_mask2, INPUT_PULLUP);
ss2.setGPIOInterrupts(button_mask2, 1);
pinMode(IRQ_PIN2, INPUT);
}
int last_x = 0, last_y = 0;
int last_x2 = 0, last_y2 = 0;
void loop() {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* int x = ss1.analogRead(2);
int y = ss1.analogRead(3);
if ( (abs(x - last_x) > 3) || (abs(x + last_x) < 3) || (abs(y - last_y) > 3) || (abs(y + last_y) < 3)) {
Serial.print(x); Serial.print(", "); Serial.println(y);
last_x = x;
last_y = y;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
int x1 = ss2.analogRead(2);
int y1 = ss2.analogRead(3);
if ( (abs(x1 - last_x2) > 3) || (abs(x1 + last_x2) < 3) || (abs(y1 - last_y2) > 3) || (abs(y1 + last_y2) < 3)) {
Serial.print(x); Serial.print(", "); Serial.println(y);
last_x2 = x1;
last_y2 = y1;
}
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
int x = ss1.analogRead(2);
int y = ss1.analogRead(3);
if ( (abs(x - last_x) > 3)){
Serial.print("left1");
last_x = x;
}
else if ( (abs(x + last_x) < 3)){
Serial.print("right1");
last_x = x;
}
else if ( (abs(y - last_y) < 3)){
Serial.print("down1");
last_y = y;
}
else if ( (abs(y + last_y) > 3)){
Serial.print("up1");
last_y = y;
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/* int x2 = ss2.analogRead(2);
int y2 = ss2.analogRead(3);
if ( (abs(x2 - last_x2) > 3)){
Serial.print("left2");
last_x2 = x2;
}
if ( (abs(x2 + last_x2) < 3)){
Serial.print("right2");
last_x2 = x2;
}
if ( (abs(y2 - last_y2) < 3)){
Serial.print("down2");
last_y2 = y2;
}
if ( (abs(y + last_y) > 3)){
Serial.print("up2");
last_y2 = y2;
}*/
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////KEEP ROTATION OF JOY IN MIND
///////LEFT SIDE ANALOG BOTTOM ROTATION 240*
if(!digitalRead(IRQ_PIN1)){
uint32_t buttons = ss1.digitalReadBulk(button_mask);
//Serial.println(buttons, BIN);
if (! (buttons & (1 << BUTTON_RIGHT))) { //////////// DIRECTION UP
Serial.println("Button X1 pressed");
}
if (! (buttons & (1 << BUTTON_DOWN))) { //////////// DIRECTION RIGHT
Serial.println("Button A1 pressed");
}
if (! (buttons & (1 << BUTTON_LEFT))) { //////////// DIRECTION DOWN
Serial.println("Button B1 pressed");
}
if (! (buttons & (1 << BUTTON_UP))) { //////////// DIRECTION LEFT
Serial.println("Button Y1 pressed");
}
if (! (buttons & (1 << BUTTON_SEL))) {
Serial.println("Button SEL pressed");
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
////////////////KEEP ROTATION OF JOY IN MIND
///////RIGHT SIDE ANALOG TOP ROTATION 90*
if(!digitalRead(IRQ_PIN2)){
uint32_t buttons = ss2.digitalReadBulk(button_mask2);
//Serial.println(buttons, BIN);
if (! (buttons & (1 << BUTTON_B))) { ///////////// DIRECTION LEFT
Serial.println("Button Y pressed");
}
if (! (buttons & (1 << BUTTON_Y))) { ///////////// DIRECTION UP
Serial.println("Button x pressed");
}
if (! (buttons & (1 << BUTTON_X))) { ///////////// DIRECTION RIGHT
Serial.println("Button A pressed");
}
if (! (buttons & (1 << BUTTON_A))) { ///////////// DIRECTION DOWN
Serial.println("Button B pressed");
}
if (! (buttons & (1 << BUTTON_START))) {
Serial.println("Button START pressed");
}
}
delay(10);
}