2014년 12월 14일 일요일

Decimal to Binary conversion

Decimal to Binary conversion using App Inventor 2.
Devised by Brian Mudd on 12/12/2014

Designer Screen for this setup was as follows:(Sorry about blank screen 1, I started with two screens but now can't delete screen 1)

8 Labels arranged horizontally
A button to cycle values from 0 to 255
A Reset button
A text entry box and Set button

Blocks Screen

Initialize numeric variables (to 0): Bit0, Bit1, Bit2, Bit3, Bit4, Bit5, Bit6, Bit7, Input, Calc
The variable 'Input' can come from Blue tooth, Ethernet or manual entry as below.

'If Reset is required, when Reset.click, set Input to 0, call DectoBin
'If text entry is required, when Entry.click, set Input to Textbox1.text, call DectoBin, set Textbox.text to ""
'if manual cycling is required, when Cycle.click, set Input to get Input + 1, call DectoBin

Procedure DectoBin
IF get Input => 256, then set Input to 0, set Label.text to Invalid Entry
'The above number is for converting an unsigned byte, to convert an unsigned integer, use 65536 and add extra if blocks to cover bits 8 to 15'
'if feedback is required, set a label.text to get Input'
SET Calc to get Input
SET all Bit variables to 0

if get Calc > 127
set Calc to get Calc - 128
set Bit7 to 1

if get Calc < 128 and get Calc > 63
set Calc to get Calc - 64
set Bit6 to 1

if get Calc < 64 and get Calc > 31
set Calc to get Calc - 32
set Bit5 to 1

if get Calc < 32 and get Calc > 15
set Calc to get Calc - 16
set Bit4 to 1

if get Calc < 16 and get Calc > 7
set Calc to get Calc - 8
set Bit3 to 1

if get Calc < 8 and get Calc > 3
set Calc to get Calc - 4
set Bit2 to 1

if get Calc > 4 and get Calc > 1
set Calc to get Calc - 2
set Bit1 to 1

if get Calc < 2 and get Calc > 0
set Calc to get Calc - 1
set Bit0 to 1

call Display

Procedure Display
if get Bit0 = 1
set Label0.backgroung to yellow
else set Label0.background to grey

if get Bit1 = 1
set Label1.backgroung to yellow
else set Label1.background to grey

if get Bit2 = 1
set Label2.backgroung to yellow
else set Label2.background to grey

if get Bit3 = 1
set Label3.backgroung to yellow
else set Label3.background to grey

if get Bit4 = 1
set Label4.backgroung to yellow
else set Label4.background to grey

if get Bit5 = 1
set Label5.backgroung to yellow
else set Label5.background to grey

if get Bit6 = 1
set Label6.backgroung to yellow
else set Label6.background to grey

if get Bit7 = 1
set Label7.backgroung to yellow
else set Label7.background to grey




Thanks for posting your code Brian.  Some of the users of the robot and bluetooth PCs and others should appreciate your effort.



This example would be a good stepping stone to learn lists
and advanced components for the labels.


댓글 없음:

댓글 쓰기