Simple Game Sketch - or so I thought .. .. .

Good Morning Guys

I'm off on my annual quest to provide a simple electronic game for my grandkids for Xmas day !

I made a memory game using coloured squares which appear in a random sequence for a few seconds which then have to be selected in the right sequence using a joystick.
Works OK, but very clunky .. .. .

So I challenged myself to reproduce it using UTFT, URtouch and a touchscreen .. .. . not going too well so far, but I'm still at it !

I'm stuck with a simple trick I can't fathom .. ..

myGLCD.setColor(VGA_WHITE);
myButtons.drawButtons();

}

switch(pressed_button){
case 1(but1==-1);
break;

case 2 (but1==+1);
continue;
}

myGLCD.setColor(VGA_GREEN);
myGLCD.fillRect(0, 30, 319, 225);

.. .. .. a snippet of my code.

The top two and bottom two lines are OK, but I want to halt the loop at this point until 'but1' is pressed .. .. I've tried to do it several ways without success.

I know this snippet is wrong, I've written it like that so that you can see what I'm trying to do

Is there a kind soul around who can help me please ??

Thanks

S

I want to halt the loop at this point until 'but1' is pressed .

I would suggest that is not how you should do it. Rather, introduce a state that checks whether the button has become pressed and if so move to the appropriate state.

Why do I suggest that rather than a simple while loop that checks whether the button has become pressed ? Well, you are already using switch/case so presumably understand that the code for a case runs each time through loop() if no blocking code is used. That would allow you to do several things whilst in the WAITING_FOR_BUTTON_PRESS state such as :

Implementing a time limit for pressing the button
Giving the user a clue after a certain period had elapsed
Flashing an LED, perhaps with increasing frequency to increase the tension as time goes by
Implementing an "abort" button to exit the game
Implementing "show me the required input" functionality
Any or all of the above

Without seeing your full code it is impossible to be sure what the game is doing at the point that you indicate

also this is not the proper syntax for a switch/case

  switch(pressed_button){
     case 1(but1==-1);
     break;
        
   case 2 (but1==+1);
        continue;
  }

and what about those == ?? what are you trying to achieve there? (== is for comparison, = is for assigning a value to a variable, +=1; or -=1; is to increment or decrement a variable by 1)

Well, I did say I knew the code was wrong .. .. .. .

I was hoping that rather than folk telling what's wrong, they'd be able to tell me what's right .. ..

Never mind

Thanks anyway

S

Musicmanager:
I was hoping that rather than folk telling what's wrong, they'd be able to tell me what's right .. ..

how would we do that if we don't understand what you are trying to do?

a switch would be written as

 switch(pressed_button) {
     case 1:
       // do something here for pressed_button being  1
       break;
     case 2:
       // do something here for pressed_button being  2
       break;
     default:
       // do something here for all other possible values of pressed_button
       break;    
  }
 {   //what is this and its partner after the delay() doing here ?
    myGLCD.setColor(VGA_WHITE);
    myButtons.drawButtons();
    while (the button is not pressed) //do you know how to test whether the button is pressed ?
    {
      //do nothing
    }
    //   delay(10000);
  }

//what is this and its partner after the delay() doing here ?

What's that got to do with it ?

As I already said .. once I can halt the loop until the 'START' button is pressed, I can then sort out what happens when it is pressed.

do you know how to test whether the button is pressed ? .. .. ..clearly not, as this is what I was asking in the first place.

However, I'm out of here ... I honestly regret asking a question in the forum.

All I've had so far is the repetitive pointing out of the shortcomings of the code I'm not yet concerned with and not one attempt to answer my question.

Please forget you ever heard of me

Writing code can be frustrating at best. I don’t see where anyone was being unreasonable. Your code, as it stands, would not even compile. You cannot test it if it doesn’t compile.

To create a successful program, and get help doing it, you must clearly communicate what you want to accomplish and present your attempt at it. Arriving at program correctness is an iterative process and there is no room, or reason, to be sensitive.

I have managed millions of lines of software for man-rated systems (i.e., you screw up and someone might die). I would rather be offended than kill someone. I hope that helps your perspective.

What's that got to do with it ?

For all I know you might think that it was significant to your problem, hence the question

clearly not, as this is what I was asking in the first place.

Not clear at all, at least not to me. My interpretation of your query was that you knew how to read the button but not how to wait until it happened

The URTouch library appears to come with examples. Did you look at them ?
The ButtonTest sketch appears to show you how to detect a button press

I'm past talking about the code … however, I do want to answer this .. .. .

" Your code, as it stands, would not even compile. You cannot test it if it doesn't compile. .. .. .. "

In my first post I wrote .. .. " I know this snippet is wrong, I've written it like that so that you can see what I'm trying to do .. .. .. .. "

Then each post and you're the latest, told me, the code was wrong and wouldn't compile .. .. .

And you don't understand my frustration .. .. .. .

I must be on another planet .. .. .. .

S

Example of how to read a button. There are many other tutorials as well.

I would suggest building the program in increments. First, do nothing but read the button, followed by adding more functionality getting each piece to work one at a time. I just finished a motor controller application with the Arduino which monitors RPM, provides a PWM signal to the motor, reverses polarity of the motor, and monitors for thermal overload. My first version of the program read the on/off switch...4 lines of code. Next, I output PWM based on the switch state, , next RPM monitor, and so on.

Trying to debug a large chuck of code at one time is difficult. Remove or comment out all but the basics, then add functionality as you get pieces to work.

Example of how to read a button. There are many other tutorials as well.

Missed point, I am afraid, as the OP is referring to a button on a touch screen.

