multiple while() and nestedIFs alternative.

General discussion on mikroElektronika website & forums.
Post Reply
Author
Message
sysavm
Posts: 13
Joined: 09 Jun 2014 13:19

multiple while() and nestedIFs alternative.

#1 Post by sysavm » 15 Jun 2014 21:27

Any idea on what is best alternative for the below conditions?

< 100 = 1ms delay
>= 100 and < 200 = 2..
>= 200 and < 300 = 3..
>= 300 and < 400 = 4..
>= 400 and < 500 = 5..
.....

>=1xxx < 19xx = x

Can look-up table be used?

sysavm
Posts: 13
Joined: 09 Jun 2014 13:19

Re: multiple while() and nestedIFs alternative.

#2 Post by sysavm » 16 Jun 2014 18:35

This is a sample code I'm trying to solve.

if (ptime ==1000) { pdelay = 245;}
if (ptime >1000 and ptime <=1025) { pdelay = 240;}
if (ptime >1025 and ptime <=1050) { pdelay = 235;}
if (ptime >1050 and ptime <=1065) { pdelay = 230;}
if (ptime >1065 and ptime <=1085) { pdelay = 225;}
if (ptime >1085 and ptime <=1100) { pdelay = 220;}
if (ptime >1100 and ptime <=1125) { pdelay = 215;}
if (ptime >1125 and ptime <=1175) { pdelay = 210;}
if (ptime >1175 and ptime <=1200) { pdelay = 205;}
if (ptime >1200 and ptime <=1225) { pdelay = 200;}
if (ptime >1225 and ptime <=1250) { pdelay = 195;}
if (ptime >1250 and ptime <=1275) { pdelay = 190;}
if (ptime >1275 and ptime <=1325) { pdelay = 185;}
if (ptime >1325 and ptime <=1350) { pdelay = 180;}
if (ptime >1350 and ptime <=1400) { pdelay = 175;}
if (ptime >1400 and ptime <=1425) { pdelay = 170;}
if (ptime >1425 and ptime <=1475) { pdelay = 165;}
if (ptime >1475 and ptime <=1525) { pdelay = 160;}
if (ptime >1525 and ptime <=1575) { pdelay = 155;}
if (ptime >1575 and ptime <=1625) { pdelay = 150;}
if (ptime >1625 and ptime <=1675) { pdelay = 145;}
if (ptime >1675 and ptime <=1725) { pdelay = 140;}
if (ptime >1725 and ptime <=1775) { pdelay = 135;}
if (ptime >1775 and ptime <=1850) { pdelay = 130;}
if (ptime >1850 and ptime <=1925) { pdelay = 125;}
if (ptime >1925 and ptime <=2000) { pdelay = 120;}
if (ptime >2000 and ptime <=2075) { pdelay = 115;}
if (ptime >2075 and ptime <=2175) { pdelay = 110;}
if (ptime >2175 and ptime <=2275) { pdelay = 105;}
if (ptime >2275 and ptime <=2375) { pdelay = 100;}
if (ptime >2375 and ptime <=2500) { pdelay = 95;}
if (ptime >2500 and ptime <=2625) { pdelay = 90;}
if (ptime >2625 and ptime <=2775) { pdelay = 85;}
if (ptime >2775 and ptime <=2950) { pdelay = 80;}
if (ptime >2950 and ptime <=3125) { pdelay = 75;}
if (ptime >3125 and ptime <=3325) { pdelay = 70;}
if (ptime >3325 and ptime <=3575) { pdelay = 65;}
if (ptime >3575 and ptime <=3850) { pdelay = 60;}
if (ptime >3850 and ptime <=4175) { pdelay = 55;}
if (ptime >4175 and ptime <=4550) { pdelay = 50;}
if (ptime >4550 and ptime <=5000) { pdelay = 45;}
if (ptime >5000 and ptime <=5550) { pdelay = 40;}
if (ptime >5550 and ptime <=6250) { pdelay = 35;}
if (ptime >6250 and ptime <=7150) { pdelay = 30;}
if (ptime >7150 and ptime <=8325) { pdelay = 25;}
if (ptime >8325 and ptime <=10000) { pdelay = 20;}
if (ptime >10000 and ptime <=12500) { pdelay = 15;}
if (ptime >12500) { pdelay = 10;}

Where in ptime is the capture difference between 2 pulses. Any suggestion?

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: multiple while() and nestedIFs alternative.

#3 Post by filip » 17 Jun 2014 08:52

Hi,

You can use switch-case structure as an alternative.
Please see the help regarding this.

Regards,
Filip.

sysavm
Posts: 13
Joined: 09 Jun 2014 13:19

Re: multiple while() and nestedIFs alternative.

#4 Post by sysavm » 20 Jul 2014 17:21

thanks Filip. but I would like to make the code smaller/shorter as much as possible. I'm building a programmable ignition module for CDI in mikroc.

I've tested the code in C and it works. But seems that it will have the same number of conditions as the IF-Else statement.

Code: Select all

#include <stdio.h>

int main()
{
int input=12;
switch (input){
        case 1 ... 5: printf("case 2\n"); break;
        case 10 ... 15: printf("case 1\n"); break;
        default: printf("not there\n"); break;
        }
}
Just tested. In mikroc, case ranges is not supported. Compiler error.

Any suggestion instead of;?

Code: Select all

case 1:
case 2:
.
.
.
case 65536:

Post Reply

Return to “Website & Forums General Discussion”