Glitches using elapsed millis with I2c and esp32

Hi guys

I'm trying to instigate a timer to repeat analog joystick movements so I can move objects in a game constantly while the stick is moved with out having to hit the joystick repeatedly to move the object. The joysticks are those adafruit featherwing joys that can be found here....

They run off I2C lines. And I have both joys connected to he same i2c lines on the huzzah 32 with separate interrupt lines. When I use the seesaw examples found here....

Every thing works. Ive tested it using the serial monitor over and over again. I can move the example to my game program and every thing works fine in game and in serial monitor. But I can only move the player object using the stick once per push making me have to repeat stich pushes repeatedly.

When the elapsedmillis timer repeat is instigated every thing goes hay wire. Up repeats and right repeats but I get no movement or serial print of down or left. Now if I hit the button designated to call my in game menu and then exit it, I then get down in game and in the serial monitor but no left at all. Menu works fine though with no glitches.

ive also tried just using the left function in my game program and nothing. ive even tried this using the twin joystick with the same glitch. So again I have retested the adafruit example with no problems on the serial monitor until repeat left is added

#include "Adafruit_seesaw.h"
#include <elapsedMillis.h>

Adafruit_seesaw ss1;

struct Player
{
  int player_x; 
  int player_y; 
  int room;
  int player_direction; 
  int player_directionRepeat;
};

Player player = { 160, 170, 3, 2, 0};

 #define IRQ_PIN1   13
 #define IRQ_PIN2   27
////////////////////////////////////////////////////////////////////////////
int last_x = 0, last_y = 0;

elapsedMillis MoveRepeat;
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
  int y = ss1.analogRead(2);
   int x = ss1.analogRead(3);
   #define MoveRepeatRate 100 // set this for how long in milis to wait for auto repeat move
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// 
void drawplayer(){

 if(x > 600 && last_x < 600 || ( 1 == player.player_directionRepeat && MoveRepeat >= MoveRepeatRate )){  ////if x is greater than 600 and last x was less than 600 do something
    Serial.println(F("UP")); ///LEFT 
      MoveRepeat = 0;
       player.player_directionRepeat = 1;
     tft.writeRectNBPP(player.player_x, player.player_y,  16, 16, 4, paulrearwa, palette); //// this mixed with the call below it calls bitmaps for walking 
    tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulrearwb, palette); ///// read above
      
     player.player_direction = 1; ////when movement ends player bitmap faces this direction paul rear
     player.player_y -= 4;    ///move player y - 4 pixels 
     if(checkcolision())     
    {
     player.player_y += 4;} ///causes player to stop when collision happens
     player.player_directionRepeat = 0;
    }
    if(player.player_y <= 16){    ////keeps player from walking off the screen. 
        player.player_y = 16;
    }
/////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////// 
 if(x < 400 && last_x > 400 || ( 1 == player.player_directionRepeat && MoveRepeat >= MoveRepeatRate )){
    Serial.println(F("DOWN")); ///LEFT 
     MoveRepeat = 0;
      player.player_directionRepeat = 1;
     tft.writeRectNBPP(player.player_x, player.player_y,16,16,4,paulfrontwa,palette);
    tft.writeRectNBPP(player.player_x, player.player_y,16,16,4,paulfrontwb,palette);
    
    player.player_direction = 2; ////when movement ends player bitmap faces this direction paul rear
     player.player_y += 4;    ///move player y - 4 pixels 
     if(checkcolision())     
    {
     player.player_y -= 4;} ///causes player to stop when collision happens
     player.player_directionRepeat = 0;
    }
    if(player.player_y >= 224){
       player.player_y = 224;
  }
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
 if(y < 400 && last_y > 400 || ( 1 == player.player_directionRepeat && MoveRepeat >= MoveRepeatRate )){
    Serial.println(F("DOWN")); ///LEFT 
     MoveRepeat = 0;
      player.player_directionRepeat = 1;
     tft.writeRectNBPP(player.player_x, player.player_y,16,16,4,paulfrontwa,palette);
    tft.writeRectNBPP(player.player_x, player.player_y,16,16,4,paulfrontwb,palette);
    
    player.player_direction = 2; ////when movement ends player bitmap faces this direction paul rear
     player.player_y += 4;    ///move player y - 4 pixels 
     if(checkcolision())     
    {
     player.player_y -= 4;} ///causes player to stop when collision happens
     player.player_directionRepeat = 0;
    }
    if(player.player_y >= 224){
       player.player_y = 224;
  }
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
 if(y > 600 && last_y < 600 || ( 1 == player.player_directionRepeat && MoveRepeat >= MoveRepeatRate )){
    Serial.println(F("RIGHT")); ///UP
     MoveRepeat = 0;
      player.player_directionRepeat = 1;
     tft.writeRectNBPP(player.player_x, player.player_y,16,16,4,paulrightw,palette);
    tft.writeRectNBPP(player.player_x, player.player_y,16,16,4,paulright,palette);
    
    player.player_direction = 4;
    player.player_x += 4;
  if(checkcolision())
  {
    player.player_x -= 4;}
     player.player_directionRepeat = 0;
  }
  if(player.player_x <= 16){
      player.player_x = 16;
 }
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////
if(!digitalRead(IRQ_PIN2)){
    uint32_t buttons = ss2.digitalReadBulk(button_mask2);
if (! (buttons & (1 << BUTTON_X))) {  /// BUTTON A RIGHT SIDE 
  state = STATE_Menu;             
  }
 }
 
