Switch Case Using Touchscreen

So I am having an issue with my switch case programming…My goal is to tap a button on my screen to select different frequencies for a flashing LED. I am not getting any errors at all in my code, but the LED is staying at the frequency that I initially defined it at. Below is what I have so far:

if ((p.x > 110) && (p.x < 235)) {
	if ((p.y > 5) && (p.y < 95)) {
        count = count++;
        if (count = 4) {
          count = 0;
        switch (count) {
          case 0:
            frq = 1000;
            break;
          case 1:
            frq = 750;
             break;
          case 2:
            frq = 500;
            break;
          case 3:
            frq = 250;
            break;
        }
           }
           
        }
      }

My guess is that my code may not be structured right with where parts are located inside the if statements, but I would totally appreciate some help on the matter!

Thanks!

-GSTEC

Maybe like this:

if ((p.x > 110) && (p.x < 235)) {
	if ((p.y > 5) && (p.y < 95)) {
        count = count++;
        if (count == 4) {
          count = 0;
       } 
        switch (count) {
          case 0:
            frq = 1000;
            break;
          case 1:
            frq = 750;
             break;
          case 2:
            frq = 500;
            break;
          case 3:
            frq = 250;
            break;
        }
           }
           
        }
        if (count = 4) {

I stopped reading at that line. Do you want to set count to 4 or compare it to 4 ?
Put it right and come back for more help if you need it.

I tried changing the = to == and still nothing :~ I did get something to work by using:

int frq = 1000;

...

if ((p.x > 110) && (p.x < 235)) {
  if ((p.y > 5) && (p.y < 95)) {
    frq = frq - 250;       // Decreases the frequency by 250ms every push

      if (frq = 0) {
        frq = 1000;         // Once the button is pushed 4 times to reach a value of 0, reset back to 1000
      }
    }
}

However the problem with this one is that it wouldn’t increment down evenly each time…ie it had to be held down. When this was done, you could see the light changing pulse rates, however when just simply pushed, not held, the flash rate would alternate between 2 speeds. Thanks again for the help fellas!

  • GSTEC

GSTEC:
So I am having an issue with my switch case programming…My goal is to tap a button on my screen to select different frequencies for a flashing LED. I am not getting any errors at all in my code, but the LED is staying at the frequency that I initially defined it at. Below is what I have so far:

Simplified:

if ((p.x > 110) && (p.x < 235) && (p.y > 5) && (p.y < 95)) {
    count++;
    if (count ==4)  count = 0;
    frq = (4-count)*250;
   }

Edit to correct code

      if (frq = 0) {

Get yourself a clue by four and write on it "= is not the same as ==", and whack yourself a few times.

@Henry_Best

    count = count++;

Perhaps YOU need to spend some time with a C++ (Not a C = C = C + 1) book...

I get the feeling the issue isn't going to be resolved until we see the whole code. How often is that block of code run? In other words, will the top conditions be true only once between frequency changes?

Woops. Copied from OP's code without checking.

Code now edited.

Simplified, on one level at least!

#define ARRAY_ENTRIES(ARRAY)    (sizeof(ARRAY) / sizeof(ARRAY[0]))

const int frequencies[] = { 1000, 750, 500, 250 };

if ( (p.x > 110) && (p.x < 235) )
{
    if ( (p.y > 5) && (p.y < 95) )
    {
        ifrq++;
        ifrq %= ARRAY_ENTRIES(frequencies);

        frq = frequencies[ifrq];
    }
}

You should be able to generalize your button hit testing and similarly simplify hit testing through out your code.

I would also make some kind of latch logic so that if the button is held down, it does not cycle through all the frequencies in a split second.

Some useful functionality might include -

• point_t class to handle point (yes something different than that of the touches lib)

• rect_t class specifying a rect with perhaps a 'point_in_rect' method.

• a button class that makes use of 'point_t' and 'rect_t' for hit testing implementing the touch point down was within the buttons 'rect' and the touch point up was also within the button rect to determine a successful button press.

These don't have to be complicated but will make for more modular, readable, maintainable and debuggable code.