Go Down

Topic: Switch Case Using Touchscreen (Read 713 times) previous topic - next topic

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:

Code: [Select]

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

elac

#1
Jul 18, 2013, 03:43 pm Last Edit: Jul 18, 2013, 03:45 pm by elac Reason: 1
Maybe like this:
Code: [Select]
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;
       }
          }
         
       }
     
It's all about the skills

UKHeliBob

Code: [Select]
        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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

GSTEC

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

Code: [Select]

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
             

Henry_Best

#4
Jul 18, 2013, 05:53 pm Last Edit: Jul 18, 2013, 08:28 pm by Henry_Best Reason: 1

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:
Code: [Select]

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

PaulS

Code: [Select]
      if (frq = 0) {
Get yourself a clue by four and write on it "= is not the same as ==", and whack yourself a few times.

PaulS

@Henry_Best
Code: [Select]
    count = count++;
Perhaps YOU need to spend some time with a C++ (Not a C = C = C + 1) book...

Arrch

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?

Henry_Best

#8
Jul 18, 2013, 06:05 pm Last Edit: Jul 18, 2013, 08:31 pm by Henry_Best Reason: 1
Woops. Copied from OP's code without checking.

Code now edited.

lloyddean

Simplified, on one level at least!

Code: [Select]

#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.

HazardsMind

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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

lloyddean

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.

Go Up