object being moved and then moved back

i am having trouble with a game i have been making. i hooked up some wires that i could place against each other, since i dont have buttons yet, but when i tried it, it would move, and then move back quickly. i questioned wether this was a programming/circuitry issue, or a programming/display issue but i brought it here. if you think im in the wrong place, please redirect me to the right place. here is my code:

#include <LCDWIKI_GUI.h> //Core graphics library
#include <LCDWIKI_KBV.h> //Hardware-specific library



//the definiens of 16bit mode as follow:
//if the IC model is known or the modules is unreadable,you can use this constructed function
LCDWIKI_KBV mylcd(ILI9486,40,38,39,-1,41); //model,cs,cd,wr,rd,reset

//define some colour values
#define BLACK       0x0000
#define BLUE        0x001F
#define RED         0xF800
#define GREEN       0x07E0
#define CYAN        0x07FF
#define MAGENTA     0xF81F
#define YELLOW      0xFFE0
#define WHITE       0xFFFF
#define ORANGE      0xFF8C
#define BROWN       0x79E0
#define LIGHT_GRAY  0xBDF7
#define DARK_GRAY   0x7BEF
#define GREEN       0x07E0

int fps = 20;

int slowness = 1000/fps;

int x = 150;

void setup() {
  mylcd.Init_LCD();
  mylcd.Fill_Screen(CYAN);
  mylcd.Set_Draw_color(LIGHT_GRAY);
  mylcd.Fill_Rectangle(x, 230, x+20, 250);
}

void loop() {
  if (analogRead(A7) > 700) {
    mylcd.Set_Draw_color(CYAN);
    mylcd.Fill_Rectangle(x, 230, x+20, 250);
    x = ++x;
    x = ++x;
    x = ++x;
    x = ++x;
    x = ++x;
    mylcd.Set_Draw_color(LIGHT_GRAY);
    mylcd.Fill_Rectangle(x, 230, x+20, 250);
    delay(slowness);
  }
  
  if (analogRead(A15) > 700) {
    mylcd.Set_Draw_color(CYAN);
    mylcd.Fill_Rectangle(x, 230, x+20, 250);
    x = --x;
    x = --x;
    x = --x;
    x = --x;
    x = --x;
    mylcd.Set_Draw_color(LIGHT_GRAY);
    mylcd.Fill_Rectangle(x, 230, x+20, 250);
    delay(slowness);
  }
  delay(slowness);
}

thanks for all the help youve been giving me forum!

A quick test but definitely has a lot of "switch bounce". Can you post a schematic, not a frizzy thing showing all the connections including power and ground. This will help eliminate the hardware as a problem but not until you get your buttons.

I'm not sure how to get a schematic myself, but here is a link to the wiki for the display I'm using: 3.5inch Arduino Display-Mega2560 - LCD wiki

as you could probably tell from the link, I'm using a mega 2560 with a 3.5 inch display.

as ive continued with my project, i seem to have solved this problem. how i solved the problem: 1. i used actual buttons (it helps a lot to use creditedd hardware), and 2. i made my if statement:

more accurate by making the 700, 1000. so trust that when you atach 5v to an analog piin, 5v is going into the pin, not 4.9 or above (though it should have worked either way).

if you would like to see this project continue, view my github page here.

This topic was automatically closed after 70 days. New replies are no longer allowed.