Tuesday, February 28, 2012

Project Update: Nixie Clock part 3.1

Hey guys, I have a update for the WIFI Nixie Clock code.  Over the last few weeks I've had a chance to debug a little bit of the code.  Basically what was happening is that at midnight when I had set it up to reset the connection, it would never update the netTime variable and would keep rebooting.  It would do this till a reconnect counter triggered after the fifth attempt and it would send a reboot command and the whole clock would reboot. After that it would work as intended but it was annoying and took almost five minutes.  The fix for this was very simple.

So here's what I changed:

First, in the reconnect function, I changed how many times the system would reconnect to the wifi from five to seven.  This way every week the clock would perform a full reboot.  Its up to you how you want to handle that.  The section of code looks like this:

// reboot if not working right yet.
  iTrack++;
  if ( iTrack > 7 ) {
    WiFly.reboot();
    iTrack = 0;
  }

Next, here's the loop function with the easy fix and something I really should have done the first time.  Just set the netTime variable again after the reconnect function finishes.   Also, because I want to keep it from reconnecting again, I extended the delay to 30 seconds.  30 seconds is a very long time and really I'm being overly cautious.  Adjust it to whatever you like.  Personally, the reconnect function takes 30-40 seconds to connect to my wifi router so another 30 seconds before polling the wifi module keeps it from running the reconnect function again, for me.    Anyway, the loop function should should look like this now:

void loop()
{
  if (netTime==0)  // if the netTime var is equal to 0, which should be midnight, then resync the time
  {
    Serial << "Reconnect netTime = " << netTime << endl;  // debug output
    backlightColor();  // The Colors!!!      
    Reconnect();  // Reconnect to the wifi AP and query the NTP server for the time.  
    delay(30000);  // I run a long delay to keep it from running  from querying the NTP server too offten.  No reason to abuse a free service.
    netTime=((hour()-5)*100)+minute();  //set the netTime after running the Reconnect. 
  }
  else  
  {
    Serial << "netTime = " << netTime << endl;  // debug output
    getNetTime();     // run GMT to local Time Zone conversion
    backlightColor();   // The Colors!!!
    nixNum(netTime);  // send data to the nixie clock.  int array nixNum with int data from gmt converstion.
    delay(100);  // Short delay for the backlight
  }
}

Here's a link to the updated code.  Remember to set your wifi settings and a NTP server.  




Sunday, February 5, 2012

Project Update: Nixie Clock part 3

Hey guys, its been a while.  I haven't had much time or money to play around lately but over christmas I received three items from my tinkering wish list.


Arduino Mega 2560 R3


The latest release of the Arduino Mega. The Mega2560 includes an upgraded Chip set with the ATmega2560. This provides the Mega2560 with 256k flash memory, that's double the original Mega board's memory. Additionally you'll find updates in the USB-serial conversion department with the addition of the ATmega8U2. This replaces the old FTDI chip that was on the original Megas.

This board has similar functionality to the Arduino Uno board, but with more programming space and more complex functionality. This thing is a beast with 54 digital input/output pins, 14 of those offering PWM. 16 analog inputs, and 4 Serial UARTs. The sky is the limit for what can be done with this board.




2.8" TFT Touch Shield for Arduino - 2.8"

