Pages: [1]   Go Down
Author Topic: Switch Case Using Touchscreen  (Read 576 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Connecticut, USA
Offline Offline
Full Member
***
Karma: 8
Posts: 180
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe like this:
Code:
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;
        }
           }
          
        }
      
« Last Edit: July 18, 2013, 08:45:54 am by elac » Logged

It's all about the skills

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4011
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
        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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
             
Logged

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
« Last Edit: July 18, 2013, 01:28:45 pm by Henry_Best » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3347
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Woops. Copied from OP's code without checking.

Code now edited.
« Last Edit: July 18, 2013, 01:31:24 pm by Henry_Best » Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simplified, on one level at least!

Code:
#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.
Logged

Queens, New York
Online Online
Faraday Member
**
Karma: 83
Posts: 3389
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: