fit-statUSB: Random Color Generator

programmable multi-color LED that plugs into a USB port
Post Reply
hassellbear
Posts: 106
Joined: Mon May 28, 2012 12:25 pm

fit-statUSB: Random Color Generator

Post by hassellbear »

Overview

fit-statUSB isn't all work and no play. It can be used to implement things which are colorful and entertaining. In this case it is configured to be a random color generator.


Random Color Generator In Action

Unfortunately, the camera image is a bit washed out. In reality, the effect is very good
ezgif.com-resize2.gif
ezgif.com-resize2.gif (1.76 MiB) Viewed 3209 times
ezgif.com-resize.gif
ezgif.com-resize.gif (1.21 MiB) Viewed 3209 times

Perl App

#!/usr/bin/perl -w
#
#fitrandom.pl

# Darryl Hassell 8/8/2018

use Tk;
use Device::SerialPort;
use Time::HiRes ('sleep');

# Define & Initialize Variables

my $red_random_dec = 255;
my $green_random_dec = 255;
my $blue_random_dec = 255;

my $red_random_hex = 'FF';
my $green_random_hex = 'FF';
my $blue_random_hex = 'FF';

my $random_color = '#FFFFFF';

# Configure Serial Port
my $port = Device::SerialPort -> new("/dev/ttyACM0");
$port -> baudrate(115200);
$port -> databits(8);
$port -> parity("none");
$port -> stopbits(1);
$port -> write_settings;

# Set Sleep Delay After Writes in Seconds
our $delay = 0.1;

# Initialize fit-statUSB to Max Brightness White
$port -> write("F0.1\n");
$port -> purge_rx;
$port -> write("$random_color\n");
$port -> purge_rx;

# Create Main Window
$WinMain = MainWindow->new;
$WinMain -> resizable (0,0);

# Label Main Window
$myframe = $WinMain -> Frame ->pack();
$Mem_Use_Label = $myframe -> Label (-text => 'fit-statUSB Random Color Demo', -background => "$random_color", -foreground => "black", -font => "helvetica 9 bold")->pack(-ipadx => 20,-ipady => 20);


# Exit Program Button
$stopbutton = $myframe -> Button (-text => 'Exit', -command => \&Exit, -foreground => 'black', -background => 'lightblue');
$stopbutton -> pack (-side => 'bottom');


$WinMain -> repeat(500, \&GetRandom);

MainLoop();

################################ Exit Program ################################

sub Exit {

$port -> write("#000000\n");
$port -> purge_rx;
sleep($delay);
exit;
}

################################ Generate Random Colors ################################

sub GetRandom {

########## Red ##########
my $red_random_dec1 = int(rand(15)) + 1;
my $red_random_hex1 = sprintf("%X", $red_random_dec1);

my $red_random_dec2 = int(rand(15)) + 1;
my $red_random_hex2 = sprintf("%X", $red_random_dec2);


########## Green ##########
my $green_random_dec1 = int(rand(15)) + 1;
my $green_random_hex1 = sprintf("%X", $green_random_dec1);

my $green_random_dec2 = int(rand(15)) + 1;
my $green_random_hex2 = sprintf("%X", $green_random_dec2);

########## Blue ##########
my $blue_random_dec1 = int(rand(15)) + 1;
my $blue_random_hex1 = sprintf("%X", $blue_random_dec1);

my $blue_random_dec2 = int(rand(15)) + 1;
my $blue_random_hex2 = sprintf("%X", $blue_random_dec2);

my $random_color = '#'.$red_random_hex1.$red_random_hex2.$green_random_hex1.$green_random_hex2.$blue_random_hex1.$blue_random_hex2;


# Send Color and Brightness to fit-statUSB
$port -> write("$random_color\n");
$port -> purge_rx;


$Mem_Use_Label -> configure (-text => 'fit-statUSB Random Color Demo', -background => "$random_color");

}




Conclusions

fit-statUSB can be used for fun and entertaining creations.

Post Reply

Return to “fit-statUSB”