Showing posts with label Binary. Show all posts
Showing posts with label Binary. Show all posts

Monday, January 21, 2013

Arduino Micro - Counting In Binary With LEDs

Step one for making a binary clock accomplished. Now I just need some more LEDs to finish the project. I had a lot of fun with this 2 hour project so I'll be nice and share. I present the 4 LED array schematic.


Arduino Micro - The Setup


Arduino Micro 4 LED Array Schematic


And here's the code to run it. Im running off pins 4-7 in the code, so this should port over to other Arduino models with no problem; ignore the 3bit and 4bit bit methods, just me prepping for the final project.


LED Array Binary Counting Code


[sourcecode language="java"]
int timeDigits[4][4] = {
{4,5,6,7},
{4,5,6,7}, // Fill these in later
{4,5,6,7},// with the proper pins
{4,5,6,7}
};

void setup() {
Serial.begin(9600);
pinMode(timeDigits[1][1], OUTPUT);
pinMode(timeDigits[1][2], OUTPUT);
pinMode(timeDigits[1][3], OUTPUT);
pinMode(timeDigits[1][4], OUTPUT);
}

void set4BitBinaryDigit(int timeDigits[], int binaryRow[], int index) {
Serial.print("Binary ");
Serial.print(index);
Serial.print(": ");
Serial.print(binaryRow[0]);
Serial.print(binaryRow[1]);
Serial.print(binaryRow[2]);
Serial.println(binaryRow[3]);
Serial.println(" ");

digitalWrite(timeDigits[0], binaryRow[0]);
digitalWrite(timeDigits[1], binaryRow[1]);
digitalWrite(timeDigits[2], binaryRow[2]);
digitalWrite(timeDigits[3], binaryRow[3]);
}

void set3BitBinaryDigit(int timeDigits[], int binaryRow[]) {
digitalWrite(timeDigits[1], binaryRow[1]);
digitalWrite(timeDigits[2], binaryRow[2]);
digitalWrite(timeDigits[3], binaryRow[3]);
}

void set2BitBinaryDigit(int timeDigits[], int binaryRow[]) {
digitalWrite(timeDigits[2], binaryRow[2]);
digitalWrite(timeDigits[3], binaryRow[3]);
}

void loop() {
int binaryMatrix[10][4] = {
{0,0,0,0}, // 0
{0,0,0,1}, // 1
{0,0,1,0}, // 2
{0,0,1,1}, // 3
{0,1,0,0}, // 4
{0,1,0,1}, // 5
{0,1,1,0}, // 6
{0,1,1,1}, // 7
{1,0,0,0}, // 8
{1,0,0,1},// 9
};



for (int i = 0; i < 1000; ++i)
{
for (int i = 0; i <= 9; ++i) {
set4BitBinaryDigit(timeDigits[1], binaryMatrix[i], i);
delay(800);
}
}
}
[/sourcecode]


And a little video footage showing it in action.

Binary Counting With LEDs - Video Footage


http://youtu.be/hRk3L37uTxs

Monday, December 3, 2012

PHP Code To Binary And Back, Eval'd

Have no clue why the fuck you'd want to do this except for some rudimentary obfuscation, but I sure as hell had fun playing around with it.
[sourcecode language="php"]
<?php
function binary_chunk($str)
{
$func = str_split($str);
$binary_chunk = '';
$l = count($func);
$x = 0;
foreach ($func as $k => $chr) {
$binary_chunk .= decbin(ord($chr));
($x == $l - 1) ?: $binary_chunk .= ' ';
$x++;
}

return $binary_chunk;
}

function decode_binary_chunk($bin)
{
$decoded_binary = '';
$binary_chunk = explode(' ',$bin);
foreach ($binary_chunk as $chunk) {
$decoded_binary .= chr(bindec($chunk));
}
return $decoded_binary;
}

$binary_chunk = binary_chunk('function add($a,$b){return $a+$b;}');
echo "\nFunction chunked to binary:\n";
echo $binary_chunk;
echo "\n\n";

$decoded_binary = decode_binary_chunk($binary_chunk);
echo "Binary chunks glued together:\n";
echo $decoded_binary;
echo "\n\n";

eval($decoded_binary);
echo "Result of decoded function add(1,1):\n";
$x = add(1,1);
echo "$x\n\n";
[/sourcecode]


Sample Output


binary-obfuscation