///////////////////////////////////////////////////////////////////////////////     
//////////////////////////////PLAYER DIRECTION/////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
if (player.player_direction == 1){
  tft.writeRectNBPP(player.player_x, player.player_y,16,16,4,paulrear,palette);
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
else if (player.player_direction == 2){
   tft.writeRectNBPP(player.player_x, player.player_y,16,16,4,paulfront,palette);
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
else if (player.player_direction == 3){
    tft.writeRectNBPP(player.player_x, player.player_y,16,16,4,paulleft,palette);
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
else if (player.player_direction == 4){
     tft.writeRectNBPP(player.player_x, player.player_y,16,16,4,paulright,palette);
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////        
/////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////for use with movey blocks////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
else if (player.player_direction == 5){
     tft.writeRectNBPP(player.player_x, player.player_y,16,16,4,paulrearwa,palette);
}     
else if (player.player_direction == 6){
     tft.writeRectNBPP(player.player_x, player.player_y,16,16,4,paulfrontwa,palette);
}     
else if (player.player_direction == 7){
     tft.writeRectNBPP(player.player_x, player.player_y,16,16,4,paulleftw,palette);
}          
else if (player.player_direction == 8){
     tft.writeRectNBPP(player.player_x, player.player_y,16,16,4,paulrightw,palette); 
     }  
  }

Is it THAT hard to use Tools + Auto Format BEFORE posting code here? Your code looks like it was typed by a drunken monkey. Sober that animal up.

Honestly just learned to do that.

Any thing you can think of thats causing the unusual glitches?

Duhjoker:
Any thing you can think of thats causing the unusual glitches?

I can't think well when I get dizzy.

I get
dizzy reading
code that
jerks around
randomly.

if(youPosted)
{
   codeThatCouldBeRead();
   thenPerhapsWeCould();
   seeWhatTheProblemIs();
}

Sorry it got way too late for me last night but I woke up today and did the auto format.......

#include "Adafruit_seesaw.h"
#include <elapsedMillis.h>

Adafruit_seesaw ss1;

struct Player
{
    int player_x;
    int player_y;
    int room;
    int player_direction;
    int player_directionRepeat;
};

Player player = { 160, 170, 3, 2, 0};

 #define IRQ_PIN1   13
 #define IRQ_PIN2   27
////////////////////////////////////////////////////////////////////////////
int last_x = 0, last_y = 0;

elapsedMillis MoveRepeat;
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
  #define MoveRepeatRate 100 // set this for how long in milis to wait for auto repeat move
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void drawplayer() {
 int y = ss1.analogRead(2);
   int x = ss1.analogRead(3);
   
   if (x > 600 && last_x < 600 || ( 1 == player.player_directionRepeat && MoveRepeat >= MoveRepeatRate )) {
      ////if x is greater than 600 and last x was less than 600 do something
        Serial.println(F("UP")); ///LEFT
          MoveRepeat = 0;
           player.player_directionRepeat = 1;
         tft.writeRectNBPP(player.player_x, player.player_y,   16, 16, 4, paulrearwa, palette); //// this mixed with the call below it calls bitmaps for walking
        tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulrearwb, palette); ///// read above
         
         player.player_direction = 1; ////when movement ends player bitmap faces this direction paul rear
         player.player_y -= 4;     ///move player y - 4 pixels
         if (checkcolision())     
          {
           player.player_y += 4;
    } ///causes player to stop when collision happens
         player.player_directionRepeat = 0;
       
  }
      if (player.player_y <= 16) {
        ////keeps player from walking off the screen.
            player.player_y = 16;
       
  }
  /////////////////////////////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////////////////////////////
   if (x < 400 && last_x > 400 || ( 1 == player.player_directionRepeat && MoveRepeat >= MoveRepeatRate )) {
        Serial.println(F("DOWN")); ///LEFT
         MoveRepeat = 0;
          player.player_directionRepeat = 1;
         tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulfrontwa, palette);
        tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulfrontwb, palette);
       
        player.player_direction = 2; ////when movement ends player bitmap faces this direction paul rear
         player.player_y += 4;     ///move player y - 4 pixels
         if (checkcolision())     
          {
           player.player_y -= 4;
    } ///causes player to stop when collision happens
         player.player_directionRepeat = 0;
       
  }
      if (player.player_y >= 224) {
           player.player_y = 224;
     
  }
  ///////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////
   if (y < 400 && last_y > 400 || ( 1 == player.player_directionRepeat && MoveRepeat >= MoveRepeatRate )) {
        Serial.println(F("DOWN")); ///LEFT
         MoveRepeat = 0;
          player.player_directionRepeat = 1;
         tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulfrontwa, palette);
        tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulfrontwb, palette);
       
        player.player_direction = 2; ////when movement ends player bitmap faces this direction paul rear
         player.player_y += 4;     ///move player y - 4 pixels
         if (checkcolision())     
          {
           player.player_y -= 4;
    } ///causes player to stop when collision happens
         player.player_directionRepeat = 0;
       
  }
      if (player.player_y >= 224) {
           player.player_y = 224;
     
  }
  ///////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////
   if (y > 600 && last_y < 600 || ( 1 == player.player_directionRepeat && MoveRepeat >= MoveRepeatRate )) {
        Serial.println(F("RIGHT")); ///UP
         MoveRepeat = 0;
          player.player_directionRepeat = 1;
         tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulrightw, palette);
        tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulright, palette);
       
        player.player_direction = 4;
        player.player_x += 4;
      if (checkcolision())
        {
          player.player_x -= 4;
    }
         player.player_directionRepeat = 0;
     
  }
    if (player.player_x <= 16) {
          player.player_x = 16;
     
  }
   ////////////////////////////////////////////////////////////////////////////////
   ////////////////////////////////////////////////////////////////////////////////
  if (!digitalRead(IRQ_PIN2)) {
        uint32_t buttons = ss2.digitalReadBulk(button_mask2);
    if (! (buttons & (1 << BUTTON_X))) {
        /// BUTTON A RIGHT SIDE
        state = STATE_Menu;              
       
    }
     
  }
   
  ///////////////////////////////////////////////////////////////////////////////     
  //////////////////////////////PLAYER DIRECTION/////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////
  if (player.player_direction == 1) {
      tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulrear, palette);
  }
  /////////////////////////////////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////////////////////////////////
  else if (player.player_direction == 2) {
       tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulfront, palette);
  }
  /////////////////////////////////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////////////////////////////////
  else if (player.player_direction == 3) {
        tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulleft, palette);
  }
  /////////////////////////////////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////////////////////////////////
  else if (player.player_direction == 4) {
         tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulright, palette);
  }
  /////////////////////////////////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////////////////////////////////       
  /////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////for use with movey blocks////////////////////////////
  /////////////////////////////////////////////////////////////////////////////////////
  else if (player.player_direction == 5) {
         tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulrearwa, palette);
  }     
  else if (player.player_direction == 6) {
         tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulfrontwa, palette);
  }     
  else if (player.player_direction == 7) {
         tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulleftw, palette);
  }         
  else if (player.player_direction == 8) {
         tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulrightw, palette);
         
  } 
   
}

One thing to note is the joy boards have been rotated 90* and 270*. The serial print and the bitmaps will tell you which direction for sure your looking at.

and did the auto format.......

But posted the same old rubbish...

void drawplayer() {
 int y = ss1.analogRead(2);
   int x = ss1.analogRead(3);
   
   if (x > 600 && last_x < 600 || ( 1 == player.player_directionRepeat && MoveRepeat >= MoveRepeatRate )) {
      ////if x is greater than 600 and last x was less than 600 do something
        Serial.println(F("UP")); ///LEFT
          MoveRepeat = 0;
           player.player_directionRepeat = 1;
         tft.writeRectNBPP(player.player_x, player.player_y,   16, 16, 4, paulrearwa, palette); //// this mixed with the call below it calls bitmaps for walking
        tft.writeRectNBPP(player.player_x, player.pl

Auto Format lines things up neatly, so the code can be read.

Comments have TWO slashes. Any more looks like you have ADHD big time.