PDA

View Full Version : If endif loop with 'OR'



camerart
- 30th December 2015, 11:25
Hi,
Can someone let me know how to do the following please? I'm using PIC BASIC and programming an 18F2431 PIC.

I am trying to move an arm around a 360 circle. For example if it is at 1degree and it needs to move to 2 degrees, then it should move cwise 1 degree. I want it always to take the shortest route. Example if the arm is at 1 degree and needs to move to 359 then how do I make it go ccwise? Because the motor moves if depending on whether ax is > or < than bx, but in this case 359 is > than 0 so it will go right round. Here is an example something like how I think it will work, but there isn't an OR in PICBASIC for this purpose.

If ax < bx Or
If bx - ax >= 180 Then
Gosub cwise
Endif
Endif

Help will be appreciated,

Camerart.

richard
- 30th December 2015, 11:39
If ( ax < bx) Or (bx - ax >= 180) Then

Gosub cwise
Endif


this will work if your math is correct

camerart
- 30th December 2015, 12:11
Hi Richard,

Thanks for your reply.

I forgot to say that I am using Oshonsoft.

See attached result image.

C

richard
- 30th December 2015, 20:44
this compiles just fine from mcs




poscnt var word
azimval var word
time var byte

main:
if (poscnt<azimval) or (azimval-poscnt >= 1800) then
gosub cwise
time=0
endif
goto main
end
cwise:
return


take up the issue with Oshonsoft.

camerart
- 31st December 2015, 11:18
Hi Richard,

There are a few glitches in Oshonsoft, and queries are usually not answered, so I just work with what's there. I tried a few combinations of OR IF THEN ELSE ETC, but no compile.

If I say the answer to my question is:
If poscnt < azimval Then
If azimval - poscnt >= 180 Then
Gosub ccwise
counter = 0
Else
Gosub cwise
counter = 0
Endif
Endif
You may notice that I had asked the wrong question:rolleyes: I have more difficulty than I should have with lines like: (If azimval - poscnt >= 180 Then) so things take 50 times as long as they should. Anyway It's working now.

Thanks and HAPPY NEW YEAR.

C.

Art
- 31st December 2015, 14:04
There’s nothing wrong with what you’re doing with the IF/THEN statements
so long as you follow it, the compiled code is similar, and possiblly even worse.



if (battery=charged) && (lightbulb != blown) then ‘ AND
bulb will light
endif

if (battery = flat) || (lightbulb = blown) then ‘ OR
bulb will not light
endif

camerart
- 31st December 2015, 14:22
Hi Art,

As I've been monolingual with basic since the early 80s, your reply is another puzzle, but thanks. I'll stick to IF/THEN.

C.

Acetronics2
- 1st January 2016, 23:29
Hi,



if (poscnt<azimval) or (azimval-poscnt >= 1800) then


I just would had written ...



if (poscnt<azimval) or ((azimval-poscnt) >= 1800) then


to be sure of what happens ... ( might be overkill ... but ... )

Alain

AvionicsMaster1
- 1st January 2016, 23:39
That's a good point. What does happen first, the comparison or the subtraction, in the first snippet. In the second you'd be sure the subtraction was done prior to the comparison. I'm sure I'll get schooled here but with code there is often no such thing as overkilled overkill.

camerart
- 2nd January 2016, 08:12
Hi,

In my case the subtraction happens first.

C.

AvionicsMaster1
- 2nd January 2016, 18:37
At least for C languages this, according to wikipedia, is the order of operations. I tried to find something more specific to PBP or the BASIC stamp but couldn't. This topic may be getting off topic but I thought it interesting to know at least a pseudo answer.


1 () [] -> . :: Function call, scope, array/member access
2 ! ~ - + * & sizeof type cast ++ -- (most) unary operators, sizeof and type casts (right to left)
3 * / % MOD Multiplication, division, modulo
4 + - Addition and subtraction
5 << >> Bitwise shift left and right
6 < <= > >= Comparisons: less-than, ...
7 == != Comparisons: equal and not equal
8 & Bitwise AND
9 ^ Bitwise exclusive OR (XOR)
10 | Bitwise inclusive (normal) OR
11 && Logical AND
12 || Logical OR
13 ? : Conditional expression (ternary)
14 = += -= *= /= %= &= |= ^= <<= >>= Assignment operators (right to left)
15 , Comma operator

richard
- 2nd January 2016, 22:02
Precedence from the pbp3 manual


The following table lists the operators in default hierarchal order. (Parentheses will
override.) Operators with the same precedence level (on the same line in the table
below) will be evaluated in the order (left to right) that they are encountered in the
written expression.
Highest Precedence
( ) (anything enclosed in parentheses)
- (unary)
! or NOT, ABS, COS, DCD, DIV32, NCD, SIN, SQR
<<, >>, ATN, DIG, HYP, MAX, MIN, REV
*, /, **, */, //
+, - (in math), ~
= or ==, <> or !=, <, <=, >, >=
&, |, ^, &/, |/, ^/
AND, OR, XOR, ANDNOT, ORNOT, XORNOT
Lowest Precedence



camerart's problem is that he is using Oshonsoft's version of picbasic not pbppro

picbasic != pbppro

Art
- 15th January 2016, 14:58
Ah, what I posted should compile in C or PBP, and in both languages inner brackets should evaluate first, and in order toward outer brackets.

PBP


if (battery=charged) && (lightbulb != blown) then ‘ AND
bulb will light
endif

C


if (battery=charged) && (lightbulb != blown) { ‘ AND
bulb will light
}







Hi Art,

As I've been monolingual with basic since the early 80s, your reply is another puzzle, but thanks. I'll stick to IF/THEN.

C.

richard
- 16th January 2016, 01:04
Ah, what I posted should compile in C or PBP, and in both languages inner brackets should evaluate first, and in order toward outer brackets.

C

if (battery=charged) && (lightbulb != blown) { ‘ AND bulb will light }


not really in C (battery=charged) is not a comparison it's an assignment ,while the statement will compile in C the results will be incorrect

(battery==charged) is a comparison statement in C

Art
- 16th January 2016, 08:49
Whoops :D.