“Missed point, I am afraid, as the OP is referring to a button on a touch screen”

My bad...

Musicmanager:
In my first post I wrote .. .. " I know this snippet is wrong, I've written it like that so that you can see what I'm trying to do .. .. .. .. "

What we understood is that you knew there was a bug, not that you had written in pseudo code..

I dug in an old test I had written, it does not use the button library and it basically creates 4 rectangle on the screen and tracks clicks. if you touch within one of the rectangle, then the rectangle is framed in white until you exit the rectangle or release the touch.

there is no event generated or whatever smartness in any of this, that was just to test drawing and touching.

may be this helps get you started? (you can remove lines related to BigFont, I don't display text)

#include <UTFT.h>
#include <SPI.h>
#include <URTouch.h>

extern uint8_t BigFont[];
#define TOUCH_ORIENTATION  LANDSCAPE

UTFT myGLCD(ITDB32S, 38, 39, 40, 41);
URTouch myTouch(6, 5, 4, 3, 2);

int screenW, screenH;

const uint8_t nbButtons = 4;
const int buttonSpacing = 30;

struct rectangleButton {
  int x1, y1, x2, y2;
  uint16_t bgcolor;
  uint16_t fcolor;
  uint8_t id;
} buttons[nbButtons];

void fillButton(const rectangleButton* r)
{
  myGLCD.setColor(r->bgcolor);
  myGLCD.fillRoundRect(r->x1, r->y1, r->x2, r->y2);
}

void frameButton(const rectangleButton* r)
{
  myGLCD.setColor(r->fcolor);
  for (int8_t i = 3; i >= 0; i--) // 4 pixel wide frame
    myGLCD.drawRoundRect(r->x1 + i, r->y1 + i, r->x2 - i, r->y2 - i);
}

void drawAllButtons()
{
  for (uint8_t i = 0; i < nbButtons; i++) fillButton(&(buttons[i]));
}

inline bool testHitButton(const rectangleButton& r, const int x, const int y)
{
  return ((r.x1 < x) && (x < r.x2) && (r.y1 < y) && (y < r.y2));
}

rectangleButton* testHit()
{
  rectangleButton* r = NULL;

  int x , y;
  if (myTouch.dataAvailable()) {
    myTouch.read();
    x = myTouch.getX();
    y = myTouch.getY();
    Serial.print(x); Serial.write(' '); Serial.println(y);
    if ((x >= 0) && (y >= 0)) {
      for (uint8_t i = 0; i < nbButtons; i++) {
        if (testHitButton(buttons[i], x, y)) {
          r = &(buttons[i]);
          break;
        }
      }
    }
  }
  return r;
}

void trackTouch()
{
  static rectangleButton* oldR = NULL;
  rectangleButton* newR = testHit();
  if (newR) { // if we touched within a rectangle 
    if (newR != oldR) { // and this is not the currently highlighted one
      if (oldR) fillButton(oldR); // if there was a previous rectangle highlighted, remove the frame
      frameButton(newR); // and frame the new one
      oldR = newR; // remember this one is framed now
    }
  } else { // we released the touch
    if (oldR) fillButton(oldR); // if there was a previous rectangle highlighted, remove the frame
    oldR = NULL; // and remember no rectangle is hit at the moment
  }
}

void setup() {
  Serial.begin(115200);

  myGLCD.InitLCD(TOUCH_ORIENTATION);  // Setup the LCD
  myGLCD.setFont(BigFont);

  myTouch.InitTouch(TOUCH_ORIENTATION);
  myTouch.setPrecision(PREC_MEDIUM);

  screenW = myGLCD.getDisplayXSize();
  screenH = myGLCD.getDisplayYSize();
  myGLCD.clrScr();
  int buttonWidth  = (screenW - 3 * buttonSpacing) / 2;
  int buttonHeight = (screenH - 3 * buttonSpacing) / 2;

  buttons[0].x1 = buttonSpacing;
  buttons[0].y1 = buttonSpacing;
  buttons[0].x2 = buttons[0].x1 + buttonWidth;
  buttons[0].y2 = buttons[0].y1 + buttonHeight;
  buttons[0].bgcolor = VGA_RED;
  buttons[0].fcolor = VGA_WHITE;
  buttons[0].id = 0;

  buttons[1].x1 = buttonSpacing;
  buttons[1].y1 = 2 * buttonSpacing + buttonHeight;
  buttons[1].x2 = buttons[1].x1 + buttonWidth;
  buttons[1].y2 = buttons[1].y1 + buttonHeight;
  buttons[1].bgcolor = VGA_BLUE;
  buttons[1].fcolor = VGA_WHITE;
  buttons[1].id = 1;

  buttons[2].x1 = 2 * buttonSpacing + buttonWidth;
  buttons[2].y1 = buttonSpacing;
  buttons[2].x2 = buttons[2].x1 + buttonWidth;
  buttons[2].y2 = buttons[2].y1 + buttonHeight;
  buttons[2].bgcolor = VGA_FUCHSIA;
  buttons[2].fcolor = VGA_WHITE;
  buttons[2].id = 2;

  buttons[3].x1 = 2 * buttonSpacing + buttonWidth;
  buttons[3].y1 = 2 * buttonSpacing + buttonHeight;
  buttons[3].x2 = buttons[3].x1 + buttonWidth;
  buttons[3].y2 = buttons[3].y1 + buttonHeight;
  buttons[3].bgcolor = VGA_GREEN;
  buttons[3].fcolor = VGA_WHITE;
  buttons[3].id = 3;

  drawAllButtons();
  Serial.println(F("GO!"));
}


void loop()
{
  trackTouch();
}