Spice up your Arduino project with a beautiful large touchscreen display shield with built in microSD card connection. This TFT display is big (2.8" diagonal) bright (4 white-LED backlight) and colorful (18-bit 262,000 different shades)! 240x320 pixels with individual pixel control. It has way more resolution than a black and white 128x64 display. As a bonus, this display has a resistive touchscreen attached to it already, so you can detect finger presses anywhere on the screen.




RN-XV WiFly Module


The RN-XV module by Roving Networks is a certified Wi-Fi solution especially designed for customer who want to migrate their existing 802.15.4 architecture to a standard TCP/IP based platform without having to redesign their existing hardware. In other words, if your project is set up for XBee and you want to move it to a standard WiFi network, you can drop this in the same socket without any other new hardware.

The RN-XV module is based upon Roving Networks' robust RN-171 Wi-Fi module and incorporates 802.11 b/g radio, 32 bit processor, TCP/IP stack, real-time clock, crypto accelerator, power management unit and analog sensor interface.The module is pre-loaded with Roving firmware to simplify integration and minimize development time of your application. In the simplest configuration, the hardware only requires four connections (PWR, TX, RX and GND) to create a wireless data connection.





I have big plans for the the Mega and the TFT Touch Shield, but that's for later.  For now I'm working with the RN-XV Wifly Module.  I had it on my list because over the last few months I've had a lot of probelms with the DS1307 RTC I was using in my original clock build.  It's been loosing about a minute every week or so.  I wasn't really sure why this was and it easily could have been my fault when I soldered the pins originally.  Anyway, it was annoying and I have a personal policy that says, if it don't work, go bigger.  So...why not get my Nixie clock to keep itself updated...with the power of the internet!!  =D

Before I start, I also had to get a XBee Adapter Kit from Adafruit.com because my clock project runs at 5v and the wifly module runs at 3.3v.  The Adapter seemed like a easy way of converting the logic without much hassle.  Plus its pinned to easily be used with a 5v FTDI cable which lets you directly communicate with the module and play with its internal software.  And man, this little guy is awesome, there are so many little features to play with.  Although I did have a disconnect issue when I first started to play with it.  But if you can get it to connect long enough, theres an internal command to have it automatically download the latest update image from roving networks which was really cool when I figured that out.  At the time of this blog post, I was using version 2.31.

After I got it hooked up to my Arduino I started to worry about how I was going to get the time data off the RN-XV and into a format I could use with the Nixie clock.  Plus because of the name it was hard to search for topics on how to interface this with my Arduino.  This is because most search engines wont search for terms less then three letters long and RN and XV of course don't.  But I did run into this simple example from http://cairohackerspace.org/ which used a serial library which was developed for other wifly boards.  After playing with the example a little bit and trying to figure out the library, I was really worried about how I was going to get the data I needed.  Basically I needed a four digit integer to work with the Nixie clock and I was concerned I would somehow have to parse char string data from the RN-XV and then convert it to an integer.  Luckily, I learned that the example I was using, was already polling the data I needed in an integer array.  Although the hours and the minutes were in separate arrays but that not hard to work with.  All I had to do is some simple math:

x = (hour() * 100) + minute()

We take the digits being used in the hour array and we multiply it by 100 which move them into the hundredth's place and then we just add the minutes.  Easy math.

Now the example gets the time as GMT which since i'm on the east coast, doesn't work for me and to be honest...I still haven't figured out how to change it to my current time zone.  But again, with some simple math, its not a problem.

x = ((hour() - 5)*100) + minute() 


Basically the same except I subtract 5 from whatever the hour is.  Which works great...except between midnight and 5am GMT where it really screws up the equation by making x a negative number.  We need to keep it a positive number for the math to work right, but once more, real easy math can take care of this.


if (hour()-5 < 0)
  {
    netTime=((hour()+19)*100)+minute();
  }
  else
  {
    netTime=((hour()-5)*100)+minute();
  }

If we take the result of  Hour minus 5 is less then 0 then instead of subtracting by 5, we will add hour by 19.  19 being whats left over after subtracting 24 by 5. This basically takes for example 2am GMT which is 02 and adds 19 which makes it 21 eastern.  If hour minus 5 isn't less then 0, the equation runs as normal.

After all this, it was super simple to finish out the rest of the code for the clock.  I kept the backlight code I took from Hypomnemata and used a simpler Nixie tube code I took from tronixstuff's example.  Also I kept the example from Cairo Hacker Space and just dropped everything in the loop.  I kept the webTime example because it made it easy to debug the code when you viewed the output through the serial monitor.

Finally to make it all fit, I had to move a few of my pins around from the original build.  The RN-XV's TX and RX are connected to pins 2 and 3, the Nixie Tubes are connected to pins 4, 5, and 6, and I kept the back light led's on pins 8-11.  Anyway, here's the code, but before you compile it though, you'll need the following library's:

Nixie Library from Ogi Lumen.
NewSoftSerial, Streaming, and Pstring library's from arduiniana.org.
WiflySerial library from arduinology.

Also update the code for your specific wifi settings and your favorite NTP server as well as change the math to your specific time zone.

Nixie Clock Wifi Edition



/*
 * Web Server example for the WiFly showing the current time and some status items.
 *
 * Download Mikal Hart's NewSoftSerial, Streaming and PString libraries from http://arduiniana.org
 *
 * Remember to set MY_WIFI_SSID and MY_WIFI_PASSPHRASE to your local values.

 Aim your browser at your WiFly address for a simple UTC time report.
 Add /status to the URL to get battery voltage and RSSI.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 Copyright GPL 2.1 Tom Waldock 2011
 */
#include <WProgram.h>
#include <Time.h>
#include <NewSoftSerial.h>
#include <Streaming.h>
#include <PString.h>
#include "WiFlySerial.h"
#include "MemoryFree.h"



// Set these to your local values
#define MY_WIFI_SSID "your SSID"
#define MY_WIFI_PASSPHRASE "your WIFI passphrase"

// Connect the WiFly TX pin to the Arduino RX pin  (Transmit from WiFly-> Receive into Arduino)
// Connect the WiFly RX pin to the Arduino TX pin  (Transmit from Arduino-> Receive into WiFly)
//
// Connect the WiFly GND pin to an Arduino GND pin
// Finally, connect the WiFly BATT pin to the 3.3V pin (NOT the 5v pin)

#define ARDUINO_RX_PIN  2
#define ARDUINO_TX_PIN  3


prog_char s_WT_SETUP_00[] PROGMEM = "your favorite NTP Server";  /* change to your favorite NTP server */
prog_char s_WT_SETUP_01[] PROGMEM = "set u m 0x1";

prog_char s_WT_SETUP_02[] PROGMEM = "set comm remote 0";
prog_char s_WT_SETUP_03[] PROGMEM = "set comm idle 30";
prog_char s_WT_SETUP_04[] PROGMEM = "set comm time 2000";
prog_char s_WT_SETUP_05[] PROGMEM = "set comm size 64";
prog_char s_WT_SETUP_06[] PROGMEM = "set comm match 0x9";
prog_char s_WT_SETUP_07[] PROGMEM = "time";
prog_char s_WT_STATUS_SENSORS[] PROGMEM = "show q 0x177 ";
prog_char s_WT_STATUS_TEMP[] PROGMEM = "show q t ";
prog_char s_WT_STATUS_RSSI[] PROGMEM = "show rssi ";
prog_char s_WT_STATUS_BATT[] PROGMEM = "show battery ";
prog_char s_WT_MSG_JOIN[] PROGMEM = "Credentials Set, Joining ";
prog_char s_WT_MSG_START_WEBTIME[] PROGMEM = "Starting WebTime - Please wait. ";
prog_char s_WT_MSG_RAM[] PROGMEM = "RAM :";
prog_char s_WT_MSG_START_WIFLY[] PROGMEM = "Started WiFly, RAM :";
prog_char s_WT_MSG_WIFI[] PROGMEM = "Initial WiFi Settings :";
prog_char s_WT_MSG_APP_SETTINGS[] PROGMEM = "Configure WebTime Settings...";
prog_char s_WT_HTML_HEAD_01[] PROGMEM = "HTTP/1.1 200 OK \r ";
prog_char s_WT_HTML_HEAD_02[] PROGMEM = "Content-Type: text/html;charset=UTF-8\r ";
prog_char s_WT_HTML_HEAD_03[] PROGMEM = " Content-Length: ";
prog_char s_WT_HTML_HEAD_04[] PROGMEM = "Connection: close \r\n\r\n ";

#define IDX_WT_SETUP_00 0
#define IDX_WT_SETUP_01 IDX_WT_SETUP_00 +1
#define IDX_WT_SETUP_02 IDX_WT_SETUP_01 +1
#define IDX_WT_SETUP_03 IDX_WT_SETUP_02 +1
#define IDX_WT_SETUP_04 IDX_WT_SETUP_03 +1
#define IDX_WT_SETUP_05 IDX_WT_SETUP_04 +1
#define IDX_WT_SETUP_06 IDX_WT_SETUP_05 +1
#define IDX_WT_SETUP_07 IDX_WT_SETUP_06 +1

#define IDX_WT_STATUS_SENSORS    IDX_WT_SETUP_07 +1
#define IDX_WT_STATUS_TEMP       IDX_WT_STATUS_SENSORS +1
#define IDX_WT_STATUS_RSSI       IDX_WT_STATUS_TEMP +1
#define IDX_WT_STATUS_BATT       IDX_WT_STATUS_RSSI +1

#define IDX_WT_MSG_JOIN          IDX_WT_STATUS_BATT +1
#define IDX_WT_MSG_START_WEBTIME IDX_WT_MSG_JOIN +1
#define IDX_WT_MSG_RAM           IDX_WT_MSG_START_WEBTIME +1
#define IDX_WT_MSG_START_WIFLY   IDX_WT_MSG_RAM +1
#define IDX_WT_MSG_WIFI          IDX_WT_MSG_START_WIFLY +1
#define IDX_WT_MSG_APP_SETTINGS  IDX_WT_MSG_WIFI +1

#define IDX_WT_HTML_HEAD_01      IDX_WT_MSG_APP_SETTINGS + 1
#define IDX_WT_HTML_HEAD_02      IDX_WT_HTML_HEAD_01 + 1
#define IDX_WT_HTML_HEAD_03      IDX_WT_HTML_HEAD_02 + 1
#define IDX_WT_HTML_HEAD_04      IDX_WT_HTML_HEAD_03 + 1



PROGMEM const char *WT_string_table[] =
{
  s_WT_SETUP_00,
  s_WT_SETUP_01,
  s_WT_SETUP_02,
  s_WT_SETUP_03,
  s_WT_SETUP_04,
  s_WT_SETUP_05,
  s_WT_SETUP_06,
  s_WT_SETUP_07,
  s_WT_STATUS_SENSORS,
  s_WT_STATUS_TEMP,
  s_WT_STATUS_RSSI,
  s_WT_STATUS_BATT,
  s_WT_MSG_JOIN,
  s_WT_MSG_START_WEBTIME,
  s_WT_MSG_RAM,
  s_WT_MSG_START_WIFLY,
  s_WT_MSG_WIFI,
  s_WT_MSG_APP_SETTINGS,
  s_WT_HTML_HEAD_01,
  s_WT_HTML_HEAD_02,
  s_WT_HTML_HEAD_03,
  s_WT_HTML_HEAD_04
};

#define REQUEST_BUFFER_SIZE 80
#define HEADER_BUFFER_SIZE 120
#define BODY_BUFFER_SIZE 180
#define TEMP_BUFFER_SIZE 40

char bufRequest[REQUEST_BUFFER_SIZE];
char bufTemp[TEMP_BUFFER_SIZE];
char chMisc;
int iRequest = 0;
int iTrack = 0;

WiFlySerial WiFly(ARDUINO_RX_PIN ,ARDUINO_TX_PIN);


// Function for setSyncProvider
time_t GetSyncTime() {
  time_t tCurrent = (time_t) WiFly.GetTime();
  WiFly.exitCommandMode();
  return tCurrent;
}

// GetBuffer_P
// Returns pointer to a supplied Buffer, from PROGMEM based on StringIndex provided.
// based on example from http://arduino.cc/en/Reference/PROGMEM

char* GetBuffer_P(const int StringIndex, char* pBuffer, int bufSize) {
  strncpy_P(pBuffer, (char*)pgm_read_word(&(WT_string_table[StringIndex])), bufSize);
  return pBuffer;
}

// Reconnects to a wifi network.
// DHCP is enabled explicitly.
// You may need to add the MAC address to your MAC filter list.
// Static IP settings available if needed.
boolean Reconnect() {

  WiFly.SendCommand(GetBuffer_P(IDX_WT_SETUP_01,bufTemp,TEMP_BUFFER_SIZE), ">",bufRequest, REQUEST_BUFFER_SIZE);
  WiFly.setUseDHCP(true);
  WiFly.SendCommand(GetBuffer_P(IDX_WT_SETUP_02,bufTemp,TEMP_BUFFER_SIZE),">",bufRequest, REQUEST_BUFFER_SIZE);
  Serial << "Leave current wifi network:" << WiFly.leave() << endl;
  // join
  WiFly.setPassphrase(MY_WIFI_PASSPHRASE);  
  Serial << GetBuffer_P(IDX_WT_MSG_JOIN,bufTemp,TEMP_BUFFER_SIZE) << MY_WIFI_SSID << endl;
  WiFly.join(MY_WIFI_SSID);

  // Set NTP server, update frequency,
  WiFly.setNTP(GetBuffer_P(IDX_WT_SETUP_00,bufTemp,TEMP_BUFFER_SIZE));
  WiFly.setNTP_Update_Frequency(" 15");
  // don't send *HELLO* on http traffic
  // close idle connections after n seconds
  // give enough time for packet data to arrive
  // make data packet size sufficiently large
  // send data packet when a \t appears in stream
  //  force time resync.

  // Configure application-specific settings

  Serial << GetBuffer_P(IDX_WT_MSG_APP_SETTINGS, bufTemp, TEMP_BUFFER_SIZE) << endl;
  for (int i = 1; i< 7 ; i++) {
    WiFly.SendCommand(GetBuffer_P(IDX_WT_SETUP_00 + i,bufTemp,TEMP_BUFFER_SIZE),">",bufRequest, REQUEST_BUFFER_SIZE);
  }

  setTime( WiFly.GetTime() );
  delay(1000);
  setSyncProvider( GetSyncTime );

  // reboot if not working right yet.
  iTrack++;
  if ( iTrack > 5 ) {
    WiFly.reboot();
    iTrack = 0;
  }

}

// Make Response Body
// Based on GET request string, generate a response.
int MakeReponseBody( char* pBody,  char* pRequest, const int sizeRequest ) {

  PString strBody( pBody, BODY_BUFFER_SIZE);

  if ( strstr(pRequest, "/status" ) ) {
    strBody << "<html>WebTime Status:</br>Free RAM:" << freeMemory() << "</br>"
      << "DateTime:" << year() << "-" << month() << "-" << day() << " " << _DEC(hour()) << ":" << minute() << ":" << second() << "</br>";
    strBody << "Battery: " << WiFly.GetBattery(bufTemp,TEMP_BUFFER_SIZE) << "</br>";  
    strBody << "RSSI: " << WiFly.GetRSSI(bufTemp,TEMP_BUFFER_SIZE) << "</br>";
    strBody << "</html>\r\n\r\n";

    // need to exit command mode to be able to send data
    WiFly.exitCommandMode();

  }
  else {
    strBody << "<html>Current request:" << pRequest << "</br>Millis:" << millis() << " Micros:" << micros()
      << "</br>DateTime:" << year() << "-" << month() << "-" << day() << " " << hour() << ":" << minute() << ":" << second()
        << "</html>\r\n\r\n";
    // No calls back to WiFly command mode; hence no need to exit Command mode that wasn't entered.
  }
  return strBody.length();
}

// MakeResponseHeader
// Form a HTML header, including length of body.
int MakeResponseHeader( char* pHeader, char* pBody ) {

  PString strHeader( pHeader, HEADER_BUFFER_SIZE);
  // send a standard http response header  

  strHeader << GetBuffer_P(IDX_WT_HTML_HEAD_01,bufTemp,TEMP_BUFFER_SIZE)
    << GetBuffer_P(IDX_WT_HTML_HEAD_02,bufTemp,TEMP_BUFFER_SIZE)
      << GetBuffer_P(IDX_WT_HTML_HEAD_03,bufTemp,TEMP_BUFFER_SIZE) << (int) strlen(pBody) << " \r"
        << GetBuffer_P(IDX_WT_HTML_HEAD_04,bufTemp,TEMP_BUFFER_SIZE);

  return strHeader.length();
}




//**************************************************************************************************\\


/* Nixie tube demonstration code - function to display an integer
 http://tronixstuff.com - John Boxall. February 2011.
 Modifed sketch originally created by Lionel Haims, July 25, 2008. Released into the public domain. */
// include the library
#include <Nixie.h>
// note the digital pins of the arduino that are connected to the nixie driver
#define dataPin 4  // data line or SER
#define clockPin 5 // clock pin or SCK
#define latchPin 6 // latch pin or RCK
// note the number of digits (nixie tubes) you have (buy more, you need more!)
#define numDigits 4
int narray[numDigits]; // holds the digits to display
int netTime=0;
// Create the Nixie object
// pass in the pin numbers in the correct order
Nixie nixie(dataPin, clockPin, latchPin);


//**************************************************************************************************\\


// This assumes you're using a RadioShack #276-0028 RGB LED
// 2011-05-12: Note!  The anode is the really long pin.
// You'll need to bend it to get it into Digital8.
//
//          ||  ||  ||  ||
//          ||  ||  ||  ||
//          ||  ||  ||  ||
//          G   B   ||  ||
//                  ||  R
//                   A
//         D11 D10 D08 D09

int common_anode = 8;
int red_pin = 9;
int blue_pin = 10;
int green_pin = 11;

int red_min = 150;
int red_max = 255;

int blue_min = 185;
int blue_max = 255;

int green_min = 195;
int green_max = 255;

float h = 0.0;
float s = 1.0;
float v = 0.8;


//**************************************************************************************************\\


void setup()
{
  Serial.begin(9600);
  WiFly.begin();
  nixie.clear(numDigits); // clear display

  pinMode(common_anode, OUTPUT);
  digitalWrite(common_anode, HIGH);
  pinMode(red_pin, OUTPUT);
  digitalWrite(red_pin, HIGH);
  pinMode(green_pin, OUTPUT);
  digitalWrite(green_pin, HIGH);
  pinMode(blue_pin, OUTPUT);
  digitalWrite(blue_pin, HIGH);

  Serial << GetBuffer_P(IDX_WT_MSG_START_WEBTIME,bufTemp,TEMP_BUFFER_SIZE) << endl << GetBuffer_P(IDX_WT_MSG_RAM,bufTemp,TEMP_BUFFER_SIZE) << freeMemory() << endl;


  Serial << GetBuffer_P(IDX_WT_MSG_START_WIFLY,bufTemp,TEMP_BUFFER_SIZE) <<  freeMemory() << endl;

  // get MAC
  Serial << "MAC: " << WiFly.GetMAC(bufRequest, REQUEST_BUFFER_SIZE) << endl;

  Reconnect();
  Serial << "DateTime:" << year() << "-" << month() << "-" << day() << " " << hour() << ":" << minute() << ":" << second() << endl;
  netTime=((hour()-5)*100)+minute();  //set the netTime var for the first time.
  Serial << GetBuffer_P(IDX_WT_MSG_WIFI,bufTemp,TEMP_BUFFER_SIZE) << endl
    << "IP: " << WiFly.GetIP(bufRequest, REQUEST_BUFFER_SIZE) << endl
    << "Netmask: " << WiFly.GetNetMask(bufRequest, REQUEST_BUFFER_SIZE) << endl
    << "Gateway: " << WiFly.GetGateway(bufRequest, REQUEST_BUFFER_SIZE) << endl
    << "DNS: " << WiFly.GetDNS(bufRequest, REQUEST_BUFFER_SIZE) << endl;

  memset (bufRequest,'\0',REQUEST_BUFFER_SIZE);

  Serial << "RSSI: " << WiFly.GetRSSI(bufRequest, REQUEST_BUFFER_SIZE) << endl;
  Serial << "battery: " <<  WiFly.GetBattery(bufRequest, REQUEST_BUFFER_SIZE) << endl;

  // close any open connections
  WiFly.closeConnection();
  Serial << "After Setup RAM:" << freeMemory() << endl ;

  WiFly.exitCommandMode();
  //  WiFly.setDebugChannel( (Print*) &Serial);
}


//**************************************************************************************************\\


void hsv_to_rgb(float h, float s, float v, unsigned char *rc, unsigned char *gc, unsigned char *bc) {
  int h_i = ((int)(h/60)) % 6;

  float f = (h/60) - (int)(h/60);

  float r,g,b;

  float p = v * (1.0 - s);
  float q = v * (1.0 - f*s);
  float t = (1.0 - (1.0 - f)*s);

  switch(h_i) {
  case 0:
    r = v;
    g = t;
    b = p;
    break;
  case 1:
    r = q;
    g = v;
    b = p;
    break;
  case 2:
    r = p;
    g = v;
    b = t;
    break;
  case 3:
    r = p;
    g = q;
    b = v;
    break;
  case 4:
    r = t;
    g = p;
    b = v;
    break;
  case 5:
    r = v;
    g = p;
    b = q;
    break;
  }

  *rc = red_max - (char)((red_max - red_min)*r);
  *gc = green_max - (char)((green_max - green_min)*g);
  *bc = blue_max - (char)((blue_max - blue_min)*b);
}


//**************************************************************************************************\\


void backlightColor()
{
  unsigned char r,g,b;

  h += 1;
  if (h > 360.0)  h -= 360.0;

  hsv_to_rgb(h,s,v, &r,&g,&b);
  analogWrite(red_pin, r);
  analogWrite(green_pin, g);
  analogWrite(blue_pin, b);
}


//**************************************************************************************************\\


void nixNum(int z)
// displays integer 'z' on 4-digit nixie display
// keeps leading zero, as blank still flickers somewhat
{
  narray[0]=int(z/1000); // thousands value
  z=z-(narray[0]*1000);
  narray[1]=int(z/100); // hundreds value
  z=z-(narray[1]*100);
  narray[2]=int(z/10); // tens value
  narray[3]=z-(narray[2]*10); // ones value
  nixie.writeArray( narray, numDigits);
}


//**************************************************************************************************\\


// change the time from GMT to local time zone

void getNetTime()
{
  if (hour()-5 < 0)
  {
    netTime=((hour()+19)*100)+minute();
  }
  else
  {
    netTime=((hour()-5)*100)+minute();
  }
}


//**************************************************************************************************\\


void loop()
{
  if (netTime==0)  // if the netTime var is equal to 0, which should be midnight, then resync the time
  {
    Serial << "Reconnect netTime = " << netTime << endl;  // debug output
    Reconnect();  // Reconnect to the wifi AP and query the NTP server for the time.
    backlightColor();  // The Colors!!!
    delay(10000);  // I run a long delay to keep from querying the NTP server too offten.  No reason to abuse a free service.
  }
  else
  {
    Serial << "netTime = " << netTime << endl;  // debug output
    getNetTime();     // run GMT to local Time Zone conversion
    backlightColor();   // The Colors!!!
    nixNum(netTime);  // send data to the nixie clock.  int array nixNum with int data from gmt converstion.
    delay(100);  // Short delay for the backlight
  }
}