Author Topic: RANDOM, It's never what you expect  (Read 2922 times)

0 Members and 1 Guest are viewing this topic.

OperaHouse

  • Hero Member
  • *****
  • Posts: 1308
  • Country: us
RANDOM, It's never what you expect
« on: December 24, 2016, 12:45:10 PM »
And you thought my other programs were useless.

                             RANDOM_EFFECTS

Probably the most irritating program I've ever written.  That doesn't mean the kids won't
love it. I can't see any practical use in solar. RANDOM is like flipping a coin. It is
predictable over a long term. Any single number from 1-20 will show up 5% of the time if
a large sample is taken. Any number under 11 will will happen 50% of the time.  These
numbers can be used for PWM or TONE to create sound effects.  The function RANDOM creates
a random number between a designated low value and a higher one. Upper and lower limits can
be selected to match the frequencies desired in TONE. IF, OR, ==, greater or less than
statements create flashing effects like a flame or bad connection.  Lights can operate
normally and then become haunted randomly by setting a flag randomly from a random number.
Every selected number of program loops a random value is compared. If that matches a small
group of numbers, a haunt bit is set. That allows a flashing routine for a period of time.
This program is part of an animation project I did for a grocery store many years ago for
a hot sauce display. We built this high tech experimental plane from old DC-8 parts. With
a 80 foot wingspan hanging from the ceiling it was quite impressive. The story was that it
was a haunted plane and never worked right. 

In this sample program pin# 10 is the landing lights which are normally ON for a long period
of time, but start flashing like they have a bad electrical connection randomly. Pin# 7 is
haunted also except it is normally OFF and will flash ON at random periods. Pins 11 & 12 flash
like a frame are just opposite of each other. Used with red and blue LED strip lights to create
the look of flames in the engine nacelle.  Pins 8 & 9 are also random continuous flashing. Pin#
3 is the sound out used to create engine sounds with some low frequency peaking. These were
fed into a speaker with a good low frequency resonance chamber. Being square waves they have
all the odd  harmonics, they may sound a lot higher in frequency if not used with low end peaking. 
Occasionally you will get some PAC MAN sounds. There were other parts to the program that had
other effects like the tail lights sending out SOS and a radar that moved up and/down and side
to side that aren't included. You could add some motors for some movement pretty easily.  A
little late to run the Christmas tree lights with. If you got the kiddies a UNO, this would be
a fun project for them or the next Halloween display.  Adding a PIR can cause it to start up
when people are detected.

/* RANDOM_EFFECTS
This is an animation program which flashes lights and creates sound
First creates as part of the plane8 program
Pins 11 & 12 are just opposite of each other, flame
Pin 10 HAUNTED LIGHT, normally ON but periodically flash erratically
Pins 8 & 9 are random continuous flashing of no specific time
Pin 7 HAUNTED #2 LIGHT, normally OFF but periodically flash erratically
Pin 3 is the sound out

Created 12/22/2016 from PLANE08
*/

int haunt    = 0;
int haunt2   = 0;
int mente    = 1;
long randval = 0;
int engine   = 0;
int bebop    = 0;
int valx     = 0;

void setup()
 {               
  // initialize the digital pins as outputs.
  // Pin 13 has LED connected on Arduino UNO board. Same as flame
  // and is a visual indicator the micro is working
 
  pinMode(13, OUTPUT);       // RADAR STROBE
  pinMode(12, OUTPUT);       // lights #1 ALT #2 FLAME
  pinMode(11, OUTPUT);       // lights #2 ALT #1
  pinMode(10, OUTPUT);       // lights #3 HAUNTED NORMALLY O
  pinMode(9,  OUTPUT);       // lights #4 FLICKER
  pinMode(8,  OUTPUT);       // lights #5 FLICKER
  pinMode(7,  OUTPUT);       // lights #6 HAUNTED NORMALLY OFF
  pinMode(3,  OUTPUT);       //  TONE SOUND OUT
 
  Serial.begin (9600);       // Set up serial monitor routine for diagnostics
  }

void loop()
 {                           // This is the start of the animation program

 // This section generates a random signal &loop counters
 
 
   bebop = bebop + 1;
   engine = engine + 1;
   
   
   randval = random (5,50);                // Get a random number to decide from 5 to 50

 
   if (randval < 23)                       // Half of the time ON determined by random number
     {
     digitalWrite (13, 1);                 // Turn ON LED 
     digitalWrite (11, 1);                 // Turn ON  #1 LIGHTS 
     digitalWrite (12, 0);                 // Turn OFF #2 LIGHTS 
     }
   else
     {
     digitalWrite (13, 0);                 // Turn OFF LED 
     digitalWrite (11, 0);                 // Turn OFF #1 LIGHTS 
     digitalWrite (12, 1);                 // Turn ON  #2 LIGHTS       
     }
     
   //  Third of the time ON determined by random number
   if (randval < 10 || randval > 40) digitalWrite (9, 1);  // ON #4 LIGHTS     
   else digitalWrite (9, 0);     // Turn OFF #4 LIGHTS
   
   //  Third of the time ON determined by random number
   if (randval > 20 && randval < 30) digitalWrite (8, 1);  // ON #5 LIGHTS     
   else digitalWrite (8, 0);     // Turn OFF #5 LIGHTS
     
       
   // This section creates a time marker and sets the HAUNT FLAGS     
   mente = mente +1;
     
   if (mente == 300) haunt2 = 1;             // start haunt2 period.
     
   if (mente == 200)  haunt = 1;             // alternate haunt time start
   
   if (mente == 600)                         // The end time for each period
     {                                       // See also count in lower section
     mente = 0;                              // Reset time counter
     haunt = 1;                              // Set HAUNT FLAG assuming random number comes up
     
         
     // Set HAUNT FLAG assuming random number comes up
     // This is the random timing generator for HAUNT EFFECT. 
     if (randval < 43) haunt = 0;    // Reset haunt if random number not found.  Random number is from 2 to 50.
     }                               // Aprox 1 in 8 chance each time period
     
     // If HAUNT FLAG is set start flickering landing lights *LIGHTS ON TO FLICKERING*
 
     if (haunt > 0)
       {                                          // Is haunt still in efect?
       haunt = haunt + 1;                  // Increment HAUNT TIMER.  This is the time of flashing
       
       if (randval > 15) digitalWrite (10, 1);    // Turn ON #3  HAUNTED LIGHTS
                                                   // Determine state by random number
       else digitalWrite (10, 0);                 // Turn OFF #3 LIGHTS
       }
       
     if (haunt > 600) haunt = 0;                   // Reset after reaching end of event. This is
                                                              // the length of time for haunt event
     
     if (haunt == 0) digitalWrite (10, 1);         // Turn ON #3 LIGHTS if not in a haunt event. 
                                                                // This is the normal state
     
     
     // If HAUNT2 FLAG is set start flickering landing lights *LIGHTS OFF TO FLICKERING*
     if (haunt2 > 0)
       {                                               // Is haunt2 still in effect?
        haunt2 = haunt2 + 1;                 // Increment HAUNT2 TIMER.  This is the time of flashing
       
        if (randval < 25) digitalWrite (7, 1);     // Turn ON #6  HAUNTED LIGHTS
                                                              // Determine state by random number
        else digitalWrite (7, 0);                      // Turn OFF #6 LIGHTS
       }
      if (haunt2 > 400) haunt2 = 0;                // Reset after reaching end of event. This is the
                                                               //length of time for haunt event
                                                               // haunt2 must be a lower number than haunt
     
      if (haunt2 == 0) digitalWrite (7, 0);        // Turn ON #6 LIGHTS OFF if not in a haunt2 event.
                                                               //  This is the normal state
     
     
      // This section creates an ENGINE SOUND with TONE on PIN 3 created from flamex
      if (engine > 3)                                 // No tone changes quicker than 3 loops
           {
           if (bebop < 70)
                  {
                  tone (3, randval / 2);        // Is it time to change the tone? This number determines
                                                       // the time between tone changes
                  engine = 0;                     
                  }                                    // tone on pin 3 to drive speaker after filtering. Division of
                                                        // number keeps a normal frequency
               
           else 
                  {
                  if (randval > 3 && randval < 45)     // Random value is from 2 to 50
                       {
                       valx =  randval + 30;
                       tone (3,valx);
                       engine = 0;
                       }
                  else 
                       {
                       tone (3,50); 
                       engine = 0;                      // Reset loop counter 
                       }
                  }
              }   
           
    if (bebop > 400) bebop =  0;               // Reset sound cycle
       
    delay(10);                // Delay for program loop
                                                         
    // optional debug screen                           
    Serial.print (mente);                           
    Serial.print ( " mente  ");                               
    Serial.print (haunt);                           
    Serial.print ( " haunt  ");   
    Serial.print (haunt2);                           
    Serial.print ( " haunt2  ");       
    Serial.print (bebop);                           
    Serial.print ( " bebop  ");
    Serial.print (engine);                           
    Serial.print ( " engine  ");
    Serial.print (randval);                           
    Serial.print ( " randval  ");   
    Serial.print (valx);                           
    Serial.println ( " valx  ");   
                               
   }                           // end of program