Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
The pins all collect either a 1 for on or 0 for off. How would I make it so that it makes a number with the binary inputs. Say I have switch 24, 23, and 22 on... and 18 on. So effectively that would be 00010111 ie binary for 23 in decimal. Then set it as a variable. I'm not sure how to convert strings or pins into a number.
Do I just do it manually. Ie pin 24 adds 1, pin 23 adds 2, pin 22 adds 4, pin 21 adds 8 etc...? Or is there a more simple way?
Cheers.
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 10406
Posted: 09:05pm 16 Sep 2015
Copy link to clipboard
Print this post
Do I just do it manually. Ie pin 24 adds 1, pin 23 adds 2, pin 22 adds 4, pin 21 adds 8 etc...? Or is there a more simple way?
I did read that part in the manual but didn't quite understand it.
[code]
DIM INTEGER Init_Number = port(24,-4,18,-4)
[/code]
Bunching commands together doesn't always seem to work. Or doesn't it take negitive numbers?
That will work, but I thought maybe there was a way to do it on one line of code. :)Edited by B15HOP 2015-09-18
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9643
Posted: 09:42pm 16 Sep 2015
Copy link to clipboard
Print this post
I just use N=PORT(1,4) or N=PORT(1,4,61,4)(for the 64-pin MM+)
The first example will read pin1,pin2,pin3 and pin4, and save the binary value of the combined pins in variable N, and the 2nd version will read an 8-bit binary number on pins 1-4 and pins 61-64 on the MM+, and save the value in N.
You make reference to -4, which is illegal I think?
Variable 'N' will contain the value of all 8-bits of your two switch banks. Edited by Grogster 2015-09-18Smoke makes things work. When the smoke gets out, it stops!
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 10406
Yeah I just thought it could read the ports backwards but I guess not. Guess it doesn't matter if I flip the breadboard around to read them upside down. :)
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9643
Posted: 09:53pm 16 Sep 2015
Copy link to clipboard
Print this post
Ahhhhh - I see what you mean by the -4 now. No, pins must be numbered consecutively to use the PORT function. I will watch this thread, and see if anyone comes up with a brilliant solution - someone usually does. Smoke makes things work. When the smoke gets out, it stops!
Ahhhhh - I see what you mean by the -4 now. No, pins must be numbered consecutively to use the PORT function. I will watch this thread, and see if anyone comes up with a brilliant solution - someone usually does.
Both techniques definitely work. The function just can't take negitive values so Mr Wizzard's option is the solution for now. But I really do like having just one line of code. :)
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 10406
Posted: 10:22pm 16 Sep 2015
Copy link to clipboard
Print this post
You can go:
DIM INTEGER Init_Number = port(24,1,23,1,22,1,18,1,17,1,16,1,15,1)
or any other order to get what you want
WhiteWizzard Guru Joined: 05/04/2013 Location: United KingdomPosts: 2948
Posted: 10:25pm 16 Sep 2015
Copy link to clipboard
Print this post
. . so Mr Wizzard's option is the solution for now. But I really do like having just one line of code